2017-12-15 06:07:55 +00:00
|
|
|
'use strict'
|
|
|
|
const path = require('path')
|
|
|
|
const utils = require('./utils')
|
|
|
|
const webpack = require('webpack')
|
|
|
|
const config = require('../config')
|
|
|
|
const merge = require('webpack-merge')
|
|
|
|
const baseWebpackConfig = require('./webpack.base.conf')
|
|
|
|
const CopyWebpackPlugin = require('copy-webpack-plugin')
|
|
|
|
const HtmlWebpackPlugin = require('html-webpack-plugin')
|
2018-08-15 07:33:34 +00:00
|
|
|
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
|
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
2018-10-23 01:51:57 +00:00
|
|
|
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
|
2017-12-15 06:07:55 +00:00
|
|
|
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
|
2017-04-18 07:09:13 +00:00
|
|
|
|
2018-08-15 07:33:34 +00:00
|
|
|
function resolve(dir) {
|
2017-12-15 06:07:55 +00:00
|
|
|
return path.join(__dirname, '..', dir)
|
2017-04-18 07:09:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 07:33:34 +00:00
|
|
|
const env = require('../config/' + process.env.env_config + '.env')
|
|
|
|
|
|
|
|
// For NamedChunksPlugin
|
|
|
|
const seen = new Set()
|
|
|
|
const nameLength = 4
|
2017-12-15 06:07:55 +00:00
|
|
|
|
|
|
|
const webpackConfig = merge(baseWebpackConfig, {
|
2018-08-15 07:33:34 +00:00
|
|
|
mode: 'production',
|
2017-08-07 08:11:10 +00:00
|
|
|
module: {
|
|
|
|
rules: utils.styleLoaders({
|
|
|
|
sourceMap: config.build.productionSourceMap,
|
2017-11-09 10:27:00 +00:00
|
|
|
extract: true,
|
|
|
|
usePostCSS: true
|
2017-08-07 08:11:10 +00:00
|
|
|
})
|
|
|
|
},
|
2017-12-15 06:07:55 +00:00
|
|
|
devtool: config.build.productionSourceMap ? config.build.devtool : false,
|
2017-08-07 08:11:10 +00:00
|
|
|
output: {
|
|
|
|
path: config.build.assetsRoot,
|
2018-08-15 07:33:34 +00:00
|
|
|
filename: utils.assetsPath('js/[name].[chunkhash:8].js'),
|
|
|
|
chunkFilename: utils.assetsPath('js/[name].[chunkhash:8].js')
|
2017-08-07 08:11:10 +00:00
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
// http://vuejs.github.io/vue-loader/en/workflow/production.html
|
|
|
|
new webpack.DefinePlugin({
|
2017-11-27 02:51:54 +00:00
|
|
|
'process.env': env
|
2017-08-07 08:11:10 +00:00
|
|
|
}),
|
|
|
|
// extract css into its own file
|
2018-08-15 07:33:34 +00:00
|
|
|
new MiniCssExtractPlugin({
|
|
|
|
filename: utils.assetsPath('css/[name].[contenthash:8].css'),
|
|
|
|
chunkFilename: utils.assetsPath('css/[name].[contenthash:8].css')
|
2017-12-15 06:07:55 +00:00
|
|
|
}),
|
2017-08-07 08:11:10 +00:00
|
|
|
// generate dist index.html with correct asset hash for caching.
|
|
|
|
// you can customize output by editing /index.html
|
|
|
|
// see https://github.com/ampedandwired/html-webpack-plugin
|
|
|
|
new HtmlWebpackPlugin({
|
2017-12-15 06:07:55 +00:00
|
|
|
filename: config.build.index,
|
2017-08-07 08:11:10 +00:00
|
|
|
template: 'index.html',
|
|
|
|
inject: true,
|
2017-12-15 06:07:55 +00:00
|
|
|
favicon: resolve('favicon.ico'),
|
|
|
|
title: 'vue-element-admin',
|
2018-10-09 07:23:27 +00:00
|
|
|
templateParameters: {
|
|
|
|
BASE_URL: config.build.assetsPublicPath + config.build.assetsSubDirectory,
|
|
|
|
},
|
2017-08-07 08:11:10 +00:00
|
|
|
minify: {
|
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
2017-12-15 06:07:55 +00:00
|
|
|
removeAttributeQuotes: true
|
|
|
|
// more options:
|
|
|
|
// https://github.com/kangax/html-minifier#options-quick-reference
|
2017-08-07 08:11:10 +00:00
|
|
|
}
|
2018-08-15 07:33:34 +00:00
|
|
|
// default sort mode uses toposort which cannot handle cyclic deps
|
|
|
|
// in certain cases, and in webpack 4, chunk order in HTML doesn't
|
|
|
|
// matter anyway
|
2017-08-07 08:11:10 +00:00
|
|
|
}),
|
2018-08-15 07:33:34 +00:00
|
|
|
new ScriptExtHtmlWebpackPlugin({
|
|
|
|
//`runtime` must same as runtimeChunk name. default is `runtime`
|
|
|
|
inline: /runtime\..*\.js$/
|
2017-11-23 02:33:29 +00:00
|
|
|
}),
|
2018-08-15 07:33:34 +00:00
|
|
|
// keep chunk.id stable when chunk has no name
|
|
|
|
new webpack.NamedChunksPlugin(chunk => {
|
|
|
|
if (chunk.name) {
|
|
|
|
return chunk.name
|
2017-08-07 08:11:10 +00:00
|
|
|
}
|
2018-08-15 07:33:34 +00:00
|
|
|
const modules = Array.from(chunk.modulesIterable)
|
|
|
|
if (modules.length > 1) {
|
|
|
|
const hash = require('hash-sum')
|
|
|
|
const joinedHash = hash(modules.map(m => m.id).join('_'))
|
|
|
|
let len = nameLength
|
|
|
|
while (seen.has(joinedHash.substr(0, len))) len++
|
|
|
|
seen.add(joinedHash.substr(0, len))
|
|
|
|
return `chunk-${joinedHash.substr(0, len)}`
|
|
|
|
} else {
|
|
|
|
return modules[0].id
|
2017-09-26 08:08:20 +00:00
|
|
|
}
|
|
|
|
}),
|
2018-08-15 07:33:34 +00:00
|
|
|
// keep module.id stable when vender modules does not change
|
|
|
|
new webpack.HashedModuleIdsPlugin(),
|
2017-08-07 08:11:10 +00:00
|
|
|
// copy custom static assets
|
2017-12-15 06:07:55 +00:00
|
|
|
new CopyWebpackPlugin([
|
|
|
|
{
|
|
|
|
from: path.resolve(__dirname, '../static'),
|
|
|
|
to: config.build.assetsSubDirectory,
|
|
|
|
ignore: ['.*']
|
|
|
|
}
|
|
|
|
])
|
2018-08-15 07:33:34 +00:00
|
|
|
],
|
|
|
|
optimization: {
|
|
|
|
splitChunks: {
|
|
|
|
chunks: 'all',
|
|
|
|
cacheGroups: {
|
|
|
|
libs: {
|
|
|
|
name: 'chunk-libs',
|
|
|
|
test: /[\\/]node_modules[\\/]/,
|
|
|
|
priority: 10,
|
|
|
|
chunks: 'initial' // 只打包初始时依赖的第三方
|
|
|
|
},
|
|
|
|
elementUI: {
|
|
|
|
name: 'chunk-elementUI', // 单独将 elementUI 拆包
|
|
|
|
priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
|
|
|
|
test: /[\\/]node_modules[\\/]element-ui[\\/]/
|
|
|
|
},
|
|
|
|
commons: {
|
2018-09-03 07:03:00 +00:00
|
|
|
name: 'chunk-commons',
|
2018-08-15 07:33:34 +00:00
|
|
|
test: resolve('src/components'), // 可自定义拓展你的规则
|
|
|
|
minChunks: 3, // 最小公用次数
|
|
|
|
priority: 5,
|
|
|
|
reuseExistingChunk: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
runtimeChunk: 'single',
|
|
|
|
minimizer: [
|
|
|
|
new UglifyJsPlugin({
|
|
|
|
uglifyOptions: {
|
|
|
|
mangle: {
|
|
|
|
safari10: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sourceMap: config.build.productionSourceMap,
|
|
|
|
cache: true,
|
|
|
|
parallel: true
|
|
|
|
}),
|
|
|
|
// Compress extracted CSS. We are using this plugin so that possible
|
|
|
|
// duplicated CSS from different components can be deduped.
|
2018-10-23 01:51:57 +00:00
|
|
|
new OptimizeCSSAssetsPlugin()
|
2018-08-15 07:33:34 +00:00
|
|
|
]
|
|
|
|
}
|
2017-04-18 07:09:13 +00:00
|
|
|
})
|
2017-09-29 05:39:42 +00:00
|
|
|
|
2017-12-15 06:07:55 +00:00
|
|
|
if (config.build.productionGzip) {
|
|
|
|
const CompressionWebpackPlugin = require('compression-webpack-plugin')
|
|
|
|
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new CompressionWebpackPlugin({
|
|
|
|
asset: '[path].gz[query]',
|
|
|
|
algorithm: 'gzip',
|
|
|
|
test: new RegExp(
|
2018-08-15 07:33:34 +00:00
|
|
|
'\\.(' + config.build.productionGzipExtensions.join('|') + ')$'
|
2017-12-15 06:07:55 +00:00
|
|
|
),
|
|
|
|
threshold: 10240,
|
|
|
|
minRatio: 0.8
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-15 07:33:34 +00:00
|
|
|
if (config.build.generateAnalyzerReport || config.build.bundleAnalyzerReport) {
|
|
|
|
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
|
|
|
|
.BundleAnalyzerPlugin
|
|
|
|
|
|
|
|
if (config.build.bundleAnalyzerReport) {
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new BundleAnalyzerPlugin({
|
|
|
|
analyzerPort: 8080,
|
|
|
|
generateStatsFile: false
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.build.generateAnalyzerReport) {
|
|
|
|
webpackConfig.plugins.push(
|
|
|
|
new BundleAnalyzerPlugin({
|
|
|
|
analyzerMode: 'static',
|
|
|
|
reportFilename: 'bundle-report.html',
|
|
|
|
openAnalyzer: false
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2017-04-18 07:09:13 +00:00
|
|
|
}
|
2017-09-29 05:39:42 +00:00
|
|
|
|
2017-04-18 07:09:13 +00:00
|
|
|
module.exports = webpackConfig
|