webpack.prod.conf.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. var env = process.env.NODE_ENV === 'production' ? config.build.prodEnv : config.build.sitEnv
  12. function resolveApp(relativePath) {
  13. return path.resolve(relativePath);
  14. }
  15. var webpackConfig = merge(baseWebpackConfig, {
  16. module: {
  17. rules: utils.styleLoaders({
  18. sourceMap: config.build.productionSourceMap,
  19. extract: true
  20. })
  21. },
  22. devtool: config.build.productionSourceMap ? '#source-map' : false,
  23. output: {
  24. path: config.build.assetsRoot,
  25. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  26. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js'),
  27. publicPath: config.build.assetsPublicPath
  28. },
  29. plugins: [
  30. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  31. new webpack.DefinePlugin({
  32. 'process.env': env
  33. }),
  34. new webpack.optimize.UglifyJsPlugin({
  35. compress: {
  36. warnings: false
  37. },
  38. sourceMap: true
  39. }),
  40. // extract css into its own file
  41. new ExtractTextPlugin({
  42. filename: utils.assetsPath('css/[name].[contenthash].css')
  43. }),
  44. // Compress extracted CSS. We are using this plugin so that possible
  45. // duplicated CSS from different components can be deduped.
  46. new OptimizeCSSPlugin(),
  47. // generate dist index.html with correct asset hash for caching.
  48. // you can customize output by editing /index.html
  49. // see https://github.com/ampedandwired/html-webpack-plugin
  50. new HtmlWebpackPlugin({
  51. filename: 'index.html',
  52. template: 'index.html',
  53. inject: true,
  54. favicon: resolveApp('favicon.ico'),
  55. minify: {
  56. removeComments: true,
  57. collapseWhitespace: true,
  58. removeRedundantAttributes: true,
  59. useShortDoctype: true,
  60. removeEmptyAttributes: true,
  61. removeStyleLinkTypeAttributes: true,
  62. keepClosingSlash: true,
  63. minifyJS: true,
  64. minifyCSS: true,
  65. minifyURLs: true
  66. },
  67. path: config.build.assetsPublicPath + config.build.assetsSubDirectory,
  68. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  69. chunksSortMode: 'dependency'
  70. }),
  71. // cache Module Identifiers
  72. new webpack.HashedModuleIdsPlugin(),
  73. // split vendor js into its own file
  74. new webpack.optimize.CommonsChunkPlugin({
  75. name: 'vendor',
  76. minChunks: function (module, count) {
  77. // any required modules inside node_modules are extracted to vendor
  78. return (
  79. module.resource &&
  80. /\.js$/.test(module.resource) &&
  81. module.resource.indexOf(
  82. path.join(__dirname, '../node_modules')
  83. ) === 0
  84. )
  85. }
  86. }),
  87. // split echarts into its own file
  88. new webpack.optimize.CommonsChunkPlugin({
  89. async: 'echarts',
  90. minChunks(module) {
  91. var context = module.context;
  92. return context && (context.indexOf('echarts') >= 0 || context.indexOf('zrender') >= 0);
  93. }
  94. }),
  95. // extract webpack runtime and module manifest to its own file in order to
  96. // prevent vendor hash from being updated whenever app bundle is updated
  97. new webpack.optimize.CommonsChunkPlugin({
  98. name: 'manifest',
  99. chunks: ['vendor']
  100. }),
  101. // copy custom static assets
  102. new CopyWebpackPlugin([{
  103. from: path.resolve(__dirname, '../static'),
  104. to: config.build.assetsSubDirectory,
  105. ignore: ['.*']
  106. }]),
  107. new webpack.ProvidePlugin({
  108. $: 'jquery',
  109. 'jQuery': 'jquery'
  110. })
  111. ]
  112. })
  113. if (config.build.bundleAnalyzerReport) {
  114. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  115. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  116. }
  117. module.exports = webpackConfig