Path: blob/develop/web/build/webpack.prod.conf.js
387 views
'use strict'1const path = require('path')2const utils = require('./utils')3const webpack = require('webpack')4const config = require('../config')5const merge = require('webpack-merge')6const baseWebpackConfig = require('./webpack.base.conf')7const CopyWebpackPlugin = require('copy-webpack-plugin')8const HtmlWebpackPlugin = require('html-webpack-plugin')9const ExtractTextPlugin = require('extract-text-webpack-plugin')10const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')11const UglifyJsPlugin = require('uglifyjs-webpack-plugin')1213const env = process.env.NODE_ENV === 'testing'14? require('../config/test.env')15: require('../config/prod.env')1617const webpackConfig = merge(baseWebpackConfig, {18module: {19rules: utils.styleLoaders({20sourceMap: config.build.productionSourceMap,21extract: true,22usePostCSS: true23})24},25devtool: config.build.productionSourceMap ? config.build.devtool : false,26output: {27path: config.build.assetsRoot,28filename: utils.assetsPath('js/[name].[chunkhash].js'),29chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')30},31plugins: [32// http://vuejs.github.io/vue-loader/en/workflow/production.html33new webpack.DefinePlugin({34'process.env': env35}),36new UglifyJsPlugin({37uglifyOptions: {38compress: {39warnings: false40}41},42sourceMap: config.build.productionSourceMap,43parallel: true44}),45// extract css into its own file46new ExtractTextPlugin({47filename: utils.assetsPath('css/[name].[contenthash].css'),48// Setting the following option to `false` will not extract CSS from codesplit chunks.49// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.50// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,51// increasing file size: https://github.com/vuejs-templates/webpack/issues/111052allChunks: true,53}),54// Compress extracted CSS. We are using this plugin so that possible55// duplicated CSS from different components can be deduped.56new OptimizeCSSPlugin({57cssProcessorOptions: config.build.productionSourceMap58? { safe: true, map: { inline: false } }59: { safe: true }60}),61// generate dist index.html with correct asset hash for caching.62// you can customize output by editing /index.html63// see https://github.com/ampedandwired/html-webpack-plugin64new HtmlWebpackPlugin({65filename: process.env.NODE_ENV === 'testing'66? 'index.html'67: config.build.index,68template: 'index.html',69inject: true,70minify: {71removeComments: true,72collapseWhitespace: true,73removeAttributeQuotes: true74// more options:75// https://github.com/kangax/html-minifier#options-quick-reference76},77// necessary to consistently work with multiple chunks via CommonsChunkPlugin78chunksSortMode: 'dependency'79}),80// keep module.id stable when vendor modules does not change81new webpack.HashedModuleIdsPlugin(),82// enable scope hoisting83new webpack.optimize.ModuleConcatenationPlugin(),84// split vendor js into its own file85new webpack.optimize.CommonsChunkPlugin({86name: 'vendor',87minChunks (module) {88// any required modules inside node_modules are extracted to vendor89return (90module.resource &&91/\.js$/.test(module.resource) &&92module.resource.indexOf(93path.join(__dirname, '../node_modules')94) === 095)96}97}),98// extract webpack runtime and module manifest to its own file in order to99// prevent vendor hash from being updated whenever app bundle is updated100new webpack.optimize.CommonsChunkPlugin({101name: 'manifest',102minChunks: Infinity103}),104// This instance extracts shared chunks from code splitted chunks and bundles them105// in a separate chunk, similar to the vendor chunk106// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk107new webpack.optimize.CommonsChunkPlugin({108name: 'app',109async: 'vendor-async',110children: true,111minChunks: 3112}),113114// copy custom static assets115new CopyWebpackPlugin([116{117from: path.resolve(__dirname, '../static'),118to: config.build.assetsSubDirectory,119ignore: ['.*']120}121])122]123})124125if (config.build.productionGzip) {126const CompressionWebpackPlugin = require('compression-webpack-plugin')127128webpackConfig.plugins.push(129new CompressionWebpackPlugin({130asset: '[path].gz[query]',131algorithm: 'gzip',132test: new RegExp(133'\\.(' +134config.build.productionGzipExtensions.join('|') +135')$'136),137threshold: 10240,138minRatio: 0.8139})140)141}142143if (config.build.bundleAnalyzerReport) {144const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin145webpackConfig.plugins.push(new BundleAnalyzerPlugin())146}147148module.exports = webpackConfig149150151