12
12
13
13
const autoprefixer = require ( 'autoprefixer' ) ;
14
14
const path = require ( 'path' ) ;
15
+ const fs = require ( 'fs' ) ;
15
16
const webpack = require ( 'webpack' ) ;
17
+ const WebpackMd5Hash = require ( 'webpack-md5-hash' ) ;
16
18
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
17
19
const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ;
18
20
const ManifestPlugin = require ( 'webpack-manifest-plugin' ) ;
@@ -53,6 +55,18 @@ const extractTextPluginOptions = shouldUseRelativeAssetPaths
53
55
? // Making sure that the publicPath goes back to to build folder.
54
56
{ publicPath : Array ( cssFilename . split ( '/' ) . length ) . join ( '../' ) }
55
57
: { } ;
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 ;
56
70
57
71
// This is the production configuration.
58
72
// It compiles slowly and is focused on producing a fast and minimal bundle.
@@ -63,8 +77,8 @@ module.exports = {
63
77
// We generate sourcemaps in production. This is slow but gives good results.
64
78
// You can exclude the *.map files from the build during deployment.
65
79
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 ,
68
82
output : {
69
83
// The build folder.
70
84
path : paths . appBuild ,
@@ -278,6 +292,17 @@ module.exports = {
278
292
// It is absolutely essential that NODE_ENV was set to production here.
279
293
// Otherwise React will be compiled in the very slow development mode.
280
294
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 ( ) ,
281
306
// Minify the code.
282
307
new webpack . optimize . UglifyJsPlugin ( {
283
308
compress : {
0 commit comments