Webpack 3.X - 4.X 升级记录

朴灿烈づ我的快乐病毒、 2022-05-30 00:42 329阅读 0赞

Webpack 3.X - 4.X 升级记录

先升级 webpack-cli

首先:执行命令

  1. npm install webpack-cli -D
  2. 或者
  3. npm install -g yarn
  4. yarn add webpack-cli -D

启动服务出现的问题

问题1compilation.mainTemplate.applyPluginsWaterfall is not a function
示例图1
暂时解决方案:

  1. yarn add webpack-contrib/html-webpack-plugin -D

相关讨论解决方案,在这里


问题2:Use Chunks.groupsIterable and filter by instanceof Entrypoint instead

示例图2

解决方案: 去除,require(‘extract-text-webpack-plugin’)的引用


问题3webpack.optimize.CommonsChunkPlugin has been removed, please use config.optimization.splitChunks instead.

示例3

目前,4.0中已经删除CommonsChunkPlugin,替换成了splitChunks,这里有相关介绍内容

解决方案: 去除 new webpack.optimize.CommonsChunkPlugin,修改为

  1. optimization: { runtimeChunk: { name: "manifest" },
  2. splitChunks: { cacheGroups: { commons: { test: /[\\/]node_modules[\\/]/, name: "vendor", chunks: "all" }
  3. }
  4. }
  5. }

即可!

optimization参数介绍:

  1. optimization: {
  2. splitChunks: {
  3. chunks: "initial", // 必须三选一: "initial" | "all"(默认就是all) | "async"
  4. minSize: 0, // 最小尺寸,默认0
  5. minChunks: 1, // 最小 chunk ,默认1
  6. maxAsyncRequests: 1, // 最大异步请求数, 默认1
  7. maxInitialRequests: 1, // 最大初始化请求书,默认1
  8. name: () => {}, // 名称,此选项课接收 function
  9. cacheGroups: { // 这里开始设置缓存的 chunks
  10. priority: "0", // 缓存组优先级 false | object |
  11. vendor: { // key 为entry中定义的 入口名称
  12. chunks: "initial", // 必须三选一: "initial" | "all" | "async"(默认就是异步)
  13. test: /react|lodash/, // 正则规则验证,如果符合就提取 chunk
  14. name: "vendor", // 要缓存的 分隔出来的 chunk 名称
  15. minSize: 0,
  16. minChunks: 1,
  17. enforce: true,
  18. maxAsyncRequests: 1, // 最大异步请求数, 默认1
  19. maxInitialRequests: 1, // 最大初始化请求书,默认1
  20. reuseExistingChunk: true // 可设置是否重用该chunk(查看源码没有发现默认值)
  21. }
  22. }
  23. }
  24. },

==最后,optimization 使用相关内容在这里==

CommonsChunkPlugin

https://news.aotu.io/a/5a7b53d3d50eee0042c20c0c?utm_medium=lite02_web&utm_source=aotu_io


问题4:警告

示例4

警告提示,表示 在启动服务的时候没有指定mode

在 package.json 中加上--mode development或者--mode production即可,如下示例:

  1. "scripts": {
  2. "dev": "webpack --mode development",
  3. "build": "webpack --mode production"
  4. }

