webpack.prod.conf.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 PreRenderSPAPlugin = require('prerender-spa-plugin');
  14. const Renderer = PreRenderSPAPlugin.PuppeteerRenderer;
  15. const routes = [ '/', '/'];
  16. const resolve = dir => path.join(__dirname, '..', dir);
  17. const env = process.env.NODE_ENV === 'testing'
  18. ? require('../config/test.env')
  19. : process.env.LANG_CODE === 'en' ? require('../config/prod.env.en') : require('../config/prod.env')
  20. const 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. new PreRenderSPAPlugin({
  36. staticDir: resolve('dist'),
  37. routes,
  38. renderer: new Renderer({
  39. inject: {},
  40. // 在 main.js 中 document.dispatchEvent(new Event('render-event')),两者的事件名称要对应上。
  41. //render-event的作用就是在render-event事件执行后执行preRender
  42. renderAfterDocumentEvent: 'render-event',
  43. //puppeteer参数,标签意思:完全信任在Chrome中打开的内容
  44. args: ['--no-sandbox', '--disable-setuid-sandbox']
  45. })
  46. }),
  47. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  48. new webpack.DefinePlugin({
  49. 'process.env': env
  50. }),
  51. new UglifyJsPlugin({
  52. uglifyOptions: {
  53. compress: {
  54. warnings: false,
  55. drop_debugger: true, // 打包去除 debugger
  56. drop_console: true // 打包去除 console
  57. }
  58. },
  59. sourceMap: config.build.productionSourceMap,
  60. parallel: true
  61. }),
  62. // extract css into its own file
  63. new ExtractTextPlugin({
  64. filename: utils.assetsPath('css/[name].[contenthash].css'),
  65. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  66. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  67. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  68. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  69. allChunks: true,
  70. }),
  71. // Compress extracted CSS. We are using this plugin so that possible
  72. // duplicated CSS from different components can be deduped.
  73. new OptimizeCSSPlugin({
  74. cssProcessorOptions: config.build.productionSourceMap
  75. ? { safe: true, map: { inline: false }, autoprefixer: false }
  76. : { safe: true, autoprefixer: false }
  77. }),
  78. // generate dist index.html with correct asset hash for caching.
  79. // you can customize output by editing /index.html
  80. // see https://github.com/ampedandwired/html-webpack-plugin
  81. new HtmlWebpackPlugin({
  82. // 自定义参数
  83. isEn: process.env.LANG_CODE === 'en',
  84. isCn: process.env.LANG_CODE !== 'en',
  85. filename: process.env.NODE_ENV === 'testing'
  86. ? 'index.html'
  87. : config.build.index,
  88. // filename: 'index.html',
  89. template: 'index.html',
  90. favicon: 'src/assets/img/login.ico',
  91. inject: true,
  92. minify: {
  93. removeComments: true,
  94. collapseWhitespace: true,
  95. removeAttributeQuotes: true
  96. // more options:
  97. // https://github.com/kangax/html-minifier#options-quick-reference
  98. },
  99. chunks: ['manifest', 'vendor', 'app'],
  100. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  101. chunksSortMode: 'dependency'
  102. }),
  103. // keep module.id stable when vendor modules does not change
  104. new webpack.HashedModuleIdsPlugin(),
  105. // enable scope hoisting
  106. new webpack.optimize.ModuleConcatenationPlugin(),
  107. // extract webpack runtime and module manifest to its own file in order to
  108. // prevent vendor hash from being updated whenever app bundle is updated
  109. new webpack.optimize.CommonsChunkPlugin({
  110. name: 'manifest',
  111. minChunks: Infinity
  112. }),
  113. // split vendor js into its own file
  114. new webpack.optimize.CommonsChunkPlugin({
  115. name: 'vendor',
  116. minChunks (module) {
  117. // any required modules inside node_modules are extracted to vendor
  118. return (
  119. module.resource &&
  120. /\.js$/.test(module.resource) &&
  121. module.resource.indexOf(
  122. path.join(__dirname, '../node_modules')
  123. ) === 0
  124. )
  125. }
  126. }),
  127. // This instance extracts shared chunks from code splitted chunks and bundles them
  128. // in a separate chunk, similar to the vendor chunk
  129. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  130. new webpack.optimize.CommonsChunkPlugin({
  131. name: 'app',
  132. async: 'vendor-async',
  133. children: true,
  134. minChunks: 3
  135. }),
  136. // copy custom static assets
  137. new CopyWebpackPlugin([
  138. {
  139. from: path.resolve(__dirname, '../static'),
  140. to: config.build.assetsSubDirectory,
  141. ignore: ['.*']
  142. }
  143. ])
  144. ]
  145. })
  146. if (config.build.productionGzip) {
  147. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  148. webpackConfig.plugins.push(
  149. new CompressionWebpackPlugin({
  150. asset: '[path].gz[query]',
  151. algorithm: 'gzip',
  152. test: new RegExp(
  153. '\\.(' +
  154. config.build.productionGzipExtensions.join('|') +
  155. ')$'
  156. ),
  157. threshold: 10240,
  158. minRatio: 0.8
  159. })
  160. )
  161. }
  162. if (config.build.bundleAnalyzerReport) {
  163. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  164. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  165. }
  166. module.exports = webpackConfig