webpack升级踩坑

Posted hideonbush

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webpack升级踩坑相关的知识,希望对你有一定的参考价值。

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;

以上是关于webpack升级踩坑的主要内容,如果未能解决你的问题,请参考以下文章

webpack 1.x升级过程中的踩坑总结大全

第1241期webpack4升级完全指南

webpack初学踩坑记

webpack踩坑之旅

webpack踩坑记录

Webpack5 踩坑之(html-webpack-plugin)