Webpack - Best way to update HTML to include latest [hashed] bundles

梦想与她 提交于 2019-12-12 18:01:05

问题


I'm using webpack to generate hashed bundle filenames.

Assuming I'm using static HTML, CSS & JS, what is the best way to automatically update index.html to point to the latest bundles?

For example,

update:

<script src="e8e839c3a189c25de178.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script>

to:

<script src="d6cba2f2e2fb3f9d98aa.app.js"></script>
<script src="c353591725524074032b.vendor.js"></script> // no change

automatically everytime a new bundle version is available.


回答1:


Amazingly, this is what the html-webpack-plugin is for.

var webpack = require('webpack');
var path = require('path');
var HTMLWebpackPlugin = require('html-webpack-plugin');

module.exports = function(env) {
    return {
        entry: {
            main: './src/index.js',
            vendor: 'moment'
        },
        output: {
            filename: '[chunkhash].[name].js',
            path: path.resolve(__dirname, 'dist')
        },
        plugins: [
            new webpack.optimize.CommonsChunkPlugin({
                names: ['vendor', 'manifest']
            }),
            new HTMLWebpackPlugin({
                tempate: path.join(__dirname, './src/index.html')
            })
        ]
    }
};

This generates an index.html in the dist directory that includes the script's in the correct order.

youtube example



来源:https://stackoverflow.com/questions/41942036/webpack-best-way-to-update-html-to-include-latest-hashed-bundles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!