Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example with Hyperapp #549

Merged
merged 4 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/with-hyperapp/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"presets": [
"razzle/babel"
],
"plugins": [
[
"transform-react-jsx",
{
"pragma": "h"
}
]
]
}
13 changes: 13 additions & 0 deletions examples/with-hyperapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
logs
*.log
npm-debug.log*
.DS_Store

coverage
node_modules
build
public/static
.env.local
.env.development.local
.env.test.local
.env.production.local
30 changes: 30 additions & 0 deletions examples/with-hyperapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Razzle Hyperapp Example

## How to use

Download the example [or clone the whole project](https://github.com/jaredpalmer/razzle.git):

```bash
curl https://codeload.github.com/jaredpalmer/razzle/tar.gz/master | tar -xz --strip=2 razzle-master/examples/with-hyperapp
cd with-hyperapp
```

Install it and run:

```bash
yarn install
yarn start
```

## Idea behind the example

This shows how to setup [Hyperapp](https://github.com/hyperapp/hyperapp/) with Razzle.

Here is a list of changes from Razzle's base template:

1. Install `babel-plugin-transform-react-jsx` as a devDependency.
2. Extend Razzle's babel config with a custom `.babelrc`
3. Install `hyperapp` and `"@hyperapp/render`
4. Remove `react`, `react-dom`, `react-router-dom` entirely
5. Update `server.js` to use `@hyperapp/render`'s `withRender` function. Also remove the `<div id="root">` element from our html template since Hyperapp can render to the body.
6. Add a `main.js` file which exports the essential pieces of Hyperapp, which are the `state`, `actions`, and `view`. These are to be shared between the `server.js` and `client.js` files.
20 changes: 20 additions & 0 deletions examples/with-hyperapp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "razzle-examples-with-hyperapp",
"version": "2.0.0-alpha.7",
"license": "MIT",
"scripts": {
"start": "razzle start",
"build": "razzle build",
"test": "razzle test --env=jsdom",
"start:prod": "NODE_ENV=production node build/server.js"
},
"dependencies": {
"@hyperapp/render": "^2.0.0",
"express": "^4.15.2",
"hyperapp": "^1.2.5"
},
"devDependencies": {
"babel-plugin-transform-react-jsx": "^6.23.0",
"razzle": "^2.0.0-alpha.7"
}
}
Binary file added examples/with-hyperapp/public/favicon.ico
Binary file not shown.
4 changes: 4 additions & 0 deletions examples/with-hyperapp/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Disallow: /api/
Disallow: /publicapi/
Disallow: /query/
6 changes: 6 additions & 0 deletions examples/with-hyperapp/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial,
sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
18 changes: 18 additions & 0 deletions examples/with-hyperapp/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @jsx h */
import { h } from 'hyperapp';

import HyperappLogo from './hyperapp.svg';
import './App.css';

const App = ({ state, actions }) => (
<div class="App">
<img src={HyperappLogo} alt="Hyperapp Logo" />
<p>Hello Hyperapp!</p>

<h3>Count is {state.count}</h3>

<button onclick={() => actions.up(1)}>up</button>
<button onclick={() => actions.down(1)}>down</button>
</div>
);
export default App;
13 changes: 13 additions & 0 deletions examples/with-hyperapp/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @jsx h */
import { h } from 'hyperapp';
import { renderToString } from '@hyperapp/render';
import App from './App';

describe('<App />', () => {
test('renders without exploding', () => {
const body = document.createElement('body');
const markup = renderToString(<App state={{}} actions={{}} />);

expect(markup).toContain('App');
});
});
10 changes: 10 additions & 0 deletions examples/with-hyperapp/src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @jsx h */
import { app } from 'hyperapp';

import { state, actions, view } from './main';

app(state, actions, view, document.body);

if (module.hot) {
module.hot.accept();
}
1 change: 1 addition & 0 deletions examples/with-hyperapp/src/hyperapp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions examples/with-hyperapp/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import express from 'express';
import app from './server';

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
17 changes: 17 additions & 0 deletions examples/with-hyperapp/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @jsx h */
import { h } from 'hyperapp';
import App from './App';

/* MODEL */
const state = { count: 0 };

/* UPDATE */
const actions = {
down: value => state => ({ count: state.count - value }),
up: value => state => ({ count: state.count + value }),
};

/* VIEW */
const view = (state, actions) => <App state={state} actions={actions} />;

export { state, actions, view };
12 changes: 12 additions & 0 deletions examples/with-hyperapp/src/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { app } from 'hyperapp';
import { withRender } from '@hyperapp/render';
import { state, actions, view } from './main';

describe('main app', () => {
test('renders without exploding', () => {
const body = document.createElement('body');
const markup = withRender(app)(state, actions, view, body).toString();

expect(markup).toContain('Hello Hyperapp');
});
});
43 changes: 43 additions & 0 deletions examples/with-hyperapp/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/** @jsx h */
import express from 'express';
import { app } from 'hyperapp';
import { withRender } from '@hyperapp/render';

import { state, actions, view } from './main';

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const server = express();
server
.disable('x-powered-by')
.use(express.static(process.env.RAZZLE_PUBLIC_DIR))
.get('/*', (req, res) => {
const markup = withRender(app)(state, actions, view).toString();

res.status(200).send(
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charSet='utf-8' />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${
assets.client.css
? `<link rel="stylesheet" href="${assets.client.css}">`
: ''
}
${
process.env.NODE_ENV === 'production'
? `<script src="${assets.client.js}" defer></script>`
: `<script src="${assets.client.js}" defer crossorigin></script>`
}
</head>
<body>
${markup}
</body>
</html>`
);
});

export default server;
16 changes: 10 additions & 6 deletions examples/with-preact/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ server
<meta charSet='utf-8' />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${assets.client.css
? `<link rel="stylesheet" href="${assets.client.css}">`
: ''}
${process.env.NODE_ENV === 'production'
? `<script src="${assets.client.js}" defer></script>`
: `<script src="${assets.client.js}" defer crossorigin></script>`}
${
assets.client.css
? `<link rel="stylesheet" href="${assets.client.css}">`
: ''
}
${
process.env.NODE_ENV === 'production'
? `<script src="${assets.client.js}" defer></script>`
: `<script src="${assets.client.js}" defer crossorigin></script>`
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't mean to change this file, sorry. Just some autoformat from Prettier.

</head>
<body>
${markup}
Expand Down