blob: 7541838e741d7b3ce8a650f6320d116e86937dd3 [file] [log] [blame]
huibing.xie1f1606f2018-08-20 15:46:55 +08001'use strict'
2const path = require('path')
3const utils = require('./utils')
4const webpack = require('webpack')
5const config = require('../config')
6const merge = require('webpack-merge')
7const baseWebpackConfig = require('./webpack.base.conf')
8const CopyWebpackPlugin = require('copy-webpack-plugin')
9const HtmlWebpackPlugin = require('html-webpack-plugin')
10const ExtractTextPlugin = require('extract-text-webpack-plugin')
11const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
12const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
13
14function resolve (dir) {
15 return path.join(__dirname, '..', dir)
16}
17
18const env = require('../config/prod.env')
19
20const webpackConfig = merge(baseWebpackConfig, {
21 module: {
22 rules: utils.styleLoaders({
23 sourceMap: config.build.productionSourceMap,
24 extract: true,
25 usePostCSS: true
26 })
27 },
28 devtool: config.build.productionSourceMap ? config.build.devtool : false,
29 output: {
30 path: config.build.assetsRoot,
31 filename: utils.assetsPath('js/[name].[chunkhash].js'),
32 chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
33 },
34 plugins: [
35 // http://vuejs.github.io/vue-loader/en/workflow/production.html
36 new webpack.DefinePlugin({
37 'process.env': env
38 }),
39 new UglifyJsPlugin({
40 uglifyOptions: {
41 compress: {
42 warnings: false
43 }
44 },
45 sourceMap: config.build.productionSourceMap,
46 parallel: true
47 }),
48 // extract css into its own file
49 new ExtractTextPlugin({
50 filename: utils.assetsPath('css/[name].[contenthash].css'),
51 // Setting the following option to `false` will not extract CSS from codesplit chunks.
52 // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
53 // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
54 allChunks: false,
55 }),
56 // Compress extracted CSS. We are using this plugin so that possible
57 // duplicated CSS from different components can be deduped.
58 new OptimizeCSSPlugin({
59 cssProcessorOptions: config.build.productionSourceMap
60 ? { safe: true, map: { inline: false } }
61 : { safe: true }
62 }),
63 // generate dist index.html with correct asset hash for caching.
64 // you can customize output by editing /index.html
65 // see https://github.com/ampedandwired/html-webpack-plugin
66 new HtmlWebpackPlugin({
67 filename: config.build.index,
68 template: 'index.html',
69 inject: true,
70 favicon: resolve('favicon.ico'),
71 title: 'vue-element-admin',
72 minify: {
73 removeComments: true,
74 collapseWhitespace: true,
75 removeAttributeQuotes: true
76 // more options:
77 // https://github.com/kangax/html-minifier#options-quick-reference
78 },
79 // necessary to consistently work with multiple chunks via CommonsChunkPlugin
80 chunksSortMode: 'dependency'
81 }),
82 // keep module.id stable when vender modules does not change
83 new webpack.HashedModuleIdsPlugin(),
84 // enable scope hoisting
85 new webpack.optimize.ModuleConcatenationPlugin(),
86 // split vendor js into its own file
87 new webpack.optimize.CommonsChunkPlugin({
88 name: 'vendor',
89 minChunks (module) {
90 // any required modules inside node_modules are extracted to vendor
91 return (
92 module.resource &&
93 /\.js$/.test(module.resource) &&
94 module.resource.indexOf(
95 path.join(__dirname, '../node_modules')
96 ) === 0
97 )
98 }
99 }),
100 // extract webpack runtime and module manifest to its own file in order to
101 // prevent vendor hash from being updated whenever app bundle is updated
102 new webpack.optimize.CommonsChunkPlugin({
103 name: 'manifest',
104 minChunks: Infinity
105 }),
106 // This instance extracts shared chunks from code splitted chunks and bundles them
107 // in a separate chunk, similar to the vendor chunk
108 // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
109 new webpack.optimize.CommonsChunkPlugin({
110 name: 'app',
111 async: 'vendor-async',
112 children: true,
113 minChunks: 3
114 }),
115
116 // copy custom static assets
117 new CopyWebpackPlugin([
118 {
119 from: path.resolve(__dirname, '../static'),
120 to: config.build.assetsSubDirectory,
121 ignore: ['.*']
122 }
123 ])
124 ]
125})
126
127if (config.build.productionGzip) {
128 const CompressionWebpackPlugin = require('compression-webpack-plugin')
129
130 webpackConfig.plugins.push(
131 new CompressionWebpackPlugin({
132 asset: '[path].gz[query]',
133 algorithm: 'gzip',
134 test: new RegExp(
135 '\\.(' +
136 config.build.productionGzipExtensions.join('|') +
137 ')$'
138 ),
139 threshold: 10240,
140 minRatio: 0.8
141 })
142 )
143}
144
145if (config.build.bundleAnalyzerReport) {
146 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
147 webpackConfig.plugins.push(new BundleAnalyzerPlugin())
148}
149
150module.exports = webpackConfig