Content-Length: 464146 | pFad | https://github.com/webpack/webpack/commit/75d185d27ecc0a73a7a743bc15dc734676da372c

2E feat: pass `output.hash*` options to loader context · webpack/webpack@75d185d · GitHub
Skip to content

Commit 75d185d

Browse files
feat: pass output.hash* options to loader context
1 parent 981943d commit 75d185d

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

declarations/LoaderContext.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import type {
1414
ImportModuleOptions
1515
} from "../lib/dependencies/LoaderPlugin";
1616
import type { Resolver } from "enhanced-resolve";
17-
import type { Environment } from "./WebpackOptions";
17+
import type {
18+
Environment,
19+
HashDigestLength,
20+
HashSalt,
21+
HashDigest,
22+
HashFunction
23+
} from "./WebpackOptions";
1824

1925
type ResolveCallback = Parameters<Resolver["resolve"]>[4];
2026
type Schema = Parameters<typeof validate>[0];
@@ -49,6 +55,10 @@ export interface NormalModuleLoaderContext<OptionsType> {
4955
sourceMap?: boolean;
5056
mode: "development" | "production" | "none";
5157
webpack?: boolean;
58+
hashFunction: HashFunction,
59+
hashDigest: HashDigest,
60+
hashDigestLength: HashDigestLength,
61+
hashSalt: HashSalt,
5262
_module?: NormalModule;
5363
_compilation?: Compilation;
5464
_compiler?: Compiler;

lib/NormalModule.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,10 @@ class NormalModule extends Module {
777777
webpack: true,
778778
sourceMap: Boolean(this.useSourceMap),
779779
mode: options.mode || "production",
780+
hashFunction: options.output.hashFunction,
781+
hashDigest: options.output.hashDigest,
782+
hashDigestLength: options.output.hashDigestLength,
783+
hashSalt: options.output.hashSalt,
780784
_module: this,
781785
_compilation: compilation,
782786
_compiler: compilation.compiler,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
it("should have hmr flag in loader context", function() {
2+
expect(require("./loader!")).toMatchObject({
3+
digest: "a0fdc3d2f3863f64d95950fc06af72f7",
4+
digestWithLength: "a0fdc3d2f3863f64d959",
5+
hashFunction: "md4",
6+
hashDigest: "hex",
7+
hashDigestLength: 20,
8+
hashSalt: "salt",
9+
});
10+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** @type {import("../../../../").LoaderDefinition}} */
2+
module.exports = function () {
3+
const hashValue = this.utils.createHash(this.hashFunction);
4+
hashValue.update(this.hashSalt);
5+
hashValue.update("test");
6+
const digest = hashValue.digest(this.hashDigest);
7+
8+
return `module.exports = ${JSON.stringify({
9+
digest,
10+
digestWithLength: digest.slice(0, this.hashDigestLength),
11+
hashFunction: this.hashFunction,
12+
hashDigest: this.hashDigest,
13+
hashDigestLength: this.hashDigestLength,
14+
hashSalt: this.hashSalt
15+
})};`;
16+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import("../../../../").Configuration[]} */
2+
module.exports = {
3+
output: {
4+
hashSalt: "salt"
5+
}
6+
};

types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5205,6 +5205,7 @@ declare class Hash {
52055205
*/
52065206
digest(encoding?: string): string | Buffer;
52075207
}
5208+
type HashFunction = string | typeof Hash;
52085209
declare interface HashableObject {
52095210
updateHash: (arg0: Hash) => void;
52105211
}
@@ -9380,6 +9381,10 @@ declare interface NormalModuleLoaderContext<OptionsType> {
93809381
sourceMap?: boolean;
93819382
mode: "none" | "development" | "production";
93829383
webpack?: boolean;
9384+
hashFunction: HashFunction;
9385+
hashDigest: string;
9386+
hashDigestLength: number;
9387+
hashSalt: string;
93839388
_module?: NormalModule;
93849389
_compilation?: Compilation;
93859390
_compiler?: Compiler;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/webpack/webpack/commit/75d185d27ecc0a73a7a743bc15dc734676da372c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy