修改过页面
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.8 KiB

1 year ago
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const env = process.env.NODE_ENV === 'testing'
  13. ? require('../config/test.env')
  14. : require('../config/prod.env')
  15. const webpackConfig = merge(baseWebpackConfig, {
  16. module: {
  17. rules: utils.styleLoaders({
  18. sourceMap: config.build.productionSourceMap,
  19. extract: true,
  20. usePostCSS: true
  21. })
  22. },
  23. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  24. output: {
  25. publicPath: '/',
  26. path: config.build.assetsRoot,
  27. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  28. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  29. },
  30. plugins: [
  31. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  32. new webpack.DefinePlugin({
  33. 'process.env': env
  34. }),
  35. // extract css into its own file
  36. new ExtractTextPlugin({
  37. filename: utils.assetsPath('css/[name].[contenthash].css'),
  38. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  39. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  40. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  41. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  42. allChunks: true,
  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: config.build.productionSourceMap
  48. ? { safe: true, map: { inline: false } }
  49. : { safe: true }
  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: process.env.NODE_ENV === 'testing'
  56. ? 'index.html'
  57. : config.build.index,
  58. template: 'index.html',
  59. inject: true,
  60. minify: {
  61. removeComments: true,
  62. collapseWhitespace: true,
  63. removeAttributeQuotes: true
  64. // more options:
  65. // https://github.com/kangax/html-minifier#options-quick-reference
  66. },
  67. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  68. chunksSortMode: 'dependency'
  69. }),
  70. // keep module.id stable when vendor modules does not change
  71. new webpack.HashedModuleIdsPlugin(),
  72. // enable scope hoisting
  73. new webpack.optimize.ModuleConcatenationPlugin(),
  74. // split vendor js into its own file
  75. new webpack.optimize.CommonsChunkPlugin({
  76. name: 'vendor',
  77. minChunks (module) {
  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. minChunks: Infinity
  93. }),
  94. // This instance extracts shared chunks from code splitted chunks and bundles them
  95. // in a separate chunk, similar to the vendor chunk
  96. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  97. new webpack.optimize.CommonsChunkPlugin({
  98. name: 'app',
  99. async: 'vendor-async',
  100. children: true,
  101. minChunks: 3
  102. }),
  103. // copy custom static assets
  104. new CopyWebpackPlugin([
  105. {
  106. from: path.resolve(__dirname, '../static'),
  107. to: config.build.assetsSubDirectory,
  108. ignore: ['.*']
  109. }
  110. ])
  111. ]
  112. })
  113. if (config.build.productionGzip) {
  114. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  115. webpackConfig.plugins.push(
  116. new CompressionWebpackPlugin({
  117. asset: '[path].gz[query]',
  118. algorithm: 'gzip',
  119. test: new RegExp(
  120. '\\.(' +
  121. config.build.productionGzipExtensions.join('|') +
  122. ')$'
  123. ),
  124. threshold: 10240,
  125. minRatio: 0.8
  126. })
  127. )
  128. }
  129. if (config.build.bundleAnalyzerReport) {
  130. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  131. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  132. }
  133. module.exports = webpackConfig