From 8c43febb2f45f9f5e6e76833d09254a48baf6c3f Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Sat, 12 Oct 2024 20:37:00 +0300 Subject: [PATCH] fix: logic for assets --- lib/asset/AssetGenerator.js | 119 +++++------------- .../asset-modules-source/source/file.text | 3 + .../asset-modules-source/source/index.js | 10 ++ .../source/webpack.config.js | 21 ++++ 4 files changed, 67 insertions(+), 86 deletions(-) create mode 100644 test/hotCases/asset-modules-source/source/file.text create mode 100644 test/hotCases/asset-modules-source/source/index.js create mode 100644 test/hotCases/asset-modules-source/source/webpack.config.js diff --git a/lib/asset/AssetGenerator.js b/lib/asset/AssetGenerator.js index 1e4a1b443..a7bace530 100644 --- a/lib/asset/AssetGenerator.js +++ b/lib/asset/AssetGenerator.js @@ -336,42 +336,6 @@ class AssetGenerator extends Generator { return encodedSource; } - /** - * @private - * @param {NormalModule} module module for which the code should be generated - * @param {GenerateContext} generateContext context for generate - * @returns {string} the full content hash - */ - _getFullContentHash(module, { runtimeTemplate }) { - const hash = createHash( - /** @type {Algorithm} */ - (runtimeTemplate.outputOptions.hashFunction) - ); - if (runtimeTemplate.outputOptions.hashSalt) { - hash.update(runtimeTemplate.outputOptions.hashSalt); - } - - hash.update(/** @type {Source} */ (module.originalSource()).buffer()); - - return /** @type {string} */ ( - hash.digest(runtimeTemplate.outputOptions.hashDigest) - ); - } - - /** - * @private - * @param {string} fullContentHash the full content hash - * @param {GenerateContext} generateContext context for generate - * @returns {string} the content hash - */ - _getContentHash(fullContentHash, generateContext) { - return nonNumericOnlyHash( - fullContentHash, - /** @type {number} */ - (generateContext.runtimeTemplate.outputOptions.hashDigestLength) - ); - } - /** * @private * @param {NormalModule} module module for which the code should be generated @@ -519,67 +483,52 @@ class AssetGenerator extends Generator { (module.buildInfo).dataUrl && needContent ) { - if (data && data.has("url") && data.get("url")[type] !== undefined) { - content = data.get("url")[type]; - } else { - const encodedSource = this.generateDataUri(module); - content = - type === "javascript" ? JSON.stringify(encodedSource) : encodedSource; + const encodedSource = this.generateDataUri(module); + content = + type === "javascript" ? JSON.stringify(encodedSource) : encodedSource; - if (data) { - data.set("url", { [type]: content }); - } + if (data) { + data.set("url", { [type]: content, ...data.get("url") }); } } else { - /** @type {string} */ - let fullHash; + const hash = createHash( + /** @type {Algorithm} */ + (runtimeTemplate.outputOptions.hashFunction) + ); - if (data && data.has("fullContentHash")) { - fullHash = data.get("fullContentHash"); - } else { - fullHash = this._getFullContentHash(module, generateContext); + if (runtimeTemplate.outputOptions.hashSalt) { + hash.update(runtimeTemplate.outputOptions.hashSalt); + } - if (data) { - data.set("fullContentHash", fullHash); - } + hash.update(/** @type {Source} */ (module.originalSource()).buffer()); + + const fullHash = + /** @type {string} */ + (hash.digest(runtimeTemplate.outputOptions.hashDigest)); + + if (data) { + data.set("fullContentHash", fullHash); } /** @type {BuildInfo} */ (module.buildInfo).fullContentHash = fullHash; /** @type {string} */ - let contentHash; + const contentHash = nonNumericOnlyHash( + fullHash, + /** @type {number} */ + (generateContext.runtimeTemplate.outputOptions.hashDigestLength) + ); - if (data && data.has("contentHash")) { - contentHash = data.get("contentHash"); - } else { - contentHash = this._getContentHash(fullHash, generateContext); - - if (data) { - data.set("contentHash", contentHash); - } + if (data) { + data.set("contentHash", contentHash); } - let originalFilename; - let filename; - let assetInfo; + const { originalFilename, filename, assetInfo } = + this._getFilenameWithInfo(module, generateContext, contentHash); - if (data && data.has("originalFilename")) { - originalFilename = data.get("originalFilename"); - filename = data.get("filename"); - assetInfo = data.get("assetInfo"); - } else { - ({ originalFilename, filename, assetInfo } = this._getFilenameWithInfo( - module, - generateContext, - contentHash - )); - - if (data) { - data.set("filename", filename); - data.set("assetInfo", assetInfo); - data.set("filenameWithInfo", originalFilename); - } + if (data) { + data.set("filename", filename); } const { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo( @@ -590,14 +539,12 @@ class AssetGenerator extends Generator { contentHash ); - assetInfo = newAssetInfo; - if (data && (type === "javascript" || type === "css-url")) { data.set("url", { [type]: assetPath, ...data.get("url") }); } if (data) { - data.set("assetInfo", assetInfo); + data.set("assetInfo", newAssetInfo); } // Due to code generation caching module.buildInfo.XXX can't used to store such information @@ -607,7 +554,7 @@ class AssetGenerator extends Generator { (module.buildInfo).filename = filename; /** @type {BuildInfo} */ - (module.buildInfo).assetInfo = assetInfo; + (module.buildInfo).assetInfo = newAssetInfo; content = assetPath; } diff --git a/test/hotCases/asset-modules-source/source/file.text b/test/hotCases/asset-modules-source/source/file.text new file mode 100644 index 000000000..75f6c1cbf --- /dev/null +++ b/test/hotCases/asset-modules-source/source/file.text @@ -0,0 +1,3 @@ +A +--- +B diff --git a/test/hotCases/asset-modules-source/source/index.js b/test/hotCases/asset-modules-source/source/index.js new file mode 100644 index 000000000..194bedd64 --- /dev/null +++ b/test/hotCases/asset-modules-source/source/index.js @@ -0,0 +1,10 @@ +it("should regenerate contenthash", function(done) { + const value = new URL("./file.text", import.meta.url); + expect(/file\.7eff7665bf7fc2696232\.text/.test(value.toString())).toBe(true); + module.hot.accept("./file.text", function() { + const value = new URL("./file.text", import.meta.url); + expect(/file\.402033be7494a9255415\.text/.test(value.toString())).toBe(true); + done(); + }); + NEXT(require("../../update")(done)); +}); diff --git a/test/hotCases/asset-modules-source/source/webpack.config.js b/test/hotCases/asset-modules-source/source/webpack.config.js new file mode 100644 index 000000000..25951fef2 --- /dev/null +++ b/test/hotCases/asset-modules-source/source/webpack.config.js @@ -0,0 +1,21 @@ +/** @type {import("../../../../").Configuration} */ +module.exports = { + mode: "development", + devtool: false, + optimization: { + realContentHash: true + }, + module: { + generator: { + asset: { + filename: "assets/[name].[contenthash][ext]" + } + }, + rules: [ + { + test: /file\.text$/, + type: "asset/resource" + } + ] + } +};