Skip to content

Commit

Permalink
feat: pass output.hash* options to loader context
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 20, 2024
1 parent 981943d commit 75d185d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
12 changes: 11 additions & 1 deletion declarations/LoaderContext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import type {
ImportModuleOptions
} from "../lib/dependencies/LoaderPlugin";
import type { Resolver } from "enhanced-resolve";
import type { Environment } from "./WebpackOptions";
import type {
Environment,
HashDigestLength,
HashSalt,
HashDigest,
HashFunction
} from "./WebpackOptions";

type ResolveCallback = Parameters<Resolver["resolve"]>[4];
type Schema = Parameters<typeof validate>[0];
Expand Down Expand Up @@ -49,6 +55,10 @@ export interface NormalModuleLoaderContext<OptionsType> {
sourceMap?: boolean;
mode: "development" | "production" | "none";
webpack?: boolean;
hashFunction: HashFunction,
hashDigest: HashDigest,
hashDigestLength: HashDigestLength,
hashSalt: HashSalt,
_module?: NormalModule;
_compilation?: Compilation;
_compiler?: Compiler;
Expand Down
4 changes: 4 additions & 0 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,10 @@ class NormalModule extends Module {
webpack: true,
sourceMap: Boolean(this.useSourceMap),
mode: options.mode || "production",
hashFunction: options.output.hashFunction,
hashDigest: options.output.hashDigest,
hashDigestLength: options.output.hashDigestLength,
hashSalt: options.output.hashSalt,
_module: this,
_compilation: compilation,
_compiler: compilation.compiler,
Expand Down
10 changes: 10 additions & 0 deletions test/configCases/loaders/hash-in-context/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it("should have hmr flag in loader context", function() {
expect(require("./loader!")).toMatchObject({
digest: "a0fdc3d2f3863f64d95950fc06af72f7",
digestWithLength: "a0fdc3d2f3863f64d959",
hashFunction: "md4",
hashDigest: "hex",
hashDigestLength: 20,
hashSalt: "salt",
});
});
16 changes: 16 additions & 0 deletions test/configCases/loaders/hash-in-context/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import("../../../../").LoaderDefinition}} */
module.exports = function () {
const hashValue = this.utils.createHash(this.hashFunction);
hashValue.update(this.hashSalt);
hashValue.update("test");
const digest = hashValue.digest(this.hashDigest);

return `module.exports = ${JSON.stringify({
digest,
digestWithLength: digest.slice(0, this.hashDigestLength),
hashFunction: this.hashFunction,
hashDigest: this.hashDigest,
hashDigestLength: this.hashDigestLength,
hashSalt: this.hashSalt
})};`;
};
6 changes: 6 additions & 0 deletions test/configCases/loaders/hash-in-context/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../../").Configuration[]} */
module.exports = {
output: {
hashSalt: "salt"
}
};
5 changes: 5 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5205,6 +5205,7 @@ declare class Hash {
*/
digest(encoding?: string): string | Buffer;
}
type HashFunction = string | typeof Hash;
declare interface HashableObject {
updateHash: (arg0: Hash) => void;
}
Expand Down Expand Up @@ -9380,6 +9381,10 @@ declare interface NormalModuleLoaderContext<OptionsType> {
sourceMap?: boolean;
mode: "none" | "development" | "production";
webpack?: boolean;
hashFunction: HashFunction;
hashDigest: string;
hashDigestLength: number;
hashSalt: string;
_module?: NormalModule;
_compilation?: Compilation;
_compiler?: Compiler;
Expand Down

0 comments on commit 75d185d

Please sign in to comment.