[英]How should I access hash bundle file name in index.ejs using webpack DefinePlugin
I want hashed bundle file name in preact project.我想在 preact 项目中散列包文件名。 in preact.config.js the output file is as
config.output.filename = "[name].[hash].js";
在 preact.config.js 中,输出文件为
config.output.filename = "[name].[hash].js";
Plugin is defined using webpack.DefinePlugin()
as shown below:插件是使用
webpack.DefinePlugin()
定义的,如下所示:
config.plugins.push(
new DefinePlugin({
process: {
env: {
API_URL: JSON.stringify(process.env.API_URL),
FILE_NAME: JSON.stringify(config.output.filename)
}
}
})
);
Is there any way to include the bundle.[hash].js
file in index.ejs.有没有办法在 index.ejs 中包含
bundle.[hash].js
文件。
This task can be solved via HtmlWebpackPlugin , all the options can be controlled through options
:这个任务可以通过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";
}
};
In this case, the template index.ejs
will be used as default .在这种情况下,模板
index.ejs
将用作默认值。
I tried to solve this problem by creating a hash function by myself and exporting the hash function.我试图通过自己创建一个哈希函数并导出哈希函数来解决这个问题。
const hashNumber = hashGenerator();
delete config.entry.polyfills;
config.entry.app = "./index.js"
config.output.filename = "[name]."+hashNumber+".js";
let { plugin } = helpers.getPluginsByName(config, "ExtractTextPlugin")[0];
plugin.options.disable = true;
if (env.production) {
config.output.libraryTarget = "umd";
}
config.plugins.push(
new DefinePlugin({
process: {
env: {
HASH: JSON.stringify(hashNumber)
}
}
})
);
};
and including that hash in index.ejs file并将该哈希包含在 index.ejs 文件中
script.src = `/bundle.<%= process.env.HASH %>.js`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.