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;
|
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
|
* @private
|
||||||
* @param {NormalModule} module module for which the code should be generated
|
* @param {NormalModule} module module for which the code should be generated
|
||||||
|
@ -519,67 +483,52 @@ class AssetGenerator extends Generator {
|
||||||
(module.buildInfo).dataUrl &&
|
(module.buildInfo).dataUrl &&
|
||||||
needContent
|
needContent
|
||||||
) {
|
) {
|
||||||
if (data && data.has("url") && data.get("url")[type] !== undefined) {
|
const encodedSource = this.generateDataUri(module);
|
||||||
content = data.get("url")[type];
|
content =
|
||||||
} else {
|
type === "javascript" ? JSON.stringify(encodedSource) : encodedSource;
|
||||||
const encodedSource = this.generateDataUri(module);
|
|
||||||
content =
|
|
||||||
type === "javascript" ? JSON.stringify(encodedSource) : encodedSource;
|
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
data.set("url", { [type]: content });
|
data.set("url", { [type]: content, ...data.get("url") });
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/** @type {string} */
|
const hash = createHash(
|
||||||
let fullHash;
|
/** @type {Algorithm} */
|
||||||
|
(runtimeTemplate.outputOptions.hashFunction)
|
||||||
|
);
|
||||||
|
|
||||||
if (data && data.has("fullContentHash")) {
|
if (runtimeTemplate.outputOptions.hashSalt) {
|
||||||
fullHash = data.get("fullContentHash");
|
hash.update(runtimeTemplate.outputOptions.hashSalt);
|
||||||
} else {
|
}
|
||||||
fullHash = this._getFullContentHash(module, generateContext);
|
|
||||||
|
|
||||||
if (data) {
|
hash.update(/** @type {Source} */ (module.originalSource()).buffer());
|
||||||
data.set("fullContentHash", fullHash);
|
|
||||||
}
|
const fullHash =
|
||||||
|
/** @type {string} */
|
||||||
|
(hash.digest(runtimeTemplate.outputOptions.hashDigest));
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
data.set("fullContentHash", fullHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {BuildInfo} */
|
/** @type {BuildInfo} */
|
||||||
(module.buildInfo).fullContentHash = fullHash;
|
(module.buildInfo).fullContentHash = fullHash;
|
||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
let contentHash;
|
const contentHash = nonNumericOnlyHash(
|
||||||
|
fullHash,
|
||||||
|
/** @type {number} */
|
||||||
|
(generateContext.runtimeTemplate.outputOptions.hashDigestLength)
|
||||||
|
);
|
||||||
|
|
||||||
if (data && data.has("contentHash")) {
|
if (data) {
|
||||||
contentHash = data.get("contentHash");
|
data.set("contentHash", contentHash);
|
||||||
} else {
|
|
||||||
contentHash = this._getContentHash(fullHash, generateContext);
|
|
||||||
|
|
||||||
if (data) {
|
|
||||||
data.set("contentHash", contentHash);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let originalFilename;
|
const { originalFilename, filename, assetInfo } =
|
||||||
let filename;
|
this._getFilenameWithInfo(module, generateContext, contentHash);
|
||||||
let assetInfo;
|
|
||||||
|
|
||||||
if (data && data.has("originalFilename")) {
|
if (data) {
|
||||||
originalFilename = data.get("originalFilename");
|
data.set("filename", filename);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo(
|
const { assetPath, assetInfo: newAssetInfo } = this._getAssetPathWithInfo(
|
||||||
|
@ -590,14 +539,12 @@ class AssetGenerator extends Generator {
|
||||||
contentHash
|
contentHash
|
||||||
);
|
);
|
||||||
|
|
||||||
assetInfo = newAssetInfo;
|
|
||||||
|
|
||||||
if (data && (type === "javascript" || type === "css-url")) {
|
if (data && (type === "javascript" || type === "css-url")) {
|
||||||
data.set("url", { [type]: assetPath, ...data.get("url") });
|
data.set("url", { [type]: assetPath, ...data.get("url") });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data) {
|
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
|
// 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;
|
(module.buildInfo).filename = filename;
|
||||||
|
|
||||||
/** @type {BuildInfo} */
|
/** @type {BuildInfo} */
|
||||||
(module.buildInfo).assetInfo = assetInfo;
|
(module.buildInfo).assetInfo = newAssetInfo;
|
||||||
|
|
||||||
content = assetPath;
|
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