我在使用编译挂钩processAssets来镜像自定义插件中已弃用的 Webpack 4 功能时遇到问题。
该插件的目的是将 js/css 文件的 chunkhash 写入 python 文件,Flask 服务器使用该文件根据用户的会话和角色(例如公共、私有、管理员等)分离网页的功能)。
该插件的主要内容(资产的直接突变)如下。
compiler.hooks.emit.tapAsync("WriteHashesPlugin", (compilation, cb) => {
//For each bundle, write the name and the corresponding hash.
compilation.chunks.forEach(chunk => {
lines.push("__" + chunk.name + "_hash__ = '" + chunk.renderedHash + "'")
});
const content = lines.join("\n");
compilation.assets[this.filename] = { // <= this.filename = "{relative_path}/bundles.py"
source: () => content,
size: () => content.length
}
cb();
})
Run Code Online (Sandbox Code Playgroud)
这段代码仍然“可以”创建/更新 python 文件,但我当然不想使用已弃用的语法。构建输出中的警告消息:
(node:38072) [DEP_WEBPACK_COMPILATION_ASSETS] DeprecationWarning: Compilation.assets will be frozen in future, …Run Code Online (Sandbox Code Playgroud)