fe-drone-ci/build/webpack.prod.conf.js

187 lines
5.6 KiB
JavaScript
Raw Normal View History

'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')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
2018-07-27 06:29:55 +00:00
const crypto = require('crypto');
2017-04-18 07:09:13 +00:00
2018-07-19 10:02:48 +00:00
function resolve(dir) {
return path.join(__dirname, '..', dir)
2017-04-18 07:09:13 +00:00
}
2018-07-27 06:29:55 +00:00
function createHash(data) {
const length = 8
const hash = crypto.createHash('md5').update(data).digest("hex")
return hash.slice(0, length)
}
2018-07-19 10:02:48 +00:00
const env = require('../config/' + process.env.env_config + '.env')
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
})
},
devtool: config.build.productionSourceMap ? config.build.devtool : false,
2017-08-07 08:11:10 +00:00
output: {
path: config.build.assetsRoot,
2018-07-27 06:29:55 +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({
'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-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({
filename: config.build.index,
2017-08-07 08:11:10 +00:00
template: 'index.html',
inject: true,
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,
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
}),
2018-07-27 06:29:55 +00:00
new webpack.NamedChunksPlugin(chunk => {
if (chunk.name) {
return chunk.name
}
return createHash(Array.from(chunk.modulesIterable, m => m.id).join('_'))
}),
// 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-25 10:22:08 +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[\\/]/
},
}
2018-07-19 10:02:48 +00:00
},
runtimeChunk: 'single',
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false
}
},
sourceMap: config.build.productionSourceMap,
2018-07-29 08:20:45 +00:00
cache: true,
2018-07-19 10:02:48 +00:00
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
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) {
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