webpack.prod.conf.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  8. var CopyWebpackPlugin = require('copy-webpack-plugin')
  9. // var HtmlWebpackPlugin = require('html-webpack-plugin')
  10. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. var MpvueVendorPlugin = require('webpack-mpvue-vendor-plugin')
  13. var env = config.build.env
  14. var webpackConfig = merge(baseWebpackConfig, {
  15. module: {
  16. rules: utils.styleLoaders({
  17. sourceMap: config.build.productionSourceMap,
  18. extract: true
  19. })
  20. },
  21. devtool: config.build.productionSourceMap ? '#source-map' : false,
  22. output: {
  23. path: config.build.assetsRoot,
  24. // filename: utils.assetsPath('[name].[chunkhash].js'),
  25. // chunkFilename: utils.assetsPath('[id].[chunkhash].js')
  26. filename: utils.assetsPath('[name].js'),
  27. chunkFilename: utils.assetsPath('[id].js')
  28. },
  29. plugins: [
  30. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  31. new webpack.DefinePlugin({
  32. 'process.env': env
  33. }),
  34. // extract css into its own file
  35. new ExtractTextPlugin({
  36. // filename: utils.assetsPath('[name].[contenthash].css')
  37. filename: utils.assetsPath(`[name].${config.build.fileExt.style}`)
  38. }),
  39. // Compress extracted CSS. We are using this plugin so that possible
  40. // duplicated CSS from different components can be deduped.
  41. new OptimizeCSSPlugin({
  42. cssProcessorOptions: {
  43. safe: true
  44. }
  45. }),
  46. // generate dist index.html with correct asset hash for caching.
  47. // you can customize output by editing /index.html
  48. // see https://github.com/ampedandwired/html-webpack-plugin
  49. // new HtmlWebpackPlugin({
  50. // filename: config.build.index,
  51. // template: 'index.html',
  52. // inject: true,
  53. // minify: {
  54. // removeComments: true,
  55. // collapseWhitespace: true,
  56. // removeAttributeQuotes: true
  57. // // more options:
  58. // // https://github.com/kangax/html-minifier#options-quick-reference
  59. // },
  60. // // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  61. // chunksSortMode: 'dependency'
  62. // }),
  63. // keep module.id stable when vender modules does not change
  64. new webpack.HashedModuleIdsPlugin(),
  65. // split vendor js into its own file
  66. new webpack.optimize.CommonsChunkPlugin({
  67. name: 'common/vendor',
  68. minChunks: function (module, count) {
  69. // any required modules inside node_modules are extracted to vendor
  70. return (
  71. module.resource &&
  72. /\.js$/.test(module.resource) &&
  73. module.resource.indexOf('node_modules') >= 0
  74. ) || count > 1
  75. }
  76. }),
  77. // extract webpack runtime and module manifest to its own file in order to
  78. // prevent vendor hash from being updated whenever app bundle is updated
  79. new webpack.optimize.CommonsChunkPlugin({
  80. name: 'common/manifest',
  81. chunks: ['common/vendor']
  82. }),
  83. new MpvueVendorPlugin({
  84. platform: process.env.PLATFORM
  85. })
  86. ]
  87. })
  88. // if (config.build.productionGzip) {
  89. // var CompressionWebpackPlugin = require('compression-webpack-plugin')
  90. // webpackConfig.plugins.push(
  91. // new CompressionWebpackPlugin({
  92. // asset: '[path].gz[query]',
  93. // algorithm: 'gzip',
  94. // test: new RegExp(
  95. // '\\.(' +
  96. // config.build.productionGzipExtensions.join('|') +
  97. // ')$'
  98. // ),
  99. // threshold: 10240,
  100. // minRatio: 0.8
  101. // })
  102. // )
  103. // }
  104. if (config.build.bundleAnalyzerReport) {
  105. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  106. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  107. }
  108. var useUglifyJs = process.env.PLATFORM !== 'swan'
  109. if (useUglifyJs) {
  110. webpackConfig.plugins.push(new UglifyJsPlugin({
  111. sourceMap: true
  112. }))
  113. }
  114. module.exports = webpackConfig