Skip to content

Commit

Permalink
added support for vendor bundling
Browse files Browse the repository at this point in the history
  • Loading branch information
shrynx committed May 22, 2017
1 parent 76cd9ea commit fa83243
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

const autoprefixer = require('autoprefixer');
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const WebpackMd5Hash = require('webpack-md5-hash');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
Expand Down Expand Up @@ -53,6 +55,18 @@ const extractTextPluginOptions = shouldUseRelativeAssetPaths
? // Making sure that the publicPath goes back to to build folder.
{ publicPath: Array(cssFilename.split('/').length).join('../') }
: {};
// Check if vendor file exists
const checkIfVendorFileExists = fs.existsSync(paths.appVendorJs);
// If the vendor file exists, add an entry point for vendor,
// and a seperate entry for polyfills and app index file,
// otherwise keep only polyfills and app index.
const appEntryFiles = [require.resolve('./polyfills'), paths.appIndexJs];
const entryFiles = checkIfVendorFileExists
? {
vendor: paths.appVendorJs,
main: appEntryFiles,
}
: appEntryFiles;

// This is the production configuration.
// It compiles slowly and is focused on producing a fast and minimal bundle.
Expand All @@ -63,8 +77,8 @@ module.exports = {
// We generate sourcemaps in production. This is slow but gives good results.
// You can exclude the *.map files from the build during deployment.
devtool: 'source-map',
// In production, we only want to load the polyfills and the app code.
entry: [require.resolve('./polyfills'), paths.appIndexJs],
// Add the entry point based on whether vendor file exists.
entry: entryFiles,
output: {
// The build folder.
path: paths.appBuild,
Expand Down Expand Up @@ -278,6 +292,17 @@ module.exports = {
// It is absolutely essential that NODE_ENV was set to production here.
// Otherwise React will be compiled in the very slow development mode.
new webpack.DefinePlugin(env.stringified),
// We need to extract out the runtime into a separate manifest file.
// more info: https://webpack.js.org/guides/code-splitting-libraries/#manifest-file
new webpack.optimize.CommonsChunkPlugin({
// Check if vendor file exists, if it does,
// generate a seperate chucks for vendor and manifest file
// else don't generate any common chunck
names: checkIfVendorFileExists ? ['vendor', 'manifest'] : [],
}),
// Need this plugin for deterministic hashing
// until this issue is resolved: https://github.com/webpack/webpack/issues/1315
new WebpackMd5Hash(),
// Minify the code.
new webpack.optimize.UglifyJsPlugin({
compress: {
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"webpack": "2.5.1",
"webpack-dev-server": "2.4.5",
"webpack-manifest-plugin": "1.1.0",
"webpack-md5-hash": "0.0.5",
"whatwg-fetch": "2.0.3"
},
"devDependencies": {
Expand Down

0 comments on commit fa83243

Please sign in to comment.