Search code examples
javascriptwebpackhtml-webpack-plugin

Modify HtmlWebpackPlugin's generated script URLs


When using HtmlWebpackPlugin to generate the dist/index.html file, we can use the inject option to automatically create <script> tags for the javascript bundle files:

new HtmlWebpackPlugin({
    filename: config.build.index,
    template: 'index.html',
    inject: true,
    // ...
})

And get an index.html file that looks similar to:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Webpack App</title>
    </head>
    <body>
        <script src="index_bundle.js"></script>
    </body>
</html>

For server side constraints, I need to add a query string parameter in the generated script source, to make it look like <script src="index_bundle.js?my-parameter=my-value"></script>. I've been looking at the plugin documentation, but I can't find the way to do it.

Is there any option to modify the generated URLs, by appending an string or replacing with a RegEx?

Thanks in advance!


Solution

  • Based on thedarkone's answer, I provide the resolution for my question:

    I added the query parameter in the webpack configuration output parameter (production build only in my case) and use HtmlWebpackPlugin with the default config:

    const webpackConfig = merge(baseWebpackConfig, {
        // ...
        output: {
            path: config.build.assetsRoot,
            filename: utils.assetsPath('js/[name].js?my-parameter=my-value'),
        },
        // ...
        new HtmlWebpackPlugin({
            filename: config.build.index,
            template: 'index.html',
            inject: true,
            // ...
        }
        // ...
    }
    

    Since I also need the query parameter in the style links, I also had to modify the ExtractTextPlugin filename parameter in the same way:

    // ...
    new ExtractTextPlugin({
        filename: 'bundle.[chunkhash].js'
        filename: utils.assetsPath('css/[name].css?my-parameter=my-value'),
    })
    // ...
    

    Keep in mind that this approach will append the query parameters to ALL of the injected scripts/styles.