Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack5升级过程遇到的一些坑? #26

Open
xccjk opened this issue Dec 16, 2020 · 7 comments
Open

webpack5升级过程遇到的一些坑? #26

xccjk opened this issue Dec 16, 2020 · 7 comments

Comments

@xccjk
Copy link
Owner

xccjk commented Dec 16, 2020

webpack5升级过程遇到的一些坑

版本相关信息

  • node: v14.15.0
  • npm: 6.14.8
  • mac: 10.14.6
  • webpack: 5.10.3
  • webpack-cli: 4.2.0
  • webpack-dev-server: 3.11.0
  "webpack": "^5.10.3",
  "webpack-cli": "^4.2.0",
  "webpack-dev-server": "^3.11.0"
  // .babelrc.js
  module.exports = {
    presets: [
      [
        '@babel/preset-env',
        {
          targets: {
            browsers: ['> 5%', 'IE 10', 'iOS 7', 'Firefox > 20']
          },
          useBuiltIns: 'usage',
          corejs: 3
        }
      ],
      '@babel/preset-react'
    ],
    plugins: [
      '@babel/plugin-transform-runtime',
      '@babel/plugin-proposal-class-properties',
      [
        'import',
        {
          libraryName: 'antd',
          libraryDirectory: 'es'
        }
      ]
    ]
  }

问题

  1. Error: Cannot find module 'webpack-cli/bin/config-yargs'

webpack-cli 4.x中,不能过webpack-dev-server启动项目了,需要通过webpack serve...或者修改webpack-cli版本改为3.x

  // package.json
  // webpack4.x
  "script": {
    "dev": "webpack-dev-server ...",
  }
  // webpack5.x
  "script": {
    "dev": "webpack serve ...",
  }
  // 降版本
  {
    webpack-cli@3.3.12
  }
  1. webpack5中错误出现错误UnhandledPromiseRejectionWarning: TypeError: webpack.NamedModulesPlugin is not a constructor
  // webpack.config.js

  // webpack4.x
  module.exports = {
    ...
    plugins: [
      ...
      new webpack.NamedModulesPlugin()
    ]
  }

  // webpack5.x
  // 在webpack5.x中,webpack.NamedModulesPlugin的功能已经内置
  1. babel配置导致的文件引入错误,@babel/runtime

在webpack5.x中,发现很多关于@babel/runtime/helpers/esm的文件引入错误,错误提示类似下面,通过锁定@babel/runtime包版本即可

Module not found: Error: Can't resolve './superPropBase' in '/Users/xxx/node_modules/@babel/runtime/helpers/esm'
Did you mean 'superPropBase.js'?
BREAKING CHANGE: The request './superPropBase' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request
  npm install @babel/runtime@7.12.0 -D
  1. webpack < 5 used to include polyfills for node.js core modules by default

