webpck4学习之新手错题集

1,422 阅读2分钟
  1. TypeError: this.options.cssProcessor.process is not a function

Pasted Graphic 3.png

分析: 安装的是webpack5, 然后使用optimize-css-assets-webpack-plugin压缩CSS,报如上错误。
解决: 使用新的plugin插件:  css-minimizer-webpack-plugin 替换

optimize-css-assets-webpackplugin o.png

  1. Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema

Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.

configuration.module has an unknown property 'mode'. These properties are valid: object { defaultRules?, exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, generator?, noParse?, parser?, rules?, strictExportPresence?, strictThisContextOnImports?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp? }
Options affecting the normal modules (NormalModuleFactory). 分析: 对应的关键字位置或者拼写出错,出现的报错信息

  1. TypeError: Cannot read property 'tap' of undefined image.png 分析: webpack使用版本与其中一个插件的依赖版本不一致导致的;当前使用webpack4,html模版解析插件html-webpack-plugin使用的版本依赖webpack5
    解决: 将两者提到统一大版本即可

  2. TypeError: Invalid value used in weak set image.png 分析: 使用命令行npm list webpack, 查看webpack在各依赖中的使用版本情况,有如下截图中插件存在版本依赖问题
    解决: 依次对其进行版本降级 image.png

  3. Cannot use [chunkhash] or [contenthash] for chunk in '[name]-[chunkhash:8].js' (use [hash] instead) image.png 分析: image.png

  4. WARNING in asset size limit: The following asset(s) exceed the recommended size limit (244 KiB). image.png 分析:assets文件或者入口文件大小超过了建议资源大小,存在影响打包性能的问题。
    解决:关闭webpack性能提示

module.exports= {
    // ...
    performance: {
        hints: "warning", // 枚举
        hints: "error", // 性能提示中抛出错误
        hints: false, // 关闭性能提示
        maxAssetSize: 200000, // 整数类型(以字节为单位)
        maxEntrypointSize: 400000, // 整数类型(以字节为单位)
        assetFilter: function (assetFilename) {
            // 提供资源文件名的断言函数
            return assetFilename.endsWith('.css') || assetFilename.endsWith('.js');
        }
    }
}
  1. TypeError: this.options.cssProcessor.process is not a function image.png 分析: 当前使用的cssnano(5.0.9)版本过高导致的
    解决: 降级版本号至 4.1.11

  2. TypeError: this.getOptions is not a function at Object.loader image.png 分析: 引入postcss-loader之后,运行报错
    解决: 将postcss-loader版本号由 6.2.0 降至 4.2.0

  3. TypeError: this.getOptions is not a function image.png 分析: 引入less-loader报错,版本号过高,不适配webpack4+,当前引入版本为8.1.1
    解决: 降级版本至7.3.0


Tips:
       同样的报错语句,不同的报错原因
       版本号降级应该是存在部分语法结构变化或者依赖变化的,根因待寻找