Skip to content
This repository was archived by the owner on Jun 16, 2020. It is now read-only.

Commit 36f18e6

Browse files
authored
Merge pull request #196 from sladiri/master
Add amok example (node module, npm scripts, code in browser.js).
2 parents f48bf96 + 1464336 commit 36f18e6

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

examples/hot-reload/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Hot module replacement with browserify/watchify or webpack
1+
# Hot module replacement with browserify/watchify, webpack or amok
22

33
Using hot reloading while developing can improve your workflow.
44
No more clicking through your application to a particular state each time you change the rendering code!
@@ -16,13 +16,22 @@ To install and run:
1616
// for webpack
1717
npm run hot-webpack
1818

19+
// for amok
20+
npm run hot-amok
21+
22+
### browserify and webpack
1923
Edit [package.json][] if you want to run on a different port.
2024
If you're running inside a container or VM, add `--host 0.0.0.0` (webpack) so you can access the dev server from your host machine.
2125

2226
Try clicking to increment the counter, then edit [render.js][]!
2327
Your changes should instantly appear on the page without refreshing or upsetting the counter's value.
2428
Note that if you edit [browser.js][], your page will refresh and your state will be reset (only using webpack).
2529

30+
### amok
31+
Amok starts its own web server to provide hot reloading and also starts the browser. Live editing is not limited to the render function. It uses a V8 feature instead of eval and is limited to Chrome or Chromium browsers. Change the counter increment in `browser.js` for example.
32+
33+
Edit [package.json][] to switch the web browser to use (Chrome or Chromium).
34+
2635
[package.json]: ./package.json
2736
[render.js]: ./render.js
2837
[browser.js]: ./browser.js
@@ -38,6 +47,11 @@ We have to be careful to not reseat any references - so we pass a function that
3847
### browserify/watchify
3948
The main difference to webpack is that browserify does not come with a dev-server which is why we use [http-server][] to host our example. Other than that it works quite similar. Under the hood [browserify-hmr][] imitates webpack's hot module replacement.
4049

50+
### amok
51+
[Amok][] uses watchify to fire a `patch` event on the window object when a file is changed. Similarly to the webpack/browserify, we must change the state so Mercury re-renders automatically. Without this action, the changes in the code would only be seen after we clicked the button in the example. (The button click would change the state and trigger a re-render as well.)
52+
4153
[hot module replacement]: https://github.com/webpack/docs/wiki/hot-module-replacement-with-webpack
4254
[http-server]: https://github.com/indexzero/http-server
4355
[browserify-hmr]: https://github.com/AgentME/browserify-hmr
56+
[Amok]: https://github.com/caspervonb/amok
57+

examples/hot-reload/browser.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var hg = require('../../index.js');
44
var document = require('global/document');
5+
var window = require('global/window');
56

67
// Copied from examples/count.js
78
function App() {
@@ -18,23 +19,34 @@ function incrementCount(state) {
1819
state.count.set(state.count() + 1);
1920
}
2021

21-
// This render function may be replaced!
22+
// This render function may be replaced by webpack and browserify!
2223
var render = require('./render.js');
2324
App.render = function renderApp(state) {
2425
return render(state);
2526
};
2627

2728
// Need a reference to this below.
2829
var appState = App();
30+
2931
hg.app(document.body, appState, App.render);
3032

31-
// Special sauce: detect changes to the rendering code and swap the rendering
33+
// Special sauce for webpack and browserify:
34+
// Detect changes to the rendering code and swap the rendering
3235
// function out without reloading the page.
3336
if (module.hot) {
3437
module.hot.accept('./render.js', function swapModule() {
3538
render = require('./render.js');
36-
// Force a re-render by changing the application state.
37-
appState._hotVersion.set(appState._hotVersion() + 1);
39+
forceRerender(appState);
3840
return true;
3941
});
4042
}
43+
44+
// Otherwise, if using amok, an event is fired when a file changes.
45+
window.addEventListener('patch', function() {
46+
forceRerender(appState);
47+
});
48+
49+
// Force a re-render by changing the application state.
50+
function forceRerender(appState) {
51+
appState._hotVersion.set(appState._hotVersion() + 1);
52+
}

examples/hot-reload/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
"scripts": {
3-
"hot-browserify": "npm run http-server && npm run watchify",
3+
"hot-browserify": "npm run http-server && npm run watchify-hmr",
44
"hot-webpack": "webpack-dev-server --port 8080 --hot --progress --colors --inline ./browser.js",
55
"http-server": "http-server -c 1 -a localhost &",
6-
"watchify": "watchify -p browserify-hmr browser.js -o bundle.js"
6+
"watchify-hmr": "watchify -p browserify-hmr browser.js -o bundle.js",
7+
"hot-amok": "npm run watchify & npm run amok-html",
8+
"watchify": "watchify browser.js -o bundle.js",
9+
"amok-html": "amok file://$PWD/index.html --browser chrome --hot"
710
},
811
"devDependencies": {
12+
"amok": "^1.1.3",
913
"browserify-hmr": "^0.2.2",
1014
"http-server": "^0.8.0",
1115
"watchify": "^3.4.0",

0 commit comments

Comments
 (0)