Skip to content

Instantly share code, notes, and snippets.

@d4goxn
Last active June 12, 2017 03:16
Show Gist options
  • Save d4goxn/37be7b185cdf4ac28cbb to your computer and use it in GitHub Desktop.
Save d4goxn/37be7b185cdf4ac28cbb to your computer and use it in GitHub Desktop.
Webpack MD5 hashed split builds and HTML templating, with stable vendor bundle hash
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app-root"></div>
{{scripts}}
</body>
</html>
const path = require("path");
const fs = require("fs");
const webpack = require("webpack");
const Md5Hash = require("webpack-md5-hash");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const dependencies = Object.keys(require("./package").dependencies);
const config = module.exports = {
devtool: "source-map",
entry: {
app: "./src/client.js",
vendor: dependencies
},
output: {
path: path.join(__dirname, "dist"),
publicPath: "/dist",
filename: "[name].[chunkhash].js",
chunkFilename: "[name].[chunkhash].js"
},
module: {
loaders: [{
test: /\.jsx?/,
exclude: /node_modules/,
loader: "babel"
}]
},
plugins: [
new CleanWebpackPlugin(["dist"]),
new Md5Hash(),
new webpack.optimize.CommonsChunkPlugin("vendor", "[name].[chunkhash].js"),
new webpack.NamedModulesPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {warnings: false},
output: {comments: false}
}),
new webpack.DefinePlugin({
"process.env": {NODE_ENV: JSON.stringify("production")}
}),
function() {
this.plugin("done", function(stats) {
const assets = stats.toJson().assetsByChunkName;
fs.writeFileSync(
path.join(__dirname, "index.html"),
fs.readFileSync(path.join(__dirname, "src", "index.html")).toString().replace(
"{{scripts}}",
[
`<script src="/dist/${assets.vendor[0]}"></script>`,
`<script src="/dist/${assets.app[0]}"></script>`
].join("\n\t\t")
)
);
});
}
]
};
@d4goxn
Copy link
Author

d4goxn commented Feb 6, 2016

Update: don't map over stats.assetsByChunkName's keys because that can generate script tags out of order.

@blade254353074
Copy link

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment