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