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-07-20 08:39:39 +00:00
|
|
|
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
|
2018-07-19 10:02:48 +00:00
|
|
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
|
|
|
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-07-19 10:02:48 +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-07-19 10:02:48 +00:00
|
|
|
const env = require('../config/' + process.env.env_config + '.env')
|
2017-12-15 06:07:55 +00:00
|
|
|
|
|
|
|
const webpackConfig = merge(baseWebpackConfig, {
|
2018-07-19 10:02:48 +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,
|
|
|
|
filename: utils.assetsPath('js/[name].[chunkhash].js'),
|
2018-07-19 10:02:48 +00:00
|
|
|
chunkFilename: utils.assetsPath('js/[name].[chunkhash].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-07-19 10:02:48 +00:00
|
|
|
new MiniCssExtractPlugin({
|
2018-07-23 08:41:30 +00:00
|
|
|
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',
|
|
|
|
path: 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-07-19 10:02:48 +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
|
|
|
|
chunksSortMode: 'none'
|
2017-08-07 08:11:10 +00:00
|
|
|
}),
|
2018-07-20 08:39:39 +00:00
|
|
|
new ScriptExtHtmlWebpackPlugin({
|
|
|
|
//`runtime` must same as runtimeChunk name. default is `runtime`
|
2018-07-24 03:40:58 +00:00
|
|
|
inline: /runtime\..*\.js$/
|
2018-07-20 08:39:39 +00:00
|
|
|
}),
|
2017-12-15 06:07:55 +00:00
|
|
|
// keep module.id stable when vender modules does not change
|
2017-08-07 08:11:10 +00:00
|
|
|
new webpack.HashedModuleIdsPlugin(),
|
|
|
|
// copy custom static assets
|
2018-07-19 10:02:48 +00:00
|
|
|
new CopyWebpackPlugin([{
|
|
|
|
from: path.resolve(__dirname, '../static'),
|
|
|
|
to: config.build.assetsSubDirectory,
|
|
|
|
ignore: ['.*']
|
|
|
|
}])
|
|
|
|
],
|
2018-07-23 09:30:26 +00:00
|
|
|
recordsPath: path.resolve('webpack-records.json'),
|
2018-07-19 10:02:48 +00:00
|
|
|
optimization: {
|
|
|
|
splitChunks: {
|
|
|
|
chunks: 'all',
|
|
|
|
cacheGroups: {
|
|
|
|
libs: {
|
|
|
|
name: 'libs',
|
|
|
|
test: /[\\/]node_modules[\\/]/,
|
|
|
|
priority: 10,
|
|
|
|
chunks: 'initial'
|
|
|
|
},
|
|
|
|
elementUI: {
|
|
|
|
name: 'chunk-elementUI',
|
|
|
|
priority: 20,
|
|
|
|
test: /[\\/]node_modules[\\/]element-ui[\\/]/
|
|
|
|
},
|
|
|
|
echarts: {
|
|
|
|
name: 'chunk-echarts',
|
|
|
|
priority: 20,
|
|
|
|
test: function (module) {
|
|
|
|
var context = module.context;
|
|
|
|
return context && (context.indexOf('echarts') >= 0 || context.indexOf('zrender') >= 0)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
xlsx: {
|
|
|
|
name: 'chunk-xlsx',
|
|
|
|
priority: 20,
|
|
|
|
test: /[\\/]node_modules[\\/]xlsx[\\/]/
|
|
|
|
},
|
|
|
|
codemirror: {
|
|
|
|
name: 'chunk-codemirror',
|
|
|
|
priority: 20,
|
|
|
|
test: /[\\/]node_modules[\\/]codemirror[\\/]/
|
|
|
|
},
|
2017-12-15 06:07:55 +00:00
|
|
|
}
|
2018-07-19 10:02:48 +00:00
|
|
|
},
|
|
|
|
runtimeChunk: 'single',
|
|
|
|
minimizer: [
|
|
|
|
new UglifyJsPlugin({
|
|
|
|
uglifyOptions: {
|
|
|
|
compress: {
|
|
|
|
warnings: false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sourceMap: config.build.productionSourceMap,
|
|
|
|
parallel: true
|
|
|
|
}),
|
|
|
|
// Compress extracted CSS. We are using this plugin so that possible
|
|
|
|
// duplicated CSS from different components can be deduped.
|
|
|
|
new OptimizeCSSAssetsPlugin()
|
|
|
|
],
|
|
|
|
}
|
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(
|
|
|
|
'\\.(' +
|
|
|
|
config.build.productionGzipExtensions.join('|') +
|
|
|
|
')$'
|
|
|
|
),
|
|
|
|
threshold: 10240,
|
|
|
|
minRatio: 0.8
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-07-24 03:40:58 +00:00
|
|
|
if (config.build.generateAnalyzerReport || config.build.bundleAnalyzerReport) {
|
2017-12-15 06:07:55 +00:00
|
|
|
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
|
2018-07-24 03:40:58 +00:00
|
|
|
|
|
|
|
if (config.build.bundleAnalyzerReport) {
|
|
|
|
webpackConfig.plugins.push(new BundleAnalyzerPlugin({
|
|
|
|
analyzerPort: 8080,
|
|
|
|
generateStatsFile: false
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.build.generateAnalyzerReport) {
|
|
|
|
webpackConfig.plugins.push(new BundleAnalyzerPlugin({
|
|
|
|
analyzerMode: 'static',
|
|
|
|
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
|