webpack升级踩坑

本文详细介绍了从Webpack3.8.1升级至Webpack4.39.2的过程中遇到的常见错误及其解决方案,包括修复scripts/start.js中的webpack不是函数错误,解决html-webpack-plugin钩子函数问题,替换ExtractTextPlugin为mini-css-extract-plugin,以及升级webpack-manifest-plugin版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

webpack3.8.1 => webpack4.39.2

1、error:webpack is not a function

fix scripts/start.js
const compiler = createCompiler(webpack, config, appName, urls, useYarn);
=>
const compiler = createCompiler({ webpack, config, appName, urls, useYarn });

2、error: this.htmlWebpackPlugin.getHooks is not a function

fix config/webpack.config.*.js
new InterpolateHtmlPlugin(env.raw),
=>
new InterpolateHtmlPlugin(HtmlWebpackPlugin,env.raw),

3、弃用 ExtractTextPlugin =>为 mini-css-extract-plugin

{
            test: /\.(css|less)$/,
            use: [
                {
                    loader: MiniCssExtractPlugin.loader,
                },
                {
                    loader: require.resolve('css-loader'),
                    options: {
                      importLoaders: 1,
                      sourceMap: shouldUseSourceMap,
                    },
                },
                {
                    loader: require.resolve('postcss-loader'),
                    options: {
                      // Necessary for external CSS imports to work
                      // https://github.com/facebookincubator/create-react-app/issues/2677
                      ident: 'postcss',
                      plugins: () => [
                        require('postcss-flexbugs-fixes'),
                        autoprefixer({
                          browsers: [
                            '>1%',
                            'last 4 versions',
                            'Firefox ESR',
                            'not ie < 9', // React doesn't support IE8 anyway
                          ],
                          flexbox: 'no-2009',
                        }),
                      ],
                    },
                },
                {
                    loader: require.resolve('less-loader'),
                    options: {
                        modifyVars: antdtheme,
                        javascriptEnabled: true
                    },
                },
            ]
          },

4、error: Chunk.initial was removed. Use canBeInitial/isOnlyInitial() at Chunk.get initial

解决方法:升级 "webpack-manifest-plugin": "^1.3.2" 到 "webpack-manifest-plugin": "^2.0.4"

5、warning:Tapable.plugin is deprecated. Use new API on .hooks instead

此警告信息指出某个loader或plugin使用了废弃的api,但不影响打包编译。
通过获取stracktrace来确定谁发出警告:
process.traceDeprecation = true;

转载于:https://www.cnblogs.com/hideonbush/p/11415851.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值