Skip to content

Commit 8c3814b

Browse files
committed
add allowNumericOnlyHash option for asset/resource
1 parent e518d0d commit 8c3814b

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,6 +2802,12 @@ export interface AssetResourceGeneratorOptions {
28022802
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
28032803
*/
28042804
publicPath?: RawPublicPath;
2805+
/**
2806+
* 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.
2807+
* when true, the returned hash may not contain any non-numeric characters.
2808+
* default false.
2809+
*/
2810+
allowNumericOnlyHash?: boolean;
28052811
}
28062812
/**
28072813
* Options for css handling.

lib/asset/AssetGenerator.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,23 @@ class AssetGenerator extends Generator {
138138
* @param {RawPublicPath=} publicPath override for output.assetModulePublicPath
139139
* @param {AssetModuleOutputPath=} outputPath the output path for the emitted file which is not included in the runtime import
140140
* @param {boolean=} emit generate output asset
141+
* @param {boolean=} allowNumericOnlyHash allow numeric only hash, disable nonNumericOnlyHash
141142
*/
142-
constructor(dataUrlOptions, filename, publicPath, outputPath, emit) {
143+
constructor(
144+
dataUrlOptions,
145+
filename,
146+
publicPath,
147+
outputPath,
148+
emit,
149+
allowNumericOnlyHash
150+
) {
143151
super();
144152
this.dataUrlOptions = dataUrlOptions;
145153
this.filename = filename;
146154
this.publicPath = publicPath;
147155
this.outputPath = outputPath;
148156
this.emit = emit;
157+
this.allowNumericOnlyHash = allowNumericOnlyHash;
149158
}
150159

151160
/**
@@ -300,10 +309,18 @@ class AssetGenerator extends Generator {
300309
const fullHash = /** @type {string} */ (
301310
hash.digest(runtimeTemplate.outputOptions.hashDigest)
302311
);
303-
const contentHash = nonNumericOnlyHash(
304-
fullHash,
305-
runtimeTemplate.outputOptions.hashDigestLength
306-
);
312+
let contentHash = fullHash;
313+
if (this.allowNumericOnlyHash) {
314+
contentHash = fullHash.slice(
315+
0,
316+
runtimeTemplate.outputOptions.hashDigestLength
317+
);
318+
} else {
319+
contentHash = nonNumericOnlyHash(
320+
fullHash,
321+
runtimeTemplate.outputOptions.hashDigestLength
322+
);
323+
}
307324
module.buildInfo.fullContentHash = fullHash;
308325
const sourceFilename = this.getSourceFileName(
309326
module,

lib/asset/AssetModulesPlugin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ class AssetModulesPlugin {
165165
filename,
166166
publicPath,
167167
outputPath,
168-
generatorOptions.emit !== false
168+
generatorOptions.emit !== false,
169+
generatorOptions.allowNumericOnlyHash === true
169170
);
170171
});
171172
}

schemas/WebpackOptions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@
208208
},
209209
"publicPath": {
210210
"$ref": "#/definitions/RawPublicPath"
211+
},
212+
"allowNumericOnlyHash": {
213+
"description": "Hash may not contain any non-numeric characters when true. Hash may prefixed with character 'a' when false.",
214+
"type": "boolean"
211215
}
212216
}
213217
},

types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ declare interface AssetResourceGeneratorOptions {
348348
* The 'publicPath' specifies the public URL address of the output files when referenced in a browser.
349349
*/
350350
publicPath?: string | ((pathData: PathData, assetInfo?: AssetInfo) => string);
351+
352+
/**
353+
* 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.
354+
* when true, the returned hash may not contain any non-numeric characters.
355+
* default false.
356+
*/
357+
allowNumericOnlyHash?: boolean;
351358
}
352359
declare class AsyncDependenciesBlock extends DependenciesBlock {
353360
constructor(

0 commit comments

Comments
 (0)