mirror of https://github.com/webpack/webpack.git
fix: logic for assets
This commit is contained in:
parent
0ba37443e6
commit
8c43febb2f
|
@ -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;
|
||||
|
||||
if (data) {
|
||||
data.set("url", { [type]: content });
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (data && data.has("contentHash")) {
|
||||
contentHash = data.get("contentHash");
|
||||
} else {
|
||||
contentHash = this._getContentHash(fullHash, generateContext);
|
||||
const contentHash = nonNumericOnlyHash(
|
||||
fullHash,
|
||||
/** @type {number} */
|
||||
(generateContext.runtimeTemplate.outputOptions.hashDigestLength)
|
||||
);
|
||||
|
||||
if (data) {
|
||||
data.set("contentHash", contentHash);
|
||||
}
|
||||
}
|
||||
|
||||
let originalFilename;
|
||||
let filename;
|
||||
let assetInfo;
|
||||
|
||||
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
|
||||
));
|
||||
const { originalFilename, filename, assetInfo } =
|
||||
this._getFilenameWithInfo(module, generateContext, contentHash);
|
||||
|
||||
if (data) {
|
||||
data.set("filename", filename);
|
||||
data.set("assetInfo", assetInfo);
|
||||
data.set("filenameWithInfo", originalFilename);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
A
|
||||
---
|
||||
B
|
|
@ -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));
|
||||
});
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue