我之前将 webpack 的 [hash] 值保存到 meta.json 文件中:
class MetaInfoPlugin {
constructor(options) {
this.options = { filename: 'meta.json', ...options };
}
apply(compiler) {
compiler.hooks.done.tap(this.constructor.name, (stats) => {
const metaInfo = {
// add any other information if necessary
hash: stats.hash
};
const json = JSON.stringify(metaInfo);
return new Promise((resolve, reject) => {
fs.writeFile(this.options.filename, json, 'utf8', (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
});
}
}
module.exports = {
mode: 'production',
target: 'web',
...
plugins: [
new MetaInfoPlugin({ filename: './public/theme/assets/scripts/meta.json' }),
],
...
};
升级到 webpack 5 后,我收到了指向使用 contenthash 或其他值的弃用通知。
[DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH] DeprecationWarning: [hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)
.hash
但是将上面的部分与或任何其他散列交换.contenthash
将不起作用。如何将 contenthash 保存到文件中,以便以后可以在模板系统中使用该值来链接文件?
我基本上试图将[contenthash]
值放入文本文件(json,无论格式)中,以便稍后在 PHP 模板系统中重用。