webpack - Webpack 从 V4 更新到 V5:this.plugin 不是函数
问题描述
这是我的 webpack 的 package.json 版本:
"webpack": "5.25.0",
"webpack-cli": "4.5.0",
"webpack-merge": "5.7.3"
所以最新版本的所有人。我来自 4.31 的 webpack 版本。
在我的 webpack.production.js 的 plugins 数组中,我有:
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[hash].app.css',
}),
function symfonyAssetsVersion() {
this.plugin('done', (stats) => {
fs.writeFile(
path.join(__dirname, 'config/packages/', 'assets_version.yaml'),
`parameters:\n assets_hash: ${stats.hash}\n`,
(err) => { if (err) throw err; }
);
});
},
],
My CircleCi tool tells that:
[webpack-cli] TypeError: this.plugin is not a function
at Compiler.symfonyAssetsVersion (/app/webpack.production.js:27:18)
at createCompiler (/app/node_modules/webpack/lib/webpack.js:69:12)
at create (/app/node_modules/webpack/lib/webpack.js:118:16)
at webpack (/app/node_modules/webpack/lib/webpack.js:126:47)
at WebpackCLI.f [as webpack] (/app/node_modules/webpack/lib/index.js:41:15)
at WebpackCLI.createCompiler (/app/node_modules/webpack-cli/lib/webpack-cli.js:1678:29)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3) ```
Any idea on how to fix the this.plugin is not a function ?
Thank you
解决方案
您必须重写symfonyAssetsVersion()
为与 Webpack 5 的 api 匹配的自定义插件。您可以在此处阅读更多信息:https ://webpack.js.org/contribute/writing-a-plugin/ 。
基本上,在原型上使用 apply 创建一个新类。然后挂钩compiler.hooks.done
,回调中会提供统计信息。
推荐阅读
- r - 如何找出 RAM 中有多少内存分配给我闪亮的应用程序
- css - 使用 NX 构建时,Sytles.css 将作为 Styles.js 复制到 dist 文件夹
- swift - 提供的 Firebase OAuth 客户端 ID 无效错误
- docker - 如何在 ansible 中运行 docker 容器?
- javascript - 如果我将我的应用程序重定向到 Next JS 页面,我们可以提取 Next JS 页面中的标题吗?
- mongodb - 用于@BsonIgnore 的工件
- python - 模拟海洋表面波
- php - 如何在 Prestashop 中将付款费用添加到购物车?
- unity3d - 如何区分统一层次结构中的对象类型?
- configuration - 当工作刚刚挂起时,我在哪里可以找到 slurm 诊断信息?