Skip to content

Commit 73b5553

Browse files
committed
HMR, better sourcemaps, HtmlWebpackPlugin
1 parent 696a532 commit 73b5553

File tree

7 files changed

+251
-21
lines changed

7 files changed

+251
-21
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ Webpack 2 automatically splits routes in chunks (small bundles) and loads them o
66

77
## Contains
88

9-
- [x] [Webpack 2](https://webpack.github.io)
9+
- [x] [Webpack 2.2](https://webpack.github.io)
1010
- [x] [React 0.15](https://facebook.github.io/react/)
11-
- [x] [Babel 6.5](https://babeljs.io/)
11+
- [x] [Babel 6.x](https://babeljs.io/)
12+
13+
## Features
14+
15+
- [x] Tree shaking
16+
- [x] Code Splitting
17+
- [x] Hot Module Replacement
18+
- [x] ES6 Source debugging in Chrome with sourcemaps
19+
- [x] React Router
20+
- [x] Yarn-friendly
1221

1322
##System Requirements
1423
Before installing the dependencies, make sure your system has the correct Node and Npm versions, otherwise you will get errors.
1524

16-
- Node 5.x.x
25+
- Node 6.x.x
1726
- Npm 3.x.x
1827

1928
## Setup

client/index.html renamed to client/index.ejs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
<meta name="viewport" content="width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1.0" />
55
<meta charset="utf-8">
66
<title>React Router + Webpack 2 + Dynamic Chunk Navigation</title>
7-
<link href="/style.css" media="all" rel="stylesheet" />
87
</head>
98
<body>
109
<div id="root"></div>
11-
<script src="/vendor.bundle.js"></script>
12-
<script src="/bundle.js"></script>
1310
</body>
1411
</html>

client/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { render } from 'react-dom';
22
import React from 'react';
3-
import { Router, browserHistory } from 'react-router/es6';
4-
import rootRoute from 'pages/routes';
5-
import 'index.html';
3+
import Root from 'pages/routes';
64
import 'general.scss';
75

86
render(
9-
<Router history={browserHistory} routes={rootRoute} />,
7+
<Root />,
108
document.getElementById('root')
119
);
10+
11+
if (module.hot) {
12+
module.hot.accept('pages/routes', () => {
13+
const NewRoot = require('pages/routes').default;
14+
15+
render(
16+
<NewRoot />,
17+
document.getElementById('root')
18+
);
19+
});
20+
}

client/pages/routes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import React from 'react';
2+
import { Router, browserHistory } from 'react-router/es6';
13
import App from 'containers/App';
24

35
function errorLoading(err) {
@@ -8,7 +10,7 @@ function loadRoute(cb) {
810
return (module) => cb(null, module.default);
911
}
1012

11-
export default {
13+
const routes = {
1214
component: App,
1315
childRoutes: [
1416
{
@@ -37,3 +39,5 @@ export default {
3739
},
3840
]
3941
};
42+
43+
export default () => <Router history={browserHistory} routes={routes} />;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"css-loader": "0.14.5",
2121
"extract-text-webpack-plugin": "2.0.0-beta.5",
2222
"file-loader": "0.8.5",
23+
"html-webpack-plugin": "2.26.0",
2324
"node-sass": "3.12.2",
2425
"sass-loader": "3.2.3",
2526
"style-loader": "0.13.1",

webpack.config.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const webpack = require('webpack');
22
const path = require('path');
33
const ExtractTextPlugin = require('extract-text-webpack-plugin');
4+
const HtmlWebpackPlugin = require('html-webpack-plugin');
45

56
const nodeEnv = process.env.NODE_ENV || 'development';
67
const isProd = nodeEnv === 'production';
@@ -19,6 +20,16 @@ const plugins = [
1920
new webpack.DefinePlugin({
2021
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
2122
}),
23+
new HtmlWebpackPlugin({
24+
template: sourcePath + '/index.ejs',
25+
production: isProd,
26+
inject: true,
27+
}),
28+
];
29+
30+
const jsEntry = [
31+
'index',
32+
'pages/Home',
2233
];
2334

2435
if (isProd) {
@@ -46,16 +57,23 @@ if (isProd) {
4657
}),
4758
extractCSS
4859
);
60+
61+
jsEntry.unshift(
62+
'webpack-dev-server/client?http://localhost:3000',
63+
'webpack/hot/only-dev-server'
64+
);
65+
} else {
66+
plugins.push(
67+
new webpack.HotModuleReplacementPlugin(),
68+
new webpack.NamedModulesPlugin()
69+
);
4970
}
5071

5172
module.exports = {
52-
devtool: isProd ? 'source-map' : 'eval',
73+
devtool: isProd ? 'source-map' : 'cheap-module-source-map',
5374
context: sourcePath,
5475
entry: {
55-
js: [
56-
'index',
57-
'pages/Home'
58-
],
76+
js: jsEntry,
5977
vendor: [
6078
'react',
6179
'react-dom'
@@ -116,6 +134,7 @@ module.exports = {
116134
contentBase: './client',
117135
historyApiFallback: true,
118136
port: 3000,
137+
hot: true,
119138
compress: isProd,
120139
stats: { colors: true },
121140
}

0 commit comments

Comments
 (0)