javascript - 我应该如何使用 webpack DefinePlugin 访问 index.ejs 中的哈希包文件名
问题描述
我想要 preact 项目中的散列包文件名。在 preact.config.js 中,输出文件为config.output.filename = "[name].[hash].js";
插件使用webpack.DefinePlugin()
如下所示定义:
config.plugins.push(
new DefinePlugin({
process: {
env: {
API_URL: JSON.stringify(process.env.API_URL),
FILE_NAME: JSON.stringify(config.output.filename)
}
}
})
);
有没有办法将bundle.[hash].js
文件包含在 index.ejs 中。
解决方案
这个任务可以通过HtmlWebpackPlugin来解决,所有的选项都可以通过options
:
export default (config, env, helpers) => {
delete config.entry.polyfills;
// add hash to the file name.
config.output.filename = "[name].[hash].js";
let {plugin} = helpers.getPluginsByName(config, "ExtractTextPlugin")[0];
let html_webpack = helpers.getPluginsByName(config, 'HtmlWebpackPlugin')[0];
// not sure why but without this option it does not inject script tag.
html_webpack.plugin.options.hash = true;
plugin.options.disable = true;
if (env.production) {
config.output.libraryTarget = "umd";
}
};
在这种情况下,模板index.ejs
将用作默认值。
推荐阅读
- python - 通过 id 从 cosmosdb 中“获取”文档(不知道 _rid)
- postgresql - 时间戳比较查询实际上并没有过滤结果
- reactjs - vscode - 基于 react 和 react native 加载代码片段
- java - 如何在 java 中创建 gnome gtk 通知?
- microsoft-graph-api - 查询特定用户电子邮件
- php - wordpress 短代码正在选择完整的 XML 而不是单行
- javascript - 用递归爬升的javascript步骤
- python - 从 pygame 中的 2d 列表中渲染背景框类的问题
- python-3.x - cython id() 函数导致整数溢出
- python - 逐行读取文本文件并计算一行中特定值的特定单词并保存上面的行