|
| 1 | +// config that are specific to --target app |
| 2 | + |
| 3 | +module.exports = (api, options) => { |
| 4 | + api.chainWebpack(webpackConfig => { |
| 5 | + // only apply when there's no alternative target |
| 6 | + if (process.env.VUE_CLI_TARGET) { |
| 7 | + return |
| 8 | + } |
| 9 | + |
| 10 | + // inject preload/prefetch to HTML |
| 11 | + const PreloadPlugin = require('../webpack/PreloadPlugin') |
| 12 | + webpackConfig |
| 13 | + .plugin('preload') |
| 14 | + .use(PreloadPlugin, [{ |
| 15 | + rel: 'preload', |
| 16 | + include: 'initial', |
| 17 | + fileBlacklist: [/\.map$/, /hot-update\.js$/] |
| 18 | + }]) |
| 19 | + |
| 20 | + webpackConfig |
| 21 | + .plugin('prefetch') |
| 22 | + .use(PreloadPlugin, [{ |
| 23 | + rel: 'prefetch', |
| 24 | + include: 'asyncChunks' |
| 25 | + }]) |
| 26 | + |
| 27 | + // HTML plugin |
| 28 | + const fs = require('fs') |
| 29 | + const htmlPath = api.resolve('public/index.html') |
| 30 | + const resolveClientEnv = require('../util/resolveClientEnv') |
| 31 | + webpackConfig |
| 32 | + .plugin('html') |
| 33 | + .use(require('html-webpack-plugin'), [ |
| 34 | + Object.assign( |
| 35 | + fs.existsSync(htmlPath) ? { template: htmlPath } : {}, |
| 36 | + // expose client env to html template |
| 37 | + { env: resolveClientEnv(options.baseUrl, true /* raw */) } |
| 38 | + ) |
| 39 | + ]) |
| 40 | + |
| 41 | + if (process.env.NODE_ENV === 'production') { |
| 42 | + // minify HTML |
| 43 | + webpackConfig |
| 44 | + .plugin('html') |
| 45 | + .tap(([options]) => [Object.assign(options, { |
| 46 | + minify: { |
| 47 | + removeComments: true, |
| 48 | + collapseWhitespace: true, |
| 49 | + removeAttributeQuotes: true |
| 50 | + // more options: |
| 51 | + // https://github.com/kangax/html-minifier#options-quick-reference |
| 52 | + }, |
| 53 | + // necessary to consistently work with multiple chunks via CommonsChunkPlugin |
| 54 | + chunksSortMode: 'dependency' |
| 55 | + })]) |
| 56 | + |
| 57 | + // Code splitting configs for better long-term caching |
| 58 | + // This needs to be updated when upgrading to webpack 4 |
| 59 | + const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin') |
| 60 | + |
| 61 | + if (!options.dll) { |
| 62 | + // extract vendor libs into its own chunk for better caching, since they |
| 63 | + // are more likely to stay the same. |
| 64 | + webpackConfig |
| 65 | + .plugin('split-vendor') |
| 66 | + .use(CommonsChunkPlugin, [{ |
| 67 | + name: 'vendor', |
| 68 | + minChunks (module) { |
| 69 | + // any required modules inside node_modules are extracted to vendor |
| 70 | + return ( |
| 71 | + module.resource && |
| 72 | + /\.js$/.test(module.resource) && |
| 73 | + module.resource.indexOf(`node_modules`) > -1 |
| 74 | + ) |
| 75 | + } |
| 76 | + }]) |
| 77 | + |
| 78 | + // extract webpack runtime and module manifest to its own file in order to |
| 79 | + // prevent vendor hash from being updated whenever app bundle is updated |
| 80 | + webpackConfig |
| 81 | + .plugin('split-manifest') |
| 82 | + .use(CommonsChunkPlugin, [{ |
| 83 | + name: 'manifest', |
| 84 | + minChunks: Infinity |
| 85 | + }]) |
| 86 | + |
| 87 | + // inline the manifest chunk into HTML |
| 88 | + webpackConfig |
| 89 | + .plugin('inline-manifest') |
| 90 | + .use(require('../webpack/InlineSourcePlugin'), [{ |
| 91 | + include: /manifest\..*\.js$/ |
| 92 | + }]) |
| 93 | + |
| 94 | + // since manifest is inlined, don't preload it anymore |
| 95 | + webpackConfig |
| 96 | + .plugin('preload') |
| 97 | + .tap(([options]) => { |
| 98 | + options.fileBlacklist.push(/manifest\..*\.js$/) |
| 99 | + return [options] |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + // This CommonsChunkPlugin instance extracts shared chunks from async |
| 104 | + // chunks and bundles them in a separate chunk, similar to the vendor chunk |
| 105 | + // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk |
| 106 | + webpackConfig |
| 107 | + .plugin('split-vendor-async') |
| 108 | + .use(CommonsChunkPlugin, [{ |
| 109 | + name: 'app', |
| 110 | + async: 'vendor-async', |
| 111 | + children: true, |
| 112 | + minChunks: 3 |
| 113 | + }]) |
| 114 | + |
| 115 | + // DLL |
| 116 | + if (options.dll) { |
| 117 | + const webpack = require('webpack') |
| 118 | + const UglifyPlugin = require('uglifyjs-webpack-plugin') |
| 119 | + const getUglifyOptions = require('./uglifyOptions') |
| 120 | + const dllEntries = Array.isArray(options.dll) |
| 121 | + ? options.dll |
| 122 | + : Object.keys(api.service.pkg.dependencies) |
| 123 | + |
| 124 | + webpackConfig |
| 125 | + .plugin('dll') |
| 126 | + .use(require('autodll-webpack-plugin'), [{ |
| 127 | + inject: true, |
| 128 | + inherit: true, |
| 129 | + path: 'js/', |
| 130 | + context: api.resolve('.'), |
| 131 | + filename: '[name].[hash:8].js', |
| 132 | + entry: { |
| 133 | + 'vendor': [ |
| 134 | + ...dllEntries, |
| 135 | + 'vue-loader/lib/component-normalizer' |
| 136 | + ] |
| 137 | + }, |
| 138 | + plugins: [ |
| 139 | + new webpack.DefinePlugin(resolveClientEnv(options.baseUrl)), |
| 140 | + new UglifyPlugin(getUglifyOptions(options)) |
| 141 | + ] |
| 142 | + }]) |
| 143 | + .after('preload') |
| 144 | + } |
| 145 | + |
| 146 | + // copy static assets in public/ |
| 147 | + webpackConfig |
| 148 | + .plugin('copy') |
| 149 | + .use(require('copy-webpack-plugin'), [[{ |
| 150 | + from: api.resolve('public'), |
| 151 | + to: api.resolve(options.outputDir), |
| 152 | + ignore: ['index.html', '.*'] |
| 153 | + }]]) |
| 154 | + } |
| 155 | + }) |
| 156 | +} |
0 commit comments