在运行过程中出现了很多这样的报错信息,是由于在webpack5中移除了nodejs核心模块的polyfill自动引入,所以需要手动引入,如果打包过程中有使用到nodejs核心模块,webpack会提示进行相应配置

  // webpack.config.js
  module.exports = {
    ...
    resolve: {
      // https://github.com/babel/babel/issues/8462
      // https://blog.csdn.net/qq_39807732/article/details/110089893
      // 如果确认需要node polyfill,设置resolve.fallback安装对应的依赖
      fallback: {
        crypto: require.resolve('crypto-browserify'),
        path: require.resolve('path-browserify'),
        url: require.resolve('url'),
        buffer: require.resolve('buffer/'),
        util: require.resolve('util/'),
        stream: require.resolve('stream-browserify/'),
        vm: require.resolve('vm-browserify')
      },
      // 如果确认不需要node polyfill,设置resolve.alias设置为false
      alias: {
        crypto: false
      }
    }
  }
  1. DeprecationWarning: Compilation.assets will be frozen in future, all modifications are deprecated. BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
    Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
    Mak`e sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.

html-webpack-plugin这个包导致的⚠️信息,github issue

  npm run html-webpack-plugin@next -D
  1. DeprecationWarning: A 'callback' argument need to be provided to the 'webpack(options, callback)' function when the 'watch' option is set. There is no way to handle the 'watch' option without a callback

官方给出的问题原因是webpack-cli这个包的版本导致的,github issue

  // 官方提供的解决方式,修改webpack-cli版本到4.2.0既可
  npm install webpack-cli@4.2.0 --save-dev

不过我在本地创建了一个新的项目,版本信息如下,还是存在上面的那个报错信息,demo地址github issue

  "webpack": "^5.10.3",
  "webpack-cli": "^4.2.0",
  "webpack-dev-server": "^3.11.0"
  1. 业务插件遇到的问题,webpack-merge包遇到的问题

csdn资料

  // 4.x版本
  {
    "webpack-merge": "^4.2.2"
  }
  // webpack.config.js
  const merge = require('webpack-merge')
  const defaultConfig = require('../...')
  const config = merge(defaultConfig, {

  })
  export default config

  // 5.x版本
  {
    "webpack-merge": "^5.7.0"
  }
  const webpackMerge = require('webpack-merge')
  const defaultConfig = require('../...')
  const config = webpackMerge.merge(defaultConfig, {

  })
  export default config
  1. 数据流工具recoil好像还不支持在webpack中使用,我们项目里有使用recoil,配置了babel后,一直提示You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

官方答复:guthub issue

  1. TypeError: Cannot read property 'tap' of undefined

hard-source-webpack-plugin这个包在版本升级后出现错误,github issue

  // webpack.config.js
  // webpack4.x
  const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
  module.exports = {
    ...
    piugins: [
      new HardSourceWebpackPlugin({
        environmentHash: {
          root: process.cwd(),
          directories: [],
          files: ['package-lock.json', 'yarn.lock'],
        },
        cachePrune: {
          maxAge: 2 * 24 * 60 * 60 * 1000,
          sizeThreshold: 50 * 1024 * 1024
        }
      })
    ]
  }
  // webpack5.x
  // https://github.com/mzgoddard/hard-source-webpack-plugin/issues/461
  const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
  module.exports = {
    ...
    piugins: [
      new HardSourceWebpackPlugin(),
      new HardSourceWebpackPlugin.ExcludeModulePlugin([])
    ]
  }

webpack升级日志

@Thyiad
Copy link

Thyiad commented Dec 30, 2020

在webpack5.x中,发现很多关于@babel/runtime/helpers/esm的文件引入错误,错误提示类似下面,通过锁定@babel/runtime包版本即可

貌似这个锁定也没用

@bbb324
Copy link

bbb324 commented Jan 5, 2021

还好发现这篇,watch 搞了一整天,感谢楼主

@xccjk
Copy link
Owner Author

xccjk commented Jan 8, 2021

  1. 错误Cannot add property htmlWebpackPluginAlterChunks, object is not extensible
    当webpack的版本到5.x时,html-webpack-plugin的版本需要升级到4.x才不会出现这个问题
    我正确的版本
    "webpack": "^5.11.1",
    "webpack-cli": "^4.3.1",
    "webpack-dev-server": "^3.11.1",
    "html-webpack-plugin": "^4.5.1"

@stanleyxu2005
Copy link

我的项目之前是基于webpack3的。直接升级感觉像抽盲盒一样,问题太多也不好定位。
所以呢,就多花一点时间,先从webpack3升级到webpack4。

升级过程还是比较顺利,遇到的唯一问题是css-loader升级到2.0的时候有问题。
最后通过设置css-loaderesModules:false,才解决问题(因为babel里面我设置了modules:false

接下去升级到webpack5基本上什么都没有做。
但也遇到2个deprecation warning。我感觉要安装html-webpack-plugin@next才能解决。
我发现webpack5最大的卖点”更好的treeshaking“效果一般性,甚至发现打包尺寸比原先还要大一点。
所以暂时就先不折腾webpack5了

@JakeXu
Copy link

JakeXu commented Mar 12, 2021

我的项目之前是基于webpack3的。直接升级感觉像抽盲盒一样,问题太多也不好定位。
所以呢,就多花一点时间,先从webpack3升级到webpack4。

升级过程还是比较顺利,遇到的唯一问题是css-loader升级到2.0的时候有问题。
最后通过设置css-loaderesModules:false,才解决问题(因为babel里面我设置了modules:false

接下去升级到webpack5基本上什么都没有做。
但也遇到2个deprecation warning。我感觉要安装html-webpack-plugin@next才能解决。
我发现webpack5最大的卖点”更好的treeshaking“效果一般性,甚至发现打包尺寸比原先还要大一点。
所以暂时就先不折腾webpack5了

应该是esModule: false

@Thyiad
Copy link

Thyiad commented Mar 12, 2021

@JakeXu 谢谢,我现在也没有问题了。前几天发现很多包升级到了webpack5,全部升上去了。
我之前会报defineProperty报错的问题,是因为没有安装@babel/runtime(很奇怪,在4中不安装不会报错,在5中必须安装),安装后就好了,最后的babel配置如下:
`
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": ["ios >= 8", "android >= 4.2"],
"useBuiltIns": "usage",
"corejs": 3
}
],
"@babel/preset-react",
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-transform-runtime",
"@loadable/babel-plugin",
[
"import",
{
"libraryName": "antd",
"libraryDirectory": "lib",
"style": true
}
]
]
}

`

@kenjiding
Copy link

我的项目之前是基于webpack3的。直接升级感觉像抽盲盒一样,问题太多也不好定位。
所以呢,就多花一点时间,先从webpack3升级到webpack4。

升级过程还是比较顺利,遇到的唯一问题是css-loader升级到2.0的时候有问题。
最后通过设置css-loaderesModules:false,才解决问题(因为babel里面我设置了modules:false

接下去升级到webpack5基本上什么都没有做。
但也遇到2个deprecation warning。我感觉要安装html-webpack-plugin@next才能解决。
我发现webpack5最大的卖点”更好的treeshaking“效果一般性,甚至发现打包尺寸比原先还要大一点。
所以暂时就先不折腾webpack5了

怎么可能包更加大了,我升级完5,包体积减少了13m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants