I am new to webpack and I am trying to add one hash value to the js file inside vueJS application whenever new build happened to avoid caching. Using webpack ExtendedApiPlugin
tried to pass the hash value to the template.ejs
file, but it throws error like Uncaught ReferenceError: __webpack_hash__ is not defined
. Referred multiple forums and posts and everywhere it is mentioned similar like this, but it is not working for me as expected. I have defined the plugin in webpack.config.js
file like below,
webpack.config.js
plugins: [
new webpack.ExtendedAPIPlugin(),
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, 'dist', 'index.html'),
template: path.resolve(__dirname, 'src', 'template.ejs'),
inject: true
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new VueLoaderPlugin()
]
webpack.config.dev.js
plugins: [
new webpack.HotModuleReplacementPlugin(),
new WriteFilePlugin()
]
This is template.ejs file,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/png" href="/images/favicon.png"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My App</title>
</head>
<body>
<% if (process.env.NODE_ENV === 'production') { %>
<script type="text/javascript" src="/inject.js"></script>
<% } %>
<div id="my-app">
</div>
</body>
<script> var val = __webpack_hash__; console.log("111111"); console.log(val) </script>
</html>
Is there any way to make this work or any other option to pass the hash value from webpack config to html file. Any help will be much appreciated.
Solution:
Found the solution after multiple attempts. We can use the following format to fetch the __webpack_hash__
value,
let webpackHash = <%= __webpack_hash__ %>;