-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathMockMinifier.ts
More file actions
97 lines (85 loc) · 3.03 KB
/
Copy pathMockMinifier.ts
File metadata and controls
97 lines (85 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type {
IModuleMinifier,
IModuleMinificationRequest,
IModuleMinificationCallback,
IMinifierConnection
} from '@rushstack/module-minifier';
import {
MODULE_WRAPPER_PREFIX,
MODULE_WRAPPER_SUFFIX,
MODULE_WRAPPER_SHORTHAND_PREFIX,
MODULE_WRAPPER_SHORTHAND_SUFFIX
} from '../Constants';
export class MockMinifier implements IModuleMinifier {
public readonly requests: Map<string, string> = new Map();
/**
* Mock code transform.
* @param request - The request to process
* @param callback - The callback to invoke
*/
public minify(request: IModuleMinificationRequest, callback: IModuleMinificationCallback): void {
const { code, hash, nameForMap } = request;
this.requests.set(hash, code);
const isModule: boolean = code.startsWith(MODULE_WRAPPER_PREFIX);
const isShorthandModule: boolean = code.startsWith(MODULE_WRAPPER_SHORTHAND_PREFIX);
// Use local function to ensure processedCode is always initialized (strictNullChecks compliant)
const getProcessedCode = (): string => {
if (isShorthandModule) {
// Handle shorthand format
// Add comment markers similar to regular format
const innerCode: string = code.slice(
MODULE_WRAPPER_SHORTHAND_PREFIX.length,
-MODULE_WRAPPER_SHORTHAND_SUFFIX.length
);
return `${MODULE_WRAPPER_SHORTHAND_PREFIX}\n// Begin Module Hash=${hash}\n${innerCode}\n// End Module\n${MODULE_WRAPPER_SHORTHAND_SUFFIX}`;
} else if (isModule) {
// Handle regular format
return `${MODULE_WRAPPER_PREFIX}\n// Begin Module Hash=${hash}\n${code.slice(
MODULE_WRAPPER_PREFIX.length,
-MODULE_WRAPPER_SUFFIX.length
)}\n// End Module${MODULE_WRAPPER_SUFFIX}`;
} else {
// Handle asset format
return `// Begin Asset Hash=${hash}\n${code}\n// End Asset`;
}
};
const processedCode: string = getProcessedCode();
callback({
hash,
error: undefined,
code: processedCode,
// If source maps are requested, provide an empty source map
map: nameForMap
? {
version: 3,
names: [],
file: nameForMap,
sources: [nameForMap],
sourcesContent: [code],
// In source mapping parlance, this means "map line 0, column 0 to the input file at index 0, line 0, column 0"
mappings: 'AAAA'
}
: undefined
});
}
// eslint-disable-next-line @typescript-eslint/naming-convention
public async connect(): Promise<IMinifierConnection> {
throw new Error('Not implemented.');
}
/**
* {@inheritdoc}
*/
public async connectAsync(): Promise<IMinifierConnection> {
return {
configHash: MockMinifier.name,
disconnectAsync: async () => {
// Do nothing.
},
disconnect: () => {
throw new Error('Method not implemented.');
}
};
}
}