webpack.prod.conf.en.js 5.5 KB

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