Skip to content

Commit c4b740d

Browse files
committed
feat(webpack): add code splitting and chunkhash to assets
1 parent 8502225 commit c4b740d

File tree

7 files changed

+84
-17
lines changed

7 files changed

+84
-17
lines changed

client/routes.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1-
import { Route, IndexRoute } from 'react-router';
2-
import React from 'react';
31
import App from './modules/App/App';
4-
import PostListPage from './modules/Post/pages/PostListPage/PostListPage';
5-
import PostDetailPage from './modules/Post/pages/PostDetailPage/PostDetailPage';
62

7-
const routes = (
8-
<Route path="/" component={App} >
9-
<IndexRoute component={PostListPage} />
10-
<Route path="/post/:slug" component={PostDetailPage} />
11-
</Route>
12-
);
3+
function errorLoading(err) {
4+
console.error('Dynamic page loading failed', err); // eslint-disable-line no-console
5+
}
6+
function loadRoute(cb) {
7+
return (module) => cb(null, module.default);
8+
}
139

14-
export default routes;
10+
// React Router with code-splitting
11+
// More Info: https://medium.com/modus-create-front-end-development/automatic-code-splitting-for-react-router-w-es6-imports-a0abdaa491e9
12+
export default {
13+
component: App,
14+
childRoutes: [
15+
{
16+
path: '/',
17+
name: 'home',
18+
getComponent(nextState, cb) {
19+
System.import('./modules/Post/pages/PostListPage/PostListPage')
20+
.then(loadRoute(cb))
21+
.catch(errorLoading);
22+
},
23+
},
24+
{
25+
path: '/post/:slug',
26+
name: 'post-detail',
27+
getComponent(nextState, cb) {
28+
System.import('./modules/Post/pages/PostDetailPage/PostDetailPage')
29+
.then(loadRoute(cb))
30+
.catch(errorLoading);
31+
},
32+
},
33+
],
34+
};

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
if (process.env.NODE_ENV === 'production') {
33
require('./static/dist/server.bundle.js');
44
} else {
5+
// System.import polyfill
6+
if (typeof System === 'undefined') {
7+
System = {
8+
import: function(path) {
9+
return Promise.resolve(require(path));
10+
},
11+
};
12+
}
13+
514
require('babel-register');
615
require('babel-polyfill');
716
require('css-modules-require-hook');

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"babel-preset-stage-0": "^6.5.0",
6060
"babel-register": "^6.9.0",
6161
"chai": "^3.5.0",
62+
"chunk-manifest-webpack-plugin": "0.0.1",
6263
"clean-css": "^3.4.13",
6364
"css-loader": "^0.23.1",
6465
"css-modules-require-hook": "^4.0.1",
@@ -70,6 +71,7 @@
7071
"eslint-plugin-react": "^5.1.1",
7172
"expect": "^1.20.1",
7273
"extract-text-webpack-plugin": "^1.0.1",
74+
"json-loader": "^0.5.4",
7375
"mocha": "^2.5.3",
7476
"nodemon": "^1.9.2",
7577
"pre-commit": "^1.1.3",
@@ -82,9 +84,10 @@
8284
"style-loader": "^0.13.1",
8385
"supertest": "^1.2.0",
8486
"webpack": "^2.1.0-beta.8",
85-
"webpack-dev-server": "^2.1.0-beta.0",
8687
"webpack-dev-middleware": "^1.6.1",
87-
"webpack-hot-middleware": "^2.10.0"
88+
"webpack-dev-server": "^2.1.0-beta.0",
89+
"webpack-hot-middleware": "^2.10.0",
90+
"webpack-manifest-plugin": "^1.0.1"
8891
},
8992
"engines": {
9093
"node": ">=4"

server/server.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import posts from './routes/post.routes';
3333
import dummyData from './dummyData';
3434
import serverConfig from './config';
3535

36+
// Import Manifests
37+
const assetsManifest = require('../static/dist/manifest.json'); // eslint-disable-line import/no-unresolved
38+
const chunkManifest = require('../static/dist/chunk-manifest.json'); // eslint-disable-line import/no-unresolved
39+
3640
// MongoDB Connection
3741
mongoose.connect(serverConfig.mongoURL, (error) => {
3842
if (error) {
@@ -73,8 +77,12 @@ const renderFullPage = (html, initialState) => {
7377
<div id="root">${html}</div>
7478
<script>
7579
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
80+
//<![CDATA[
81+
window.webpackManifest = ${JSON.stringify(chunkManifest)};
82+
//]]>
7683
</script>
77-
<script src="/dist/bundle.js"></script>
84+
${process.env.NODE_ENV === 'production' ? `<script src='${assetsManifest['/dist/vendor.js']}'></script>` : ''}
85+
<script src="${assetsManifest['/dist/app.js']}"></script>
7886
</body>
7987
</html>
8088
`;

webpack.config.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212

1313
output: {
1414
path: __dirname + '/dist/',
15-
filename: 'bundle.js',
15+
filename: 'app.js',
1616
publicPath: '/dist/',
1717
},
1818

webpack.config.prod.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
var webpack = require('webpack');
22
var ExtractTextPlugin = require('extract-text-webpack-plugin');
3+
var ManifestPlugin = require('webpack-manifest-plugin');
4+
var ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
35

46
module.exports = {
57
devtool: 'source-map',
68

7-
entry: __dirname + "/client/index.js",
9+
entry: {
10+
app: [
11+
'./client/index.js',
12+
],
13+
vendor: [
14+
'react',
15+
'react-dom',
16+
]
17+
},
818

919
output: {
1020
path: __dirname + '/static/dist/',
11-
filename: 'bundle.js',
21+
filename: '[name].[chunkhash].js',
22+
publicPath: '/dist/',
1223
},
1324

1425
resolve: {
@@ -35,6 +46,18 @@ module.exports = {
3546
'NODE_ENV': JSON.stringify('production'),
3647
}
3748
}),
49+
new webpack.optimize.CommonsChunkPlugin({
50+
name: 'vendor',
51+
minChunks: Infinity,
52+
filename: 'vendor.js'
53+
}),
54+
new ManifestPlugin({
55+
basePath: '/dist/'
56+
}),
57+
new ChunkManifestPlugin({
58+
filename: "chunk-manifest.json",
59+
manifestVariable: "webpackManifest"
60+
}),
3861
new webpack.optimize.UglifyJsPlugin({
3962
compressor: {
4063
warnings: false,

webpack.server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ module.exports = {
4040
],
4141
},
4242
},
43+
{
44+
test: /\.json$/,
45+
loader: 'json-loader',
46+
},
4347
],
4448
},
4549
};

0 commit comments

Comments
 (0)