I am using webpack to build my react application. I just want to know is there are way of generating html having script tab with version number in file name like .
I have tried to explore the HtmlWebpackPlugin but found nothing. Can anyone give me a direction whether there is a plugin available to do this or I have to write my own script.
Thanks
You might be wondering how webpack and its plugins seem to "know" what files are being generated. The answer is in the manifest that webpack keeps to track how all the modules map to the output bundles. If you're interested in managing webpack's output in other ways, the manifest would be a good place to start.
Output The top-level output key contains set of options instructing webpack on how and where it should output your bundles, assets and anything else you bundle or load with webpack.
Then you need to set up your webpack config: As you want to inject it into javascript, you should add a tag inside your javascript file ( which will be changed to version during the webpack compilation ) You can set it up to auto increase the version directly from webpack by: Where --other-webpack-settings is equal to your custom line args.
Before we do a build, you should know that the HtmlWebpackPlugin by default will generate its own index.html file, even though we already have one in the dist/ folder. This means that it will replace our index.html file with a newly generated one. Let's see what happens when we do an npm run build:
Fulfilling @Manis answer I want to provide more complete instructions on this solution.
It can be achieved by output.filename
substitutions settings.
{
entry: ['./your/entry.js'],
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[contenthash].bundle.js', // Line of interest
publicPath: '/'
},
/* ... */
}
The
[contenthash]
substitution will add a unique hash based on the content of an asset. When the asset's content changes,[contenthash]
will change as well.
More options and examples here.
Use [hash]
in the name of your output files. Look here for more details.
P.S.: Your
index.html
should have correct file name as far as I'm experiencing in my project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With