webpack.prod.conf.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 version = new Date().getTime()
  12. var env = config.build.env
  13. var webpackConfig = merge(baseWebpackConfig, {
  14. module: {
  15. rules: utils.styleLoaders({
  16. sourceMap: config.build.productionSourceMap,
  17. extract: true
  18. })
  19. },
  20. devtool: config.build.productionSourceMap ? '#source-map' : false,
  21. output: {
  22. publicPath: './',
  23. path: config.build.assetsRoot,
  24. filename: utils.assetsPath(`js/[name].[chunkhash].${version}.js`),
  25. chunkFilename: utils.assetsPath(`js/[id].[chunkhash].${version}.js`)
  26. },
  27. plugins: [
  28. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  29. new webpack.DefinePlugin({
  30. 'process.env': env,
  31. 'BASE_URL' : '"/api"',
  32. 'LINK_URL' : '"/ips"'
  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].${version}.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. cssProcessorOptions: {
  48. safe: true
  49. }
  50. }),
  51. // generate dist index.html with correct asset hash for caching.
  52. // you can customize output by editing /index.html
  53. // see https://github.com/ampedandwired/html-webpack-plugin
  54. new HtmlWebpackPlugin({
  55. filename: config.build.index,
  56. template: 'index.html',
  57. inject: true,
  58. favicon: './favicon.ico',
  59. minify: {
  60. removeComments: true,
  61. collapseWhitespace: true,
  62. removeAttributeQuotes: true
  63. // more options:
  64. // https://github.com/kangax/html-minifier#options-quick-reference
  65. },
  66. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  67. chunksSortMode: 'dependency'
  68. }),
  69. // split vendor js into its own file
  70. new webpack.optimize.CommonsChunkPlugin({
  71. name: 'vendor',
  72. minChunks: function (module, count) {
  73. // any required modules inside node_modules are extracted to vendor
  74. return (
  75. module.resource &&
  76. /\.js$/.test(module.resource) &&
  77. module.resource.indexOf(
  78. path.join(__dirname, '../node_modules')
  79. ) === 0
  80. )
  81. }
  82. }),
  83. // extract webpack runtime and module manifest to its own file in order to
  84. // prevent vendor hash from being updated whenever app bundle is updated
  85. new webpack.optimize.CommonsChunkPlugin({
  86. name: 'manifest',
  87. chunks: ['vendor']
  88. }),
  89. // copy custom static assets
  90. new CopyWebpackPlugin([
  91. {
  92. from: path.resolve(__dirname, '../static'),
  93. to: config.build.assetsSubDirectory,
  94. ignore: ['.*']
  95. }
  96. ])
  97. ]
  98. })
  99. if (config.build.productionGzip) {
  100. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  101. webpackConfig.plugins.push(
  102. new CompressionWebpackPlugin({
  103. asset: '[path].gz[query]',
  104. algorithm: 'gzip',
  105. test: new RegExp(
  106. '\\.(' +
  107. config.build.productionGzipExtensions.join('|') +
  108. ')$'
  109. ),
  110. threshold: 10240,
  111. minRatio: 0.8
  112. })
  113. )
  114. }
  115. if (config.build.bundleAnalyzerReport) {
  116. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  117. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  118. }
  119. module.exports = webpackConfig