|
2 | 2 |
|
3 | 3 | const ExtractTextPlugin = require('extract-text-webpack-plugin');
|
4 | 4 | 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'); |
5 | 9 | const paths = require('../../config/paths');
|
6 | 10 | const superScriptConfigOptions = require('./superScriptConfigOption');
|
7 | 11 |
|
| 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 | + |
8 | 63 | const addPreactAlias = ({ config, env }) => {
|
9 | 64 | if (superScriptConfigOptions('usePreact')) {
|
10 | 65 | const preactAlias = {
|
@@ -331,7 +386,8 @@ const superScriptWebpackConfigurator = (config, env) => {
|
331 | 386 | addImageLoader,
|
332 | 387 | updateBabelConfig,
|
333 | 388 | updateEslintConfig,
|
334 |
| - addPreactAlias |
| 389 | + addPreactAlias, |
| 390 | + addVendorSplitting |
335 | 391 | );
|
336 | 392 |
|
337 | 393 | return superScriptWebpackConfig(configParam).config;
|
|
0 commit comments