Skip to content

Commit fa83243

Browse files
committed
added support for vendor bundling
1 parent 76cd9ea commit fa83243

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/react-scripts/config/webpack.config.prod.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
const autoprefixer = require('autoprefixer');
1414
const path = require('path');
15+
const fs = require('fs');
1516
const webpack = require('webpack');
17+
const WebpackMd5Hash = require('webpack-md5-hash');
1618
const HtmlWebpackPlugin = require('html-webpack-plugin');
1719
const ExtractTextPlugin = require('extract-text-webpack-plugin');
1820
const ManifestPlugin = require('webpack-manifest-plugin');
@@ -53,6 +55,18 @@ const extractTextPluginOptions = shouldUseRelativeAssetPaths
5355
? // Making sure that the publicPath goes back to to build folder.
5456
{ publicPath: Array(cssFilename.split('/').length).join('../') }
5557
: {};
58+
// Check if vendor file exists
59+
const checkIfVendorFileExists = fs.existsSync(paths.appVendorJs);
60+
// If the vendor file exists, add an entry point for vendor,
61+
// and a seperate entry for polyfills and app index file,
62+
// otherwise keep only polyfills and app index.
63+
const appEntryFiles = [require.resolve('./polyfills'), paths.appIndexJs];
64+
const entryFiles = checkIfVendorFileExists
65+
? {
66+
vendor: paths.appVendorJs,
67+
main: appEntryFiles,
68+
}
69+
: appEntryFiles;
5670

5771
// This is the production configuration.
5872
// It compiles slowly and is focused on producing a fast and minimal bundle.
@@ -63,8 +77,8 @@ module.exports = {
6377
// We generate sourcemaps in production. This is slow but gives good results.
6478
// You can exclude the *.map files from the build during deployment.
6579
devtool: 'source-map',
66-
// In production, we only want to load the polyfills and the app code.
67-
entry: [require.resolve('./polyfills'), paths.appIndexJs],
80+
// Add the entry point based on whether vendor file exists.
81+
entry: entryFiles,
6882
output: {
6983
// The build folder.
7084
path: paths.appBuild,
@@ -278,6 +292,17 @@ module.exports = {
278292
// It is absolutely essential that NODE_ENV was set to production here.
279293
// Otherwise React will be compiled in the very slow development mode.
280294
new webpack.DefinePlugin(env.stringified),
295+
// We need to extract out the runtime into a separate manifest file.
296+
// more info: https://webpack.js.org/guides/code-splitting-libraries/#manifest-file
297+
new webpack.optimize.CommonsChunkPlugin({
298+
// Check if vendor file exists, if it does,
299+
// generate a seperate chucks for vendor and manifest file
300+
// else don't generate any common chunck
301+
names: checkIfVendorFileExists ? ['vendor', 'manifest'] : [],
302+
}),
303+
// Need this plugin for deterministic hashing
304+
// until this issue is resolved: https://github.com/webpack/webpack/issues/1315
305+
new WebpackMd5Hash(),
281306
// Minify the code.
282307
new webpack.optimize.UglifyJsPlugin({
283308
compress: {

packages/react-scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"webpack": "2.5.1",
5959
"webpack-dev-server": "2.4.5",
6060
"webpack-manifest-plugin": "1.1.0",
61+
"webpack-md5-hash": "0.0.5",
6162
"whatwg-fetch": "2.0.3"
6263
},
6364
"devDependencies": {

0 commit comments

Comments
 (0)