最后,贴上我的webpack.config.js配置:

  1. var webpack = require('webpack'),
  2. path = require('path'),
  3. HtmlWebpackPlugin = require('html-webpack-plugin'),
  4. //ExtractTextPlugin = require('extract-text-webpack-plugin'), 4.0弃用
  5. ip = require('ip'),
  6. CleanPlugin = require('clean-webpack-plugin'),
  7. c = require('./config/packConfig'),
  8. HashedModuleIdsPlugin = require('./config/HashedModuleIdsPlugin'),
  9. BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin,
  10. Visualizer = require('webpack-visualizer-plugin'),
  11. HappyPack = require('happypack'),
  12. CopyWebpackPlugin = require('copy-webpack-plugin'),
  13. CompressionPlugin = require('compression-webpack-plugin');
  14. const resolve = dir => {
  15. return path.join(__dirname, c.cf.entry.module, dir)
  16. }
  17. module.exports = {
  18. entry: {
  19. app: resolve("/components/app.js"),
  20. vendor: c.cf.entry.vendor,
  21. },
  22. output: {
  23. path: resolve(c.cf.output.path),
  24. publicPath: '',
  25. filename: "assets/js/[name]" + env + ".js",
  26. chunkFilename: "chunk/[name]" + env + ".js",
  27. library: 'library',
  28. libraryTarget: 'umd',
  29. umdNamedDefine: true
  30. },
  31. devServer: {
  32. inline: true,
  33. open: true,
  34. port: c.cf.server.port,
  35. compress: true,
  36. host: ip.address(),
  37. progress: true,
  38. historyApiFallback: true,
  39. contentBase: "./",
  40. https: false,
  41. proxy: c.prox
  42. },
  43. //4.0配置
  44. optimization: {
  45. /*splitChunks: {
  46. chunks: 'all',//"initial" | "async" | "all"
  47. cacheGroups: {
  48. default: false,
  49. vendors: false,
  50. },
  51. },*/
  52. /*splitChunks: {
  53. cacheGroups: {
  54. commons: {
  55. test: /[\\/]node_modules[\\/]/,
  56. name: "vendor",
  57. chunks: "all"
  58. }
  59. }
  60. }*/
  61. runtimeChunk: {
  62. name: "manifest"
  63. },
  64. splitChunks: {
  65. cacheGroups: {
  66. commons: {
  67. test: /[\\/]node_modules[\\/]/,
  68. name: "vendor",
  69. chunks: "all"
  70. }
  71. }
  72. }
  73. },
  74. resolveLoader: {
  75. moduleExtensions: ['-loader']
  76. },
  77. module: {
  78. //4.0之前是 loaders,现在修改为 rules
  79. rules: [{
  80. test: /\.js$/,
  81. exclude: /node_modules/,
  82. loader: 'babel',
  83. }, {
  84. test: /\.vue$/,
  85. exclude: /node_modules/,
  86. loader: 'vue',
  87. }, {
  88. test: /\.(css|less)$/,
  89. exclude: /node_modules/,
  90. loader: "style!css!less"
  91. }, {
  92. test: /\.(html|tpl)$/,
  93. exclude: /node_modules/,
  94. loader: 'html'
  95. }, {
  96. test: /\.jsx$/,
  97. exclude: /node_modules/,
  98. loaders: ['jsx', 'babel']
  99. }],
  100. noParse: /jquery|lodash/,
  101. unknownContextRegExp: /$^/,
  102. unknownContextCritical: false,
  103. exprContextRegExp: /$^/,
  104. exprContextCritical: false,
  105. wrappedContextRegExp: /$^/,
  106. wrappedContextCritical: false,
  107. },
  108. plugins: [
  109. new HtmlWebpackPlugin({
  110. filename: 'index.html',
  111. template: resolve('/components/index.html')
  112. }),
  113. new HashedModuleIdsPlugin(),
  114. /* //4.0 删除 CommonsChunkPlugin模块,改为splitChunks
  115. * new webpack.optimize.CommonsChunkPlugin({
  116. name: 'vendor'
  117. }),
  118. new webpack.optimize.CommonsChunkPlugin({
  119. name: 'manifest',
  120. chunks: ['vendor'],
  121. }),
  122. /* //4.0报错 Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
  123. new ExtractTextPlugin({
  124. filename: 'assets/css/[name].css',
  125. allChunks: true
  126. }),*/
  127. new CompressionPlugin({
  128. asset: '[path].gz[query]',
  129. algorithm: 'gzip',
  130. test: new RegExp('\\.(js|css)$'),
  131. threshold: 50240,
  132. minRatio: 0.8
  133. }),
  134. //new webpack.NoErrorsPlugin(),
  135. new webpack.optimize.OccurrenceOrderPlugin(),
  136. new CleanPlugin(["*"], {
  137. "root": resolve(c.cf.output.path),
  138. verbose: true,
  139. dry: false
  140. }),
  141. new webpack.HotModuleReplacementPlugin()
  142. ]
  143. };

参考资料:

1、webpack 4.0更新内容

发表评论

表情:
评论列表 (有 0 条评论,329人围观)

还没有评论,来说两句吧...

相关阅读

    相关 zabbix3.X升级4.0

    一、3.2升级至4.0版本 使用3.2版本测试升级至4.0版本,可直接进行升级 (环境是之前已经部署好的,以下仅说明如何进行升级操作) 中文乱码问题可参考:[ht...

    相关 webpack4.x 第二节

    webpack4.x 打包初体验 在根目录下建一个src(源码)目录 在建一个dist(打包后)目录 在src下建一个入口文件,index.js // in