diff --git a/examples/webpack4-contenthash/README.md b/examples/webpack4-contenthash/README.md new file mode 100644 index 0000000..d8c7453 --- /dev/null +++ b/examples/webpack4-contenthash/README.md @@ -0,0 +1,3 @@ +# Ensure contenthash path variable works in Webpack 4.3+ + +Test case for issue #78 diff --git a/examples/webpack4-contenthash/chunk.js b/examples/webpack4-contenthash/chunk.js new file mode 100644 index 0000000..3710888 --- /dev/null +++ b/examples/webpack4-contenthash/chunk.js @@ -0,0 +1 @@ +console.log('ok'); diff --git a/examples/webpack4-contenthash/index.js b/examples/webpack4-contenthash/index.js new file mode 100644 index 0000000..ae97ab5 --- /dev/null +++ b/examples/webpack4-contenthash/index.js @@ -0,0 +1,3 @@ +require.ensure([], require => { + require('./chunk.js'); +}); diff --git a/examples/webpack4-contenthash/test.js b/examples/webpack4-contenthash/test.js new file mode 100644 index 0000000..f726185 --- /dev/null +++ b/examples/webpack4-contenthash/test.js @@ -0,0 +1,12 @@ +var webpackVersionComponents = require('webpack/package.json').version.split( + '.' +); +var webpackVersionMajor = Number(webpackVersionComponents[0]); +var webpackVersionMinor = Number(webpackVersionComponents[0]); + +module.exports.skip = function skip() { + return ( + webpackVersionMajor < 4 || + (webpackVersionMajor === 4 && webpackVersionMinor < 3) + ); +}; diff --git a/examples/webpack4-contenthash/webpack.config.js b/examples/webpack4-contenthash/webpack.config.js new file mode 100644 index 0000000..abd3d1e --- /dev/null +++ b/examples/webpack4-contenthash/webpack.config.js @@ -0,0 +1,18 @@ +var SriPlugin = require("webpack-subresource-integrity"); +var HtmlWebpackPlugin = require("html-webpack-plugin"); + +module.exports = { + entry: "./index.js", + mode: "production", + output: { + filename: "[name].[contenthash].js", + chunkFilename: "[name].[contenthash].js", + crossOriginLoading: "anonymous" + }, + plugins: [ + new HtmlWebpackPlugin(), + new SriPlugin({ + hashFuncNames: ["sha256", "sha384"] + }) + ] +}; diff --git a/util.js b/util.js index df472e9..044d84f 100644 --- a/util.js +++ b/util.js @@ -118,11 +118,13 @@ function getChunkFilename(compilation, chunk) { var args = [isInitialChunk(chunk) ? filename : chunkFilename, { hash: compilation.hash.substr(0, compilation.mainTemplate.outputOptions.hashDigestLength), hashWithLength: length => compilation.hash.substr(0, length), + contentHashType: "javascript", chunk: { id: chunk.id, hash: chunk.hash.substr(0, compilation.mainTemplate.outputOptions.hashDigestLength), hashWithLength: length => chunk.hash.substr(0, length), - name: chunk.name + name: chunk.name, + contentHash: chunk.contentHash } }];