Skip to content

Commit

Permalink
Add hot reloading example
Browse files Browse the repository at this point in the history
  • Loading branch information
jordangarcia committed Dec 23, 2015
1 parent d6200bd commit 04f676b
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/hot-reloading/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
presets: ['es2015', 'react'],
plugins: ['transform-decorators-legacy', 'syntax-decorators'],
ignore: [
'**/nuclear-js-react-addons/**',
]
}
2 changes: 2 additions & 0 deletions examples/hot-reloading/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
12 changes: 12 additions & 0 deletions examples/hot-reloading/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<title>NuclearJS Hot Reloading</title>
</head>

<body style="padding: 30px;">
<div id="root"></div>
<script src="http://localhost:3000/dist/app.js"></script>
</body>
</html>

25 changes: 25 additions & 0 deletions examples/hot-reloading/npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
0 info it worked if it ends with ok
1 verbose cli [ '/Users/jordan/.nvm/versions/node/v5.0.0/bin/node',
1 verbose cli '/Users/jordan/.nvm/versions/node/v5.0.0/bin/npm',
1 verbose cli 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose stack Error: missing script: start
4 verbose stack at run (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/lib/run-script.js:148:19)
4 verbose stack at /Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/lib/run-script.js:58:5
4 verbose stack at /Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/read-package-json/read-json.js:345:5
4 verbose stack at checkBinReferences_ (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/read-package-json/read-json.js:309:45)
4 verbose stack at final (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/read-package-json/read-json.js:343:3)
4 verbose stack at then (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/read-package-json/read-json.js:113:5)
4 verbose stack at ReadFileContext.<anonymous> (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/read-package-json/read-json.js:284:20)
4 verbose stack at ReadFileContext.callback (/Users/jordan/.nvm/versions/node/v5.0.0/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:76:16)
4 verbose stack at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:324:13)
5 verbose cwd /Users/jordan/code/nuclear/examples/hot-reloading
6 error Darwin 14.4.0
7 error argv "/Users/jordan/.nvm/versions/node/v5.0.0/bin/node" "/Users/jordan/.nvm/versions/node/v5.0.0/bin/npm" "start"
8 error node v5.0.0
9 error npm v3.3.6
10 error missing script: start
11 error If you need help, you may report this error at:
11 error <https://github.com/npm/npm/issues>
12 verbose exit [ 1, true ]
30 changes: 30 additions & 0 deletions examples/hot-reloading/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "nuclear-js-hot-reloading-example",
"version": "1.0.0",
"description": "Hot Reloading with NuclearJS",
"main": "index.js",
"scripts": {
"start": "node webpack-server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Jordan Garcia",
"license": "MIT",
"dependencies": {
"react": "^0.14.3",
"react-dom": "^0.14.3"
},
"devDependencies": {
"babel-cli": "^6.3.17",
"babel-core": "^6.3.21",
"babel-loader": "^6.2.0",
"babel-plugin-syntax-decorators": "^6.3.13",
"babel-plugin-transform-decorators": "^6.3.13",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"react-hot-loader": "^1.3.0",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.12.1"
}
}
7 changes: 7 additions & 0 deletions examples/hot-reloading/src/actions/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function increment(reactor) {
reactor.dispatch('increment')
}

export function decrement(reactor) {
reactor.dispatch('decrement')
}
24 changes: 24 additions & 0 deletions examples/hot-reloading/src/components/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { Component, PropTypes } from 'react'

class Counter extends Component {
render() {
const { increment, decrement, counter } = this.props
return (
<p>
Clicked: {counter} times
{' '}
<button onClick={increment}>+</button>
{' '}
<button onClick={decrement}>-</button>
</p>
)
}
}

Counter.propTypes = {
increment: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
counter: PropTypes.number.isRequired
}

export default Counter
18 changes: 18 additions & 0 deletions examples/hot-reloading/src/containers/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { Component } from 'react'
import { connect } from 'nuclear-js-react-addons'
import Counter from '../components/Counter'
import { increment, decrement } from '../actions/counter'

@connect(props => ({
counter: ['counter']
}))
export default class AppContainer extends Component {
render() {
let { reactor, counter } = this.props
return <Counter
counter={counter}
increment={increment.bind(null, reactor)}
decrement={decrement.bind(null, reactor)}
/>
}
}
13 changes: 13 additions & 0 deletions examples/hot-reloading/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'nuclear-js-react-addons'
import App from './containers/App'
import reactor from './reactor'
debugger

render(
<Provider reactor={reactor}>
<App />
</Provider>,
document.getElementById('root')
)
16 changes: 16 additions & 0 deletions examples/hot-reloading/src/reactor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Reactor } from 'nuclear-js'
import * as stores from './stores'

const reactor = new Reactor({
debug: true,
})
reactor.registerStores(stores)

if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('./stores', () => {
reactor.replaceStores(require('./stores'))
})
}

export default reactor
11 changes: 11 additions & 0 deletions examples/hot-reloading/src/stores/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Store } from 'nuclear-js'

export default new Store({
getInitialState() {
return 0
},
initialize() {
this.on('increment', (state) => state + 1)
this.on('decrement', (state) => state - 1)
}
})
5 changes: 5 additions & 0 deletions examples/hot-reloading/src/stores/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import counter from './counter'

export {
counter
}
15 changes: 15 additions & 0 deletions examples/hot-reloading/webpack-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
var config = require('./webpack.config');

new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
}).listen(3000, 'localhost', function (err, result) {
if (err) {
console.log(err);
}

console.log('Listening at localhost:3000');
});
28 changes: 28 additions & 0 deletions examples/hot-reloading/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var path = require('path');
var webpack = require('webpack');

module.exports = {
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./src/main'
],
output: {
path: path.join(__dirname, 'public', 'dist'),
filename: 'app.js',
publicPath: 'http://localhost:3000/dist/'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel-loader'],
}
]
},
devtool: 'eval',
}

0 comments on commit 04f676b

Please sign in to comment.