webpack.prod.test.conf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var path = require('path')
  2. var utils = require('./utils')
  3. var webpack = require('webpack')
  4. var config = require('../config')
  5. var merge = require('webpack-merge')
  6. var baseWebpackConfig = require('./webpack.base.conf')
  7. var CopyWebpackPlugin = require('copy-webpack-plugin')
  8. var HtmlWebpackPlugin = require('html-webpack-plugin')
  9. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  10. var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  11. const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
  12. var env = config.test.env
  13. var now_time = new Date().getTime()
  14. var webpackConfig = merge(baseWebpackConfig, {
  15. module: {
  16. rules: utils.styleLoaders({
  17. sourceMap: config.test.productionSourceMap,
  18. extract: true
  19. })
  20. },
  21. devtool: config.test.productionSourceMap ? '#source-map' : false,
  22. output: {
  23. path: config.test.assetsRoot,
  24. filename: utils.assetsPath('js/[name].[chunkhash].'+now_time+'.js'),
  25. chunkFilename: utils.assetsPath('js/[id].[chunkhash].'+now_time+'.js')
  26. },
  27. plugins: [
  28. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  29. new webpack.DefinePlugin({
  30. 'process.env': env
  31. }),
  32. // new webpack.optimize.UglifyJsPlugin({
  33. // compress: {
  34. // warnings: false,
  35. // drop_console:true
  36. // },
  37. // sourceMap: true
  38. // }),
  39. new ParallelUglifyPlugin({
  40. // 传递给 UglifyJS的参数如下:
  41. uglifyJS: {
  42. output: {
  43. beautify: false,
  44. /*
  45. 是否保留代码中的注释,默认为保留,为了达到更好的压缩效果,可以设置为false
  46. */
  47. comments: false
  48. },
  49. warnings: false,
  50. }
  51. }),
  52. // extract css into its own file
  53. new ExtractTextPlugin({
  54. filename: utils.assetsPath('css/[name].[contenthash].css')
  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: {
  60. safe: true
  61. }
  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.test.index,
  68. template: 'index.html',
  69. inject: true,
  70. minify: {
  71. removeComments: true,
  72. collapseWhitespace: true,
  73. removeAttributeQuotes: true
  74. // more options:
  75. // https://github.com/kangax/html-minifier#options-quick-reference
  76. },
  77. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  78. chunksSortMode: 'dependency'
  79. }),
  80. // split vendor js into its own file
  81. // new webpack.optimize.CommonsChunkPlugin({
  82. // name: 'vendor',
  83. // minChunks: function (module, count) {
  84. // // any required modules inside node_modules are extracted to vendor
  85. // return (
  86. // module.resource &&
  87. // /\.js$/.test(module.resource) &&
  88. // module.resource.indexOf(
  89. // path.join(__dirname, '../node_modules')
  90. // ) === 0
  91. // )
  92. // }
  93. // }),
  94. // extract webpack runtime and module manifest to its own file in order to
  95. // prevent vendor hash from being updated whenever app bundle is updated
  96. // new webpack.optimize.CommonsChunkPlugin({
  97. // name: 'manifest',
  98. // chunks: ['vendor']
  99. // }),
  100. // copy custom static assets
  101. new CopyWebpackPlugin([
  102. {
  103. from: path.resolve(__dirname, '../static'),
  104. to: config.test.assetsSubDirectory,
  105. ignore: ['.*']
  106. }
  107. ])
  108. ]
  109. })
  110. // if (config.test.productionGzip) {
  111. // var CompressionWebpackPlugin = require('compression-webpack-plugin')
  112. // webpackConfig.plugins.push(
  113. // new CompressionWebpackPlugin({
  114. // asset: '[path].gz[query]',
  115. // algorithm: 'gzip',
  116. // test: new RegExp(
  117. // '\\.(' +
  118. // config.test.productionGzipExtensions.join('|') +
  119. // ')$'
  120. // ),
  121. // threshold: 10240,
  122. // minRatio: 0.8
  123. // })
  124. // )
  125. // }
  126. if (config.test.bundleAnalyzerReport) {
  127. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  128. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  129. }
  130. module.exports = webpackConfig