webpack.prod.conf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. const utils = require('./utils')
  5. const webpack = require('webpack')
  6. const config = require('../config')
  7. const merge = require('webpack-merge')
  8. const baseWebpackConfig = require('./webpack.base.conf')
  9. const CopyWebpackPlugin = require('copy-webpack-plugin')
  10. const HtmlWebpackPlugin = require('html-webpack-plugin')
  11. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  12. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  13. const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin')
  14. const loadMinified = require('./load-minified')
  15. const env = process.env.NODE_ENV === 'testing'
  16. ? require('../config/test.env')
  17. : config.build.env
  18. const webpackConfig = merge(baseWebpackConfig, {
  19. module: {
  20. rules: utils.styleLoaders({
  21. sourceMap: config.build.productionSourceMap,
  22. extract: true
  23. })
  24. },
  25. devtool: config.build.productionSourceMap ? '#source-map' : false,
  26. output: {
  27. path: config.build.assetsRoot,
  28. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  29. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  30. },
  31. plugins: [
  32. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  33. new webpack.DefinePlugin({
  34. 'process.env': env
  35. }),
  36. new webpack.optimize.UglifyJsPlugin({
  37. compress: {
  38. warnings: false
  39. },
  40. sourceMap: true
  41. }),
  42. // extract css into its own file
  43. new ExtractTextPlugin({
  44. filename: utils.assetsPath('css/[name].[contenthash].css')
  45. }),
  46. // Compress extracted CSS. We are using this plugin so that possible
  47. // duplicated CSS from different components can be deduped.
  48. new OptimizeCSSPlugin({
  49. cssProcessorOptions: {
  50. safe: true
  51. }
  52. }),
  53. // generate dist index.html with correct asset hash for caching.
  54. // you can customize output by editing /index.html
  55. // see https://github.com/ampedandwired/html-webpack-plugin
  56. new HtmlWebpackPlugin({
  57. filename: process.env.NODE_ENV === 'testing'
  58. ? 'index.html'
  59. : config.build.index,
  60. template: 'index.html',
  61. inject: true,
  62. minify: {
  63. removeComments: true,
  64. collapseWhitespace: true,
  65. removeAttributeQuotes: true
  66. // more options:
  67. // https://github.com/kangax/html-minifier#options-quick-reference
  68. },
  69. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  70. chunksSortMode: 'dependency',
  71. serviceWorkerLoader: `<script>${loadMinified(path.join(__dirname,
  72. './service-worker-prod.js'))}</script>`
  73. }),
  74. // split vendor js into its own file
  75. new webpack.optimize.CommonsChunkPlugin({
  76. name: 'vendor',
  77. minChunks: function (module, count) {
  78. // any required modules inside node_modules are extracted to vendor
  79. return (
  80. module.resource &&
  81. /\.js$/.test(module.resource) &&
  82. module.resource.indexOf(
  83. path.join(__dirname, '../node_modules')
  84. ) === 0
  85. )
  86. }
  87. }),
  88. // extract webpack runtime and module manifest to its own file in order to
  89. // prevent vendor hash from being updated whenever app bundle is updated
  90. new webpack.optimize.CommonsChunkPlugin({
  91. name: 'manifest',
  92. chunks: ['vendor']
  93. }),
  94. // copy custom static assets
  95. new CopyWebpackPlugin([
  96. {
  97. from: path.resolve(__dirname, '../static'),
  98. to: config.build.assetsSubDirectory,
  99. ignore: ['.*']
  100. }
  101. ]),
  102. // service worker caching
  103. new SWPrecacheWebpackPlugin({
  104. cacheId: 'spsemulator',
  105. filename: 'service-worker.js',
  106. staticFileGlobs: ['dist/**/*.{js,html,css}'],
  107. minify: true,
  108. stripPrefix: 'dist/'
  109. })
  110. ]
  111. })
  112. if (config.build.productionGzip) {
  113. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  114. webpackConfig.plugins.push(
  115. new CompressionWebpackPlugin({
  116. asset: '[path].gz[query]',
  117. algorithm: 'gzip',
  118. test: new RegExp(
  119. '\\.(' +
  120. config.build.productionGzipExtensions.join('|') +
  121. ')$'
  122. ),
  123. threshold: 10240,
  124. minRatio: 0.8
  125. })
  126. )
  127. }
  128. if (config.build.bundleAnalyzerReport) {
  129. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  130. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  131. }
  132. module.exports = webpackConfig