webpack.prod.conf.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 ScriptExtHtmlWebpackPlugin = require("script-ext-html-webpack-plugin");
  11. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  12. const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  13. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  14. function resolve(dir) {
  15. return path.join(__dirname, "..", dir);
  16. }
  17. const env = require("../config/" + process.env.env_config + ".env");
  18. // const env = require("../config/prod.env");
  19. // For NamedChunksPlugin
  20. const seen = new Set();
  21. const nameLength = 4;
  22. const webpackConfig = merge(baseWebpackConfig, {
  23. mode: "production",
  24. module: {
  25. rules: utils.styleLoaders({
  26. sourceMap: config.build.productionSourceMap,
  27. extract: true,
  28. usePostCSS: true
  29. })
  30. },
  31. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  32. output: {
  33. path: config.build.assetsRoot,
  34. filename: utils.assetsPath("js/[name].[chunkhash:8].js"),
  35. chunkFilename: utils.assetsPath("js/[name].[chunkhash:8].js")
  36. },
  37. plugins: [
  38. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  39. new webpack.DefinePlugin({
  40. "process.env": env
  41. }),
  42. // extract css into its own file
  43. new MiniCssExtractPlugin({
  44. filename: utils.assetsPath("css/[name].[contenthash:8].css"),
  45. chunkFilename: utils.assetsPath("css/[name].[contenthash:8].css")
  46. }),
  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: config.build.index,
  52. template: "index.html",
  53. inject: true,
  54. favicon: resolve("favicon.ico"),
  55. title: "ui-jz-v4",
  56. templateParameters: {
  57. BASE_URL: config.build.assetsPublicPath + config.build.assetsSubDirectory
  58. },
  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. // default sort mode uses toposort which cannot handle cyclic deps
  67. // in certain cases, and in webpack 4, chunk order in HTML doesn't
  68. // matter anyway
  69. }),
  70. new ScriptExtHtmlWebpackPlugin({
  71. //`runtime` must same as runtimeChunk name. default is `runtime`
  72. inline: /runtime\..*\.js$/
  73. }),
  74. // keep chunk.id stable when chunk has no name
  75. new webpack.NamedChunksPlugin(chunk => {
  76. if (chunk.name) {
  77. return chunk.name;
  78. }
  79. const modules = Array.from(chunk.modulesIterable);
  80. if (modules.length > 1) {
  81. const hash = require("hash-sum");
  82. const joinedHash = hash(modules.map(m => m.id).join("_"));
  83. let len = nameLength;
  84. while (seen.has(joinedHash.substr(0, len))) len++;
  85. seen.add(joinedHash.substr(0, len));
  86. return `chunk-${joinedHash.substr(0, len)}`;
  87. } else {
  88. return modules[0].id;
  89. }
  90. }),
  91. // keep module.id stable when vender modules does not change
  92. new webpack.HashedModuleIdsPlugin(),
  93. // copy custom static assets
  94. new CopyWebpackPlugin([
  95. {
  96. from: path.resolve(__dirname, "../static"),
  97. to: config.build.assetsSubDirectory,
  98. ignore: [".*"]
  99. }
  100. ])
  101. ],
  102. optimization: {
  103. splitChunks: {
  104. chunks: "all",
  105. cacheGroups: {
  106. libs: {
  107. name: "chunk-libs",
  108. test: /[\\/]node_modules[\\/]/,
  109. priority: 10,
  110. chunks: "initial" // 只打包初始时依赖的第三方
  111. },
  112. elementUI: {
  113. name: "chunk-elementUI", // 单独将 elementUI 拆包
  114. priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
  115. test: /[\\/]node_modules[\\/]element-ui[\\/]/
  116. },
  117. commons: {
  118. name: "chunk-commons",
  119. test: resolve("src/components"), // 可自定义拓展你的规则
  120. minChunks: 3, // 最小公用次数
  121. priority: 5,
  122. reuseExistingChunk: true
  123. }
  124. }
  125. },
  126. runtimeChunk: "single",
  127. minimizer: [
  128. new UglifyJsPlugin({
  129. uglifyOptions: {
  130. mangle: {
  131. safari10: true
  132. }
  133. },
  134. sourceMap: config.build.productionSourceMap,
  135. cache: true,
  136. parallel: true
  137. }),
  138. // Compress extracted CSS. We are using this plugin so that possible
  139. // duplicated CSS from different components can be deduped.
  140. new OptimizeCSSAssetsPlugin()
  141. ]
  142. }
  143. });
  144. if (config.build.productionGzip) {
  145. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  146. webpackConfig.plugins.push(
  147. new CompressionWebpackPlugin({
  148. algorithm: "gzip",
  149. test: new RegExp("\\.(" + config.build.productionGzipExtensions.join("|") + ")$"),
  150. threshold: 10240,
  151. minRatio: 0.8
  152. })
  153. );
  154. }
  155. if (config.build.generateAnalyzerReport || config.build.bundleAnalyzerReport) {
  156. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
  157. if (config.build.bundleAnalyzerReport) {
  158. webpackConfig.plugins.push(
  159. new BundleAnalyzerPlugin({
  160. analyzerPort: 8080,
  161. generateStatsFile: false
  162. })
  163. );
  164. }
  165. if (config.build.generateAnalyzerReport) {
  166. webpackConfig.plugins.push(
  167. new BundleAnalyzerPlugin({
  168. analyzerMode: "static",
  169. reportFilename: "bundle-report.html",
  170. openAnalyzer: false
  171. })
  172. );
  173. }
  174. }
  175. module.exports = webpackConfig;