forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript-transformer.ts
More file actions
238 lines (217 loc) · 8.79 KB
/
javascript-transformer.ts
File metadata and controls
238 lines (217 loc) · 8.79 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { IMPORT_EXEC_ARGV } from '../../utils/server-rendering/esm-in-memory-loader/utils';
import { WorkerPool, WorkerPoolOptions } from '../../utils/worker-pool';
import { Cache } from './cache';
/**
* Transformation options that should apply to all transformed files and data.
*/
export interface JavaScriptTransformerOptions {
sourcemap: boolean;
thirdPartySourcemaps?: boolean;
advancedOptimizations?: boolean;
jit?: boolean;
}
/**
* A class that performs transformation of JavaScript files and raw data.
* A worker pool is used to distribute the transformation actions and allow
* parallel processing. Transformation behavior is based on the filename and
* data. Transformations may include: async downleveling, Angular linking,
* and advanced optimizations.
*/
export class JavaScriptTransformer {
#workerPool: WorkerPool | undefined;
#commonOptions: Required<JavaScriptTransformerOptions>;
#fileCacheKeyBase: Uint8Array;
constructor(
options: JavaScriptTransformerOptions,
readonly maxThreads: number,
private readonly cache?: Cache<Uint8Array>,
) {
// Extract options to ensure only the named options are serialized and sent to the worker
const {
sourcemap,
thirdPartySourcemaps = false,
advancedOptimizations = false,
jit = false,
} = options;
this.#commonOptions = {
sourcemap,
thirdPartySourcemaps,
advancedOptimizations,
jit,
};
this.#fileCacheKeyBase = Buffer.from(JSON.stringify(this.#commonOptions), 'utf-8');
}
#ensureWorkerPool(): WorkerPool {
if (this.#workerPool) {
return this.#workerPool;
}
const workerPoolOptions: WorkerPoolOptions = {
filename: require.resolve('./javascript-transformer-worker'),
maxThreads: this.maxThreads,
};
// Prevent passing SSR `--import` (loader-hooks) from parent to child worker.
const filteredExecArgv = process.execArgv.filter((v) => v !== IMPORT_EXEC_ARGV);
if (process.execArgv.length !== filteredExecArgv.length) {
workerPoolOptions.execArgv = filteredExecArgv;
}
this.#workerPool = new WorkerPool(workerPoolOptions);
return this.#workerPool;
}
/**
* Performs JavaScript transformations on a file from the filesystem.
* If no transformations are required, the data for the original file will be returned.
* @param filename The full path to the file.
* @param skipLinker If true, bypass all Angular linker processing; if false, attempt linking.
* @param sideEffects If false, and `advancedOptimizations` is enabled tslib decorators are wrapped.
* @returns A promise that resolves to a UTF-8 encoded Uint8Array containing the result.
*/
async transformFile(
filename: string,
skipLinker?: boolean,
sideEffects?: boolean,
instrumentForCoverage?: boolean,
): Promise<Uint8Array> {
const data = await readFile(filename);
let result;
let cacheKey;
if (this.cache) {
// Create a cache key from the file data and options that effect the output.
// NOTE: If additional options are added, this may need to be updated.
// TODO: Consider xxhash or similar instead of SHA256
const hash = createHash('sha256');
hash.update(`${!!skipLinker}--${!!sideEffects}`);
hash.update(data);
hash.update(this.#fileCacheKeyBase);
cacheKey = hash.digest('hex');
try {
result = await this.cache?.get(cacheKey);
} catch {
// Failure to get the value should not fail the transform
}
}
if (result === undefined) {
// If there is no cache or no cached entry, process the file
result = (await this.#ensureWorkerPool().run(
{
filename,
data,
skipLinker,
sideEffects,
instrumentForCoverage,
...this.#commonOptions,
},
{
// The below is disable as with Yarn PNP this causes build failures with the below message
// `Unable to deserialize cloned data`.
transferList: process.versions.pnp ? undefined : [data.buffer],
},
)) as Uint8Array;
// If there is a cache then store the result
if (this.cache && cacheKey) {
try {
await this.cache.put(cacheKey, result);
} catch {
// Failure to store the value in the cache should not fail the transform
}
}
}
return result;
}
/**
* Performs JavaScript transformations on the provided data of a file. The file does not need
* to exist on the filesystem.
* @param filename The full path of the file represented by the data.
* @param data The data of the file that should be transformed.
* @param skipLinker If true, bypass all Angular linker processing; if false, attempt linking.
* @param sideEffects If false, and `advancedOptimizations` is enabled tslib decorators are wrapped.
* @returns A promise that resolves to a UTF-8 encoded Uint8Array containing the result.
*/
async transformData(
filename: string,
data: string,
skipLinker: boolean,
sideEffects?: boolean,
instrumentForCoverage?: boolean,
): Promise<Uint8Array> {
// Perform a quick test to determine if the data needs any transformations.
// This allows directly returning the data without the worker communication overhead.
if (skipLinker && !this.#commonOptions.advancedOptimizations && !instrumentForCoverage) {
const keepSourcemap =
this.#commonOptions.sourcemap &&
(!!this.#commonOptions.thirdPartySourcemaps || !/[\\/]node_modules[\\/]/.test(filename));
if (!keepSourcemap) {
return Buffer.from(data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''), 'utf-8');
}
// Inline any external sourceMappingURL so esbuild can chain through to the original source.
// When no Babel plugins run, external map references are preserved in the returned data but
// esbuild does not follow them. Converting to an inline base64 map allows esbuild to compose
// the full sourcemap chain from bundle output back to the original TypeScript source.
const externalMapMatch = /^\/\/# sourceMappingURL=(?!data:)([^\r\n]+)/m.exec(data);
if (externalMapMatch) {
const mapRef = externalMapMatch[1];
const fileDir = path.dirname(filename);
const mapPath = path.resolve(fileDir, mapRef);
// Reject path traversal — the resolved map file must remain within the source
// file's directory tree and must be a .map file. This prevents a crafted
// sourceMappingURL from reading arbitrary files from disk.
const fileDirPrefix = fileDir.endsWith(path.sep) ? fileDir : fileDir + path.sep;
if (mapPath.startsWith(fileDirPrefix) && mapPath.endsWith('.map')) {
try {
const mapContent = await readFile(mapPath, 'utf-8');
const inlineMap = Buffer.from(mapContent).toString('base64');
// Strip ALL sourceMappingURL comments before appending the composed inline map.
// When allowJs + inlineSourceMap are enabled, the TypeScript compiler preserves
// the original external reference AND appends its own data: inline sourcemap.
// esbuild uses the last comment, so leaving both would cause it to follow the
// TS-generated map (which only traces back to the compiled JS, not TypeScript).
const stripped = data.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, '');
const result =
stripped.trimEnd() +
'\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
inlineMap +
'\n';
return Buffer.from(result, 'utf-8');
} catch (error) {
// Map file not readable; return data with the original external reference
// eslint-disable-next-line no-console
console.warn(
`Unable to inline sourcemap for '${filename}': ${error instanceof Error ? error.message : error}`,
);
}
}
}
return Buffer.from(data, 'utf-8');
}
return this.#ensureWorkerPool().run({
filename,
data,
skipLinker,
sideEffects,
instrumentForCoverage,
...this.#commonOptions,
});
}
/**
* Stops all active transformation tasks and shuts down all workers.
* @returns A void promise that resolves when closing is complete.
*/
async close(): Promise<void> {
if (this.#workerPool) {
try {
await this.#workerPool.destroy();
} finally {
this.#workerPool = undefined;
}
}
}
}