html-webpack-plugin
Advanced tools
Comparing version 5.3.0 to 5.3.1
@@ -5,2 +5,9 @@ # Changelog | ||
### [5.3.1](https://github.com/jantimon/html-webpack-plugin/compare/v5.3.0...v5.3.1) (2021-03-09) | ||
### Bug Fixes | ||
* remove loader-utils from plugin core ([82d0ee8](https://github.com/jantimon/html-webpack-plugin/commit/82d0ee8ddf146f17d71e98c1b44b2f2ec7420051)) | ||
## [5.3.0](https://github.com/jantimon/html-webpack-plugin/compare/v5.2.0...v5.3.0) (2021-03-07) | ||
@@ -7,0 +14,0 @@ |
81
index.js
@@ -17,3 +17,2 @@ // @ts-check | ||
const path = require('path'); | ||
const loaderUtils = require('loader-utils'); | ||
const { CachedChildCompilation } = require('./lib/cached-child-compiler'); | ||
@@ -136,4 +135,3 @@ | ||
require: require, | ||
htmlWebpackPluginPublicPath: | ||
publicPath, | ||
htmlWebpackPluginPublicPath: publicPath, | ||
URL: require('url').URL, | ||
@@ -194,9 +192,2 @@ __filename: templateWithoutLoaders | ||
// `contenthash` is introduced in webpack v4.3 | ||
// which conflicts with the plugin's existing `contenthash` method, | ||
// hence it is renamed to `templatehash` to avoid conflicts | ||
options.filename = options.filename.replace(/\[(?:(\w+):)?contenthash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (match) => { | ||
return match.replace('contenthash', 'templatehash'); | ||
}); | ||
// Check if webpack is running in production mode | ||
@@ -255,11 +246,2 @@ // @see https://github.com/webpack/webpack/blob/3366421f1784c449f415cda5930a8e445086f688/lib/WebpackOptionsDefaulter.js#L12-L14 | ||
const compiledEntries = 'compiledEntry' in templateResult ? { | ||
hash: templateResult.compiledEntry.hash, | ||
chunk: templateResult.compiledEntry.entry | ||
} : { | ||
hash: templateResult.mainCompilationHash | ||
}; | ||
const childCompilationOutputName = compilation.getAssetPath(options.filename, compiledEntries); | ||
// If the child compilation was not executed during a previous main compile run | ||
@@ -270,3 +252,3 @@ // it is a cached result | ||
/** The public path used inside the html file */ | ||
const htmlPublicPath = getPublicPath(compilation, childCompilationOutputName, options.publicPath); | ||
const htmlPublicPath = getPublicPath(compilation, options.filename, options.publicPath); | ||
@@ -296,3 +278,3 @@ /** Generated file paths from the entry point names */ | ||
assets: assets, | ||
outputName: childCompilationOutputName, | ||
outputName: options.filename, | ||
plugin: plugin | ||
@@ -315,3 +297,3 @@ }); | ||
}, | ||
outputName: childCompilationOutputName, | ||
outputName: options.filename, | ||
publicPath: htmlPublicPath, | ||
@@ -330,3 +312,3 @@ plugin: plugin | ||
bodyTags: assetGroups.bodyTags, | ||
outputName: childCompilationOutputName, | ||
outputName: options.filename, | ||
publicPath: htmlPublicPath, | ||
@@ -362,3 +344,3 @@ plugin: plugin | ||
.then(([assetTags, html]) => { | ||
const pluginArgs = { html, headTags: assetTags.headTags, bodyTags: assetTags.bodyTags, plugin: plugin, outputName: childCompilationOutputName }; | ||
const pluginArgs = { html, headTags: assetTags.headTags, bodyTags: assetTags.bodyTags, plugin: plugin, outputName: options.filename }; | ||
return getHtmlWebpackPluginHooks(compilation).afterTemplateExecution.promise(pluginArgs); | ||
@@ -373,3 +355,3 @@ }) | ||
.then((html) => { | ||
const pluginArgs = { html, plugin: plugin, outputName: childCompilationOutputName }; | ||
const pluginArgs = { html, plugin: plugin, outputName: options.filename }; | ||
return getHtmlWebpackPluginHooks(compilation).beforeEmit.promise(pluginArgs) | ||
@@ -385,12 +367,11 @@ .then(result => result.html); | ||
.then(html => { | ||
// Allow to use [templatehash] as placeholder for the html-webpack-plugin name | ||
// See also https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/ | ||
// From https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/8de6558e33487e7606e7cd7cb2adc2cccafef272/src/index.js#L212-L214 | ||
const finalOutputName = childCompilationOutputName.replace(/\[(?:(\w+):)?templatehash(?::([a-z]+\d*))?(?::(\d+))?\]/ig, (_, hashType, digestType, maxLength) => { | ||
return loaderUtils.getHashDigest(Buffer.from(html, 'utf8'), hashType, digestType, parseInt(maxLength, 10)); | ||
}); | ||
const filename = options.filename.replace(/\[templatehash([^\]]*)\]/g, require('util').deprecate( | ||
(match, options) => `[contenthash${options}]`, | ||
'[templatehash] is now [contenthash]') | ||
); | ||
const replacedFilename = replacePlaceholdersInFilename(filename, html, compilation); | ||
// Add the evaluated html code to the webpack assets | ||
compilation.emitAsset(finalOutputName, new webpack.sources.RawSource(html, false)); | ||
previousEmittedAssets.push({ name: finalOutputName, html }); | ||
return finalOutputName; | ||
compilation.emitAsset(replacedFilename.path, new webpack.sources.RawSource(html, false), replacedFilename.info); | ||
previousEmittedAssets.push({ name: replacedFilename.path, html }); | ||
return replacedFilename.path; | ||
}) | ||
@@ -534,2 +515,34 @@ .then((finalOutputName) => getHtmlWebpackPluginHooks(compilation).afterEmit.promise({ | ||
/** | ||
* Replace [contenthash] in filename | ||
* | ||
* @see https://survivejs.com/webpack/optimizing/adding-hashes-to-filenames/ | ||
* | ||
* @param {string} filename | ||
* @param {string|Buffer} fileContent | ||
* @param {WebpackCompilation} compilation | ||
* @returns {{ path: string, info: {} }} | ||
*/ | ||
function replacePlaceholdersInFilename (filename, fileContent, compilation) { | ||
if (/\[\\*([\w:]+)\\*\]/i.test(filename) === false) { | ||
return { path: filename, info: {} }; | ||
} | ||
const hash = compiler.webpack.util.createHash(compilation.outputOptions.hashFunction); | ||
hash.update(fileContent); | ||
if (compilation.outputOptions.hashSalt) { | ||
hash.update(compilation.outputOptions.hashSalt); | ||
} | ||
const contentHash = hash.digest(compilation.outputOptions.hashDigest).slice(0, compilation.outputOptions.hashDigestLength); | ||
return compilation.getPathWithInfo( | ||
filename, | ||
{ | ||
contentHash, | ||
chunk: { | ||
hash: contentHash, | ||
contentHash | ||
} | ||
} | ||
); | ||
} | ||
/** | ||
* Helper to sort chunks | ||
@@ -536,0 +549,0 @@ * @param {string[]} entryNames |
{ | ||
"name": "html-webpack-plugin", | ||
"version": "5.3.0", | ||
"version": "5.3.1", | ||
"license": "MIT", | ||
@@ -31,3 +31,2 @@ "description": "Simplifies creation of HTML files to serve your webpack bundles", | ||
"devDependencies": { | ||
"@types/loader-utils": "2.0.1", | ||
"@types/node": "11.13.9", | ||
@@ -49,5 +48,5 @@ "commitizen": "4.2.1", | ||
"typescript": "4.1.3", | ||
"webpack": "5.23.0", | ||
"webpack": "5.24.3", | ||
"webpack-recompilation-simulator": "3.2.0", | ||
"webpack-cli": "4.2.0" | ||
"webpack-cli": "4.5.0" | ||
}, | ||
@@ -54,0 +53,0 @@ "dependencies": { |
@@ -467,3 +467,3 @@ [![npm][npm]][npm-url] | ||
For long term caching add `contenthash/templatehash` to the filename. | ||
For long term caching add `contenthash` to the filename. | ||
@@ -480,12 +480,6 @@ **Example:** | ||
`contenthash/templatehash` is the hash of the content of the output file. | ||
`contenthash` is the hash of the content of the output file. | ||
Optionally, You can configure like `[<hashType>:contenthash:<digestType>:<length>]` | ||
Refer webpack's [Template Strings](https://webpack.js.org/configuration/output/#template-strings) for more details | ||
* `hashType` - one of `sha1`, `md5`, `sha256`, `sha512` or any other node.js supported hash type | ||
* `digestType` - one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64` | ||
* `maxlength` - maximum length of the generated hash in chars | ||
**Defaults:** `[md5:contenthash:hex:9999]` | ||
### Events | ||
@@ -492,0 +486,0 @@ |
19
2227
150818
669