一次create-react-app创建项目升级webpack的流水账

不再赘述为什么要升级webpack4,有兴趣的小伙伴可以看一下 知乎:如何评价webpack4

下面撸起袖子开干:

克隆项目,新建分支

git checkout -b feature_webpack_upgrade  
# 相当于以下两句的简写
git branch feature_webpack_upgrade  
git checkout feature_webpack_upgrade

升级webpack

yarn add webpack webpack-dev-server webpack-cli

运行后报错:

Plugin could not be registered at 'html-webpack-plugin-before-html-processing'. Hook was not found.

报错信息表示插件html-webpack-plugin-before-html-processing有问题,然而webpack中并没有这个插件,google之后发现github上有对这个问题的讨论,所以升级插件:

yarn add react-dev-utils html-webpack-plugin

修改配置文件,交换以下两个插件位置 :

new HtmlWebpackPlugin({}), 

new InterpolateHtmlPlugin(env.raw),

继续运行,报错

TypeError: Cannot read property 'eslint' of undefined

升级eslint即可

yarn add eslint-loader

继续运行,报错:

missingDeps.some not a function

这个问题比较棘手,继续google之后找到原因missingDeps.some not a function

yarn add [email protected]

继续运行,报错:

Error: webpack.optimize.UglifyJsPlugin has been removed, please use config.optimization.minimize instead.

干掉被移除的插件UglifyJsPlugin 即可,webpack4生产模式下原生支持代码压缩和分割

继续运行,报错:

Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead 
mini-css-extract-plugin

很明显,Entrypointmini-css-extract-plugin提供了,查找之下发现Chunk.entrypointsextract-text-webpack-plugin 提供,那么:

yarn remove extract-text-webpack-plugin
yarn add mini-css-extract-plugin

继续运行,报错:

Error: Chunk.initial was removed. Use canBeInitial/isOnlyInitial()

升级webpack-manifest-plugin即可。

运行通过,提交代码,PR。

所有问题都可以通过仔细阅读error trace加上善用google搞定。

收工~

相关推荐