Skip to content

Commit be6dbf8

Browse files
committed
add code splitting
1 parent 2f4ccb8 commit be6dbf8

File tree

7 files changed

+220
-62
lines changed

7 files changed

+220
-62
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
],
1010
"plugins": [
1111
"transform-regenerator",
12-
"transform-async-to-generator"
12+
"transform-async-to-generator",
13+
"dynamic-import-webpack"
1314
],
1415
"env": {
1516
"test": {

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ This starter also embeds `JWT token based` `authentication` (check [Route](https
1616
- *no more struggle in redux with async thank to Apollo Client (you can even get rid of redux thunk or redux saga if you needed theses just for your asyncs. Otherwise keep them since they are nice :smile:)*
1717
- [redux-devtools-extension](https://github.com/zalmoxisus/redux-devtools-extension#redux-devtools-extension) to make developments a pure pleasure.
1818

19+
## New 🎉 since v3.0.0!
20+
21+
- Add `code splitting` by using `dynamic import` (*router level but you can extend to component level as you feel the need*)
22+
- *NOTE: combined with SSR server and SW for caching, this is a good start to follow PWA path!*
23+
- Want to know more about code splitting with SSR a React application, [read this complete beautiful article](https://marmelab.com/blog/2017/10/17/code-splitting.html) (don't forget to clap the author - no I don't even know him 😉 - but this is a really nice job)
24+
1925
## Detailed Content
2026

2127
**Front:**
@@ -26,6 +32,7 @@ This starter also embeds `JWT token based` `authentication` (check [Route](https
2632
- React-Redux 5.x (*Redux is not specific to ReactJS, you could easily use it with Angular2 for instance*)
2733
- React-Router-Redux (*previously named react-simple-router*)
2834
- react-router (4.x- [github :link:](https://github.com/reactjs/react-router))
35+
- loadable-components (1.4.x [github :link:](https://github.com/smooth-code/loadable-components))
2936
- styled-components (3.2.x- [github :link:](https://github.com/styled-components/styled-components))
3037
- Bootstrap (3.x - [github :link:](https://github.com/twbs/bootstrap))
3138
- React-Bootstrap ([github :link:](https://github.com/react-bootstrap/react-bootstrap))

docs/assets/precache-manifest.1b17d6b16807bcc99953987739f19867.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

docs/assets/sw.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"jest": "jest",
99
"dev": "rimraf docs/assets && cross-env NODE_ENV=dev webpack --display-entrypoints --config webpack.dev.config.js",
1010
"prod": "rimraf docs/assets && cross-env NODE_ENV=production webpack -p --config webpack.production.config.js",
11+
"analyze": "rimraf docs/assets && cross-env NODE_ENV=production webpack -p --config webpack.bundle-analyze.config.js",
1112
"start": "cross-env NODE_ENV=dev node server.hot.reload",
1213
"start-spa-dev": "npm run dev && cross-env NODE_ENV=dev nodemon ./src/server/SPA/index.js",
1314
"start-spa-prod": "npm run prod && cross-env NODE_ENV=production node ./src/server/SPA/index.js",
@@ -85,6 +86,7 @@
8586
"babel-eslint": "^8.2.1",
8687
"babel-jest": "^22.4.1",
8788
"babel-loader": "^7.1.3",
89+
"babel-plugin-dynamic-import-webpack": "^1.0.2",
8890
"babel-plugin-transform-async-to-generator": "^6.24.1",
8991
"babel-plugin-transform-regenerator": "^6.26.0",
9092
"babel-preset-env": "^1.6.1",
@@ -135,6 +137,7 @@
135137
"style-loader": "^0.20.1",
136138
"url-loader": "^0.6.2",
137139
"webpack": "^3.10.0",
140+
"webpack-bundle-analyzer": "^2.11.1",
138141
"webpack-dev-middleware": "^2.0.4",
139142
"webpack-dev-server": "^2.11.1",
140143
"webpack-hot-middleware": "^2.21.0",
@@ -169,6 +172,7 @@
169172
"jquery": "^3.0.0",
170173
"js-base64": "^2.4.3",
171174
"jwt-decode": "^2.2.0",
175+
"loadable-components": "^1.4.0",
172176
"lodash.debounce": "^4.0.8",
173177
"lodash.throttle": "^4.1.1",
174178
"moment": "^2.20.1",

webpack.bundle-analyze.config.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// @flow
2+
3+
const webpack = require('webpack');
4+
const path = require('path');
5+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
6+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
7+
.BundleAnalyzerPlugin;
8+
9+
const assetsDir = path.resolve(__dirname, 'docs/assets');
10+
const nodeModulesDir = path.resolve(__dirname, 'node_modules');
11+
const indexFile = path.resolve(__dirname, 'src/front/index.js');
12+
13+
const SPLIT_STYLE = true;
14+
15+
const config = {
16+
entry: {
17+
app: ['babel-polyfill', indexFile],
18+
vendor: [
19+
'apollo-client',
20+
'babel-polyfill',
21+
'bootstrap',
22+
'classnames',
23+
'graphql-tag',
24+
'jquery',
25+
'js-base64',
26+
'moment',
27+
'react',
28+
'react-apollo',
29+
'react-bootstrap',
30+
'react-dom',
31+
'react-motion',
32+
'react-notification',
33+
'react-redux',
34+
'react-router',
35+
'react-router-dom',
36+
'history',
37+
'react-router-redux',
38+
'react-tap-event-plugin',
39+
'redux',
40+
'redux-thunk',
41+
],
42+
},
43+
output: {
44+
path: assetsDir,
45+
filename: 'app.bundle.js',
46+
},
47+
module: {
48+
rules: [
49+
{
50+
test: /\.jsx?$/,
51+
exclude: [nodeModulesDir],
52+
loader: 'babel-loader',
53+
},
54+
{
55+
test: /\.css$/,
56+
use: SPLIT_STYLE
57+
? ExtractTextPlugin.extract({
58+
fallback: 'style-loader',
59+
use: [
60+
{ loader: 'css-loader', options: { importLoaders: 1 } },
61+
'postcss-loader',
62+
],
63+
})
64+
: [
65+
'style-loader',
66+
{ loader: 'css-loader', options: { importLoaders: 1 } },
67+
'postcss-loader',
68+
],
69+
},
70+
{
71+
test: /\.scss$/,
72+
use: SPLIT_STYLE
73+
? ExtractTextPlugin.extract({
74+
fallback: 'style-loader',
75+
use: [
76+
{ loader: 'css-loader', options: { importLoaders: 1 } },
77+
'postcss-loader',
78+
'sass-loader',
79+
],
80+
})
81+
: [
82+
'style-loader',
83+
{ loader: 'css-loader', options: { importLoaders: 1 } },
84+
'postcss-loader',
85+
'sass-loader',
86+
],
87+
},
88+
{
89+
test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif)(\?\S*)?$/,
90+
use: [
91+
{
92+
loader: 'url-loader',
93+
options: {
94+
limit: 100000,
95+
name: '[name].[ext]',
96+
},
97+
},
98+
],
99+
},
100+
],
101+
},
102+
plugins: [
103+
getImplicitGlobals(),
104+
setNodeEnv(),
105+
new ExtractTextPlugin('app.styles.css'),
106+
new webpack.optimize.CommonsChunkPlugin({
107+
name: 'vendor',
108+
filename: 'app.vendor.bundle.js',
109+
}),
110+
new BundleAnalyzerPlugin(),
111+
],
112+
};
113+
114+
/*
115+
* here using hoisting so don't use `var NAME = function()...`
116+
*/
117+
function getImplicitGlobals() {
118+
return new webpack.ProvidePlugin({
119+
$: 'jquery',
120+
jQuery: 'jquery',
121+
});
122+
}
123+
124+
function setNodeEnv() {
125+
return new webpack.DefinePlugin({
126+
'process.env': {
127+
NODE_ENV: JSON.stringify('production'),
128+
},
129+
});
130+
}
131+
132+
module.exports = config;

0 commit comments

Comments
 (0)