25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

143 lines
4.8 KiB

  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 = require('../config/prod.env')
  13. const webpackConfig = merge(baseWebpackConfig, {
  14. module: {
  15. rules: utils.styleLoaders({
  16. sourceMap: config.build.productionSourceMap,
  17. extract: true,
  18. usePostCSS: true
  19. })
  20. },
  21. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  22. output: {
  23. path: config.build.assetsRoot,
  24. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  25. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  26. },
  27. plugins: [
  28. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  29. new webpack.DefinePlugin({
  30. 'process.env': env
  31. }),
  32. // UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
  33. new webpack.optimize.UglifyJsPlugin({
  34. compress: {
  35. warnings: false
  36. },
  37. sourceMap: config.build.productionSourceMap,
  38. parallel: true
  39. }),
  40. // extract css into its own file
  41. new ExtractTextPlugin({
  42. filename: utils.assetsPath('css/[name].[contenthash].css'),
  43. // set the following option to `true` if you want to extract CSS from
  44. // codesplit chunks into this main css file as well.
  45. // This will result in *all* of your app's CSS being loaded upfront.
  46. allChunks: false,
  47. }),
  48. // Compress extracted CSS. We are using this plugin so that possible
  49. // duplicated CSS from different components can be deduped.
  50. new OptimizeCSSPlugin({
  51. cssProcessorOptions: config.build.productionSourceMap
  52. ? { safe: true, map: { inline: false } }
  53. : { safe: true }
  54. }),
  55. // generate dist index.html with correct asset hash for caching.
  56. // you can customize output by editing /index.html
  57. // see https://github.com/ampedandwired/html-webpack-plugin
  58. new HtmlWebpackPlugin({
  59. filename: 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. }),
  72. // keep module.id stable when vender modules does not change
  73. new webpack.HashedModuleIdsPlugin(),
  74. // enable scope hoisting
  75. new webpack.optimize.ModuleConcatenationPlugin(),
  76. // split vendor js into its own file
  77. new webpack.optimize.CommonsChunkPlugin({
  78. name: 'vendor',
  79. minChunks: function (module) {
  80. // any required modules inside node_modules are extracted to vendor
  81. return (
  82. module.resource &&
  83. /\.js$/.test(module.resource) &&
  84. module.resource.indexOf(
  85. path.join(__dirname, '../node_modules')
  86. ) === 0
  87. )
  88. }
  89. }),
  90. // extract webpack runtime and module manifest to its own file in order to
  91. // prevent vendor hash from being updated whenever app bundle is updated
  92. new webpack.optimize.CommonsChunkPlugin({
  93. name: 'manifest',
  94. minChunks: Infinity
  95. }),
  96. // This instance extracts shared chunks from code splitted chunks and bundles them
  97. // in a separate chunk, similar to the vendor chunk
  98. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  99. new webpack.optimize.CommonsChunkPlugin({
  100. name: 'app',
  101. async: 'vendor-async',
  102. children: true,
  103. minChunks: 3
  104. }),
  105. // copy custom static assets
  106. new CopyWebpackPlugin([
  107. {
  108. from: path.resolve(__dirname, '../static'),
  109. to: config.build.assetsSubDirectory,
  110. ignore: ['.*']
  111. }
  112. ])
  113. ]
  114. })
  115. if (config.build.productionGzip) {
  116. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  117. webpackConfig.plugins.push(
  118. new CompressionWebpackPlugin({
  119. asset: '[path].gz[query]',
  120. algorithm: 'gzip',
  121. test: new RegExp(
  122. '\\.(' +
  123. config.build.productionGzipExtensions.join('|') +
  124. ')$'
  125. ),
  126. threshold: 10240,
  127. minRatio: 0.8
  128. })
  129. )
  130. }
  131. if (config.build.bundleAnalyzerReport) {
  132. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  133. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  134. }
  135. module.exports = webpackConfig