Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and version numbers in output file

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

like image 785
Shahzad Fateh Ali Avatar asked May 24 '16 14:05

Shahzad Fateh Ali


People also ask

How does Webpack know what files are being generated?

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.

What is the output key in Webpack?

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.

How to add a custom version of JavaScript to a 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.

How does the htmlwebpackplugin work with npm builds?

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:


2 Answers

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.

like image 103
Paul T. Rawkeen Avatar answered Nov 22 '22 22:11

Paul T. Rawkeen


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

like image 43
Manis Avatar answered Nov 22 '22 21:11

Manis