Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit 044baf4

Browse files
committed
added vendor chuncking
1 parent 0e95ff3 commit 044baf4

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

packages/react-scripts/config/paths.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ module.exports = {
6464
appIndexJs: resolveApp(
6565
customEntryFile(resolveApp('package.json'), '') || 'src/index.js'
6666
),
67+
appVendorJs: resolveApp('src/vendor.js'),
6768
appPackageJson: resolveApp('package.json'),
6869
appSrc: resolveApp('src'),
6970
yarnLockFile: resolveApp('yarn.lock'),
@@ -86,6 +87,7 @@ module.exports = {
8687
appIndexJs: resolveApp(
8788
customEntryFile(resolveApp('package.json'), '') || 'src/index.js'
8889
),
90+
appVendorJs: resolveApp('src/vendor.js'),
8991
appPackageJson: resolveApp('package.json'),
9092
appSrc: resolveApp('src'),
9193
yarnLockFile: resolveApp('yarn.lock'),
@@ -119,6 +121,7 @@ if (
119121
customEntryFile(resolveOwn('package.json'), 'template/') ||
120122
'template/src/index.js'
121123
),
124+
appVendorJs: resolveOwn('template/src/vendor.js'),
122125
appPackageJson: resolveOwn('package.json'),
123126
appSrc: resolveOwn('template/src'),
124127
yarnLockFile: resolveOwn('template/yarn.lock'),

packages/react-scripts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"jest": "20.0.4",
4848
"less": "^2.7.2",
4949
"less-loader": "^4.0.5",
50+
"name-all-modules-plugin": "^1.0.1",
5051
"node-sass": "^4.5.3",
5152
"object-assign": "4.1.1",
5253
"postcss-flexbugs-fixes": "3.0.0",
@@ -55,6 +56,7 @@
5556
"react-dev-utils": "^3.0.2",
5657
"react-error-overlay": "^1.0.9",
5758
"sass-loader": "^6.0.6",
59+
"script-ext-html-webpack-plugin": "^1.8.5",
5860
"style-loader": "0.18.2",
5961
"sw-precache-webpack-plugin": "0.11.3",
6062
"url-loader": "0.5.9",

packages/react-scripts/scripts/utils/superScriptWebpackConfigurator.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,64 @@
22

33
const ExtractTextPlugin = require('extract-text-webpack-plugin');
44
const autoprefixer = require('autoprefixer');
5+
const fs = require('fs');
6+
const path = require('path');
7+
const webpack = require('webpack');
8+
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
59
const paths = require('../../config/paths');
610
const superScriptConfigOptions = require('./superScriptConfigOption');
711

12+
const addVendorSplitting = ({ config, env }) => {
13+
const checkIfVendorFileExists = fs.existsSync(paths.appVendorJs);
14+
if (env === 'prod' && checkIfVendorFileExists) {
15+
// Add the entry point based on whether vendor file exists.
16+
config.entry = {
17+
vendor: [require.resolve('../../config/polyfills'), paths.appVendorJs],
18+
main: paths.appIndexJs,
19+
};
20+
21+
const vendorChcukedConfig = addPluginAtStart(config, [
22+
new webpack.NamedModulesPlugin(),
23+
new webpack.NamedChunksPlugin(chunk => {
24+
if (chunk.name) {
25+
return chunk.name;
26+
}
27+
return chunk.modules
28+
.map(m => path.relative(m.context, m.request))
29+
.join('_');
30+
}),
31+
new webpack.optimize.CommonsChunkPlugin(
32+
// generate a seperate chucks for vendor
33+
// else don't generate any common chunck
34+
{
35+
name: 'vendor',
36+
minChunks: Infinity,
37+
}
38+
),
39+
// We need to extract out the runtime into a separate manifest file.
40+
// more info: https://webpack.js.org/guides/code-splitting-libraries/#manifest-file
41+
new webpack.optimize.CommonsChunkPlugin(
42+
// generate a seperate chucks for manifest file
43+
// else don't generate any common chunck
44+
{
45+
name: 'manifest',
46+
}
47+
),
48+
]);
49+
50+
// This ensures that the browser will load the scripts in parallel,
51+
// but execute them in the order they appear in the document.
52+
const configWithVendorAndDefer = addPluginAtEnd(vendorChcukedConfig, [
53+
new ScriptExtHtmlWebpackPlugin({
54+
defaultAttribute: 'defer',
55+
}),
56+
]);
57+
58+
return { config: configWithVendorAndDefer, env };
59+
}
60+
return { config, env };
61+
};
62+
863
const addPreactAlias = ({ config, env }) => {
964
if (superScriptConfigOptions('usePreact')) {
1065
const preactAlias = {
@@ -331,7 +386,8 @@ const superScriptWebpackConfigurator = (config, env) => {
331386
addImageLoader,
332387
updateBabelConfig,
333388
updateEslintConfig,
334-
addPreactAlias
389+
addPreactAlias,
390+
addVendorSplitting
335391
);
336392

337393
return superScriptWebpackConfig(configParam).config;

0 commit comments

Comments
 (0)