Skip to content

add allowNumericOnlyHash option for asset/resource #17903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add allowNumericOnlyHash option for asset/resource
  • Loading branch information
makaria committed Dec 28, 2023
commit 8c3814bd4b8340e4d27b40c61ea40f7c7214315b
6 changes: 6 additions & 0 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2802,6 +2802,12 @@ export interface AssetResourceGeneratorOptions {
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: RawPublicPath;
/**
* when false, if hash has only numeric char, the returned hash will be prefixed with character `a` to make it has at least one non numeric char.
* when true, the returned hash may not contain any non-numeric characters.
* default false.
*/
allowNumericOnlyHash?: boolean;
}
/**
* Options for css handling.
Expand Down
27 changes: 22 additions & 5 deletions lib/asset/AssetGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,23 @@ class AssetGenerator extends Generator {
* @param {RawPublicPath=} publicPath override for output.assetModulePublicPath
* @param {AssetModuleOutputPath=} outputPath the output path for the emitted file which is not included in the runtime import
* @param {boolean=} emit generate output asset
* @param {boolean=} allowNumericOnlyHash allow numeric only hash, disable nonNumericOnlyHash
*/
constructor(dataUrlOptions, filename, publicPath, outputPath, emit) {
constructor(
dataUrlOptions,
filename,
publicPath,
outputPath,
emit,
allowNumericOnlyHash
) {
super();
this.dataUrlOptions = dataUrlOptions;
this.filename = filename;
this.publicPath = publicPath;
this.outputPath = outputPath;
this.emit = emit;
this.allowNumericOnlyHash = allowNumericOnlyHash;
}

/**
Expand Down Expand Up @@ -300,10 +309,18 @@ class AssetGenerator extends Generator {
const fullHash = /** @type {string} */ (
hash.digest(runtimeTemplate.outputOptions.hashDigest)
);
const contentHash = nonNumericOnlyHash(
fullHash,
runtimeTemplate.outputOptions.hashDigestLength
);
let contentHash = fullHash;
if (this.allowNumericOnlyHash) {
contentHash = fullHash.slice(
0,
runtimeTemplate.outputOptions.hashDigestLength
);
} else {
contentHash = nonNumericOnlyHash(
fullHash,
runtimeTemplate.outputOptions.hashDigestLength
);
}
module.buildInfo.fullContentHash = fullHash;
const sourceFilename = this.getSourceFileName(
module,
Expand Down
3 changes: 2 additions & 1 deletion lib/asset/AssetModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ class AssetModulesPlugin {
filename,
publicPath,
outputPath,
generatorOptions.emit !== false
generatorOptions.emit !== false,
generatorOptions.allowNumericOnlyHash === true
);
});
}
Expand Down
4 changes: 4 additions & 0 deletions schemas/WebpackOptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@
},
"publicPath": {
"$ref": "#/definitions/RawPublicPath"
},
"allowNumericOnlyHash": {
"description": "Hash may not contain any non-numeric characters when true. Hash may prefixed with character 'a' when false.",
"type": "boolean"
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ declare interface AssetResourceGeneratorOptions {
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
*/
publicPath?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);

/**
* when false, if hash has only numeric char, the returned hash will be prefixed with character `a` to make it has at least one non numeric char.
* when true, the returned hash may not contain any non-numeric characters.
* default false.
*/
allowNumericOnlyHash?: boolean;
}
declare class AsyncDependenciesBlock extends DependenciesBlock {
constructor(
Expand Down