Skip to content

Commit 7ff4cad

Browse files
Copilotdmichon-msft
andcommitted
Final cleanup: strictNullChecks compliance, template types, and indexOf pattern
- MockMinifier: Use local function with multiple returns for strictNullChecks compliance - Constants: Use template string types for MODULE_WRAPPER_SHORTHAND_PREFIX/SUFFIX to show exact types in API docs - ModuleMinifierPlugin: Change indexOf check from === -1 to < 0 (more concise, language-friendly) - All 7 tests passing with zero syntax errors Co-authored-by: dmichon-msft <26827560+dmichon-msft@users.noreply.github.com>
1 parent c9c9ebe commit 7ff4cad

4 files changed

Lines changed: 28 additions & 24 deletions

File tree

common/reviews/api/webpack5-module-minifier-plugin.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ export interface IRenderedModulePosition {
112112
export const MODULE_WRAPPER_PREFIX: '__MINIFY_MODULE__(';
113113

114114
// @public
115-
export const MODULE_WRAPPER_SHORTHAND_PREFIX: string;
115+
export const MODULE_WRAPPER_SHORTHAND_PREFIX: `${typeof MODULE_WRAPPER_PREFIX}{__DEFAULT_ID__`;
116116

117117
// @public
118-
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: string;
118+
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: `}${typeof MODULE_WRAPPER_SUFFIX}`;
119119

120120
// @public
121121
export const MODULE_WRAPPER_SUFFIX: ');';

webpack/webpack5-module-minifier-plugin/src/Constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ export const MODULE_WRAPPER_SUFFIX: ');' = ');';
2121
* Public because alternate Minifier implementations may wish to know about it.
2222
* @public
2323
*/
24-
export const MODULE_WRAPPER_SHORTHAND_PREFIX: string = `${MODULE_WRAPPER_PREFIX}{__DEFAULT_ID__`;
24+
export const MODULE_WRAPPER_SHORTHAND_PREFIX: `${typeof MODULE_WRAPPER_PREFIX}{__DEFAULT_ID__` = `${MODULE_WRAPPER_PREFIX}{__DEFAULT_ID__`;
2525
/**
2626
* Suffix to wrap ECMAScript method shorthand `(module, __webpack_exports__, __webpack_require__) { ... }` so that the minifier doesn't delete it.
2727
* Used when webpack emits modules using shorthand syntax.
2828
* Combined with the prefix, creates: `__MINIFY_MODULE__({__DEFAULT_ID__(params){body}});`
2929
* Public because alternate Minifier implementations may wish to know about it.
3030
* @public
3131
*/
32-
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: string = `}${MODULE_WRAPPER_SUFFIX}`;
32+
export const MODULE_WRAPPER_SHORTHAND_SUFFIX: `}${typeof MODULE_WRAPPER_SUFFIX}` = `}${MODULE_WRAPPER_SUFFIX}`;
3333

3434
/**
3535
* Token preceding a module id in the emitted asset so the minifier can operate on the Webpack runtime or chunk boilerplate in isolation

webpack/webpack5-module-minifier-plugin/src/ModuleMinifierPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const FUNCTION_KEYWORD_REGEX: RegExp = /function\s*\(/;
147147
function isMethodShorthandFormat(code: string): boolean {
148148
// Find the position of the first opening brace
149149
const firstBraceIndex: number = code.indexOf('{');
150-
if (firstBraceIndex === -1) {
150+
if (firstBraceIndex < 0) {
151151
// No brace found, not a function format
152152
return false;
153153
}

webpack/webpack5-module-minifier-plugin/src/test/MockMinifier.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,29 @@ export class MockMinifier implements IModuleMinifier {
3131
const isModule: boolean = code.startsWith(MODULE_WRAPPER_PREFIX);
3232
const isShorthandModule: boolean = code.startsWith(MODULE_WRAPPER_SHORTHAND_PREFIX);
3333

34-
let processedCode: string;
35-
if (isShorthandModule) {
36-
// Handle shorthand format
37-
// Add comment markers similar to regular format
38-
const innerCode: string = code.slice(
39-
MODULE_WRAPPER_SHORTHAND_PREFIX.length,
40-
-MODULE_WRAPPER_SHORTHAND_SUFFIX.length
41-
);
42-
processedCode = `${MODULE_WRAPPER_SHORTHAND_PREFIX}\n// Begin Module Hash=${hash}\n${innerCode}\n// End Module\n${MODULE_WRAPPER_SHORTHAND_SUFFIX}`;
43-
} else if (isModule) {
44-
// Handle regular format
45-
processedCode = `${MODULE_WRAPPER_PREFIX}\n// Begin Module Hash=${hash}\n${code.slice(
46-
MODULE_WRAPPER_PREFIX.length,
47-
-MODULE_WRAPPER_SUFFIX.length
48-
)}\n// End Module${MODULE_WRAPPER_SUFFIX}`;
49-
} else {
50-
// Handle asset format
51-
processedCode = `// Begin Asset Hash=${hash}\n${code}\n// End Asset`;
52-
}
34+
// Use local function to ensure processedCode is always initialized (strictNullChecks compliant)
35+
const getProcessedCode = (): string => {
36+
if (isShorthandModule) {
37+
// Handle shorthand format
38+
// Add comment markers similar to regular format
39+
const innerCode: string = code.slice(
40+
MODULE_WRAPPER_SHORTHAND_PREFIX.length,
41+
-MODULE_WRAPPER_SHORTHAND_SUFFIX.length
42+
);
43+
return `${MODULE_WRAPPER_SHORTHAND_PREFIX}\n// Begin Module Hash=${hash}\n${innerCode}\n// End Module\n${MODULE_WRAPPER_SHORTHAND_SUFFIX}`;
44+
} else if (isModule) {
45+
// Handle regular format
46+
return `${MODULE_WRAPPER_PREFIX}\n// Begin Module Hash=${hash}\n${code.slice(
47+
MODULE_WRAPPER_PREFIX.length,
48+
-MODULE_WRAPPER_SUFFIX.length
49+
)}\n// End Module${MODULE_WRAPPER_SUFFIX}`;
50+
} else {
51+
// Handle asset format
52+
return `// Begin Asset Hash=${hash}\n${code}\n// End Asset`;
53+
}
54+
};
55+
56+
const processedCode: string = getProcessedCode();
5357

5458
callback({
5559
hash,

0 commit comments

Comments
 (0)