Webpack and version numbers in output file
Asked Answered
V

3

6

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

Verminous answered 24/5, 2016 at 14:30 Comment(0)
D
4

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.

Dowling answered 7/8, 2019 at 12:8 Comment(0)
D
2

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

Dagall answered 15/5, 2017 at 18:17 Comment(0)
H
1

If the question here is how to use the version from your package.json, I had the same problem and solved it by:

add this to your webpack.config.js

var PACKAGE = require('./package.json');
var version = PACKAGE.version;

Then you can append the version to your file:

  output: {
    ...
    filename: `myLibrary-${version}.js`
    ...
  }
Hautboy answered 19/2, 2021 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.