-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathcompiler-plugin.ts
433 lines (374 loc) · 15.4 KB
/
compiler-plugin.ts
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* @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.io/license
*/
import type {
OnStartResult,
OutputFile,
PartialMessage,
PartialNote,
Plugin,
PluginBuild,
} from 'esbuild';
import * as assert from 'node:assert';
import { platform } from 'node:os';
import * as path from 'node:path';
import { pathToFileURL } from 'node:url';
import ts from 'typescript';
import { maxWorkers } from '../../utils/environment-options';
import { AngularCompilation, FileEmitter } from './angular-compilation';
import { AngularHostOptions } from './angular-host';
import { JavaScriptTransformer } from './javascript-transformer';
import {
logCumulativeDurations,
profileAsync,
profileSync,
resetCumulativeDurations,
} from './profiling';
import { BundleStylesheetOptions, bundleComponentStylesheet } from './stylesheets';
/**
* A counter for component styles used to generate unique build-time identifiers for each stylesheet.
*/
let componentStyleCounter = 0;
/**
* Converts TypeScript Diagnostic related information into an esbuild compatible note object.
* Related information is a subset of a full TypeScript Diagnostic and also used for diagnostic
* notes associated with the main Diagnostic.
* @param info The TypeScript diagnostic relative information to convert.
* @returns An esbuild diagnostic message as a PartialMessage object
*/
function convertTypeScriptDiagnosticInfo(
info: ts.DiagnosticRelatedInformation,
textPrefix?: string,
): PartialNote {
const newLine = platform() === 'win32' ? '\r\n' : '\n';
let text = ts.flattenDiagnosticMessageText(info.messageText, newLine);
if (textPrefix) {
text = textPrefix + text;
}
const note: PartialNote = { text };
if (info.file) {
note.location = {
file: info.file.fileName,
length: info.length,
};
// Calculate the line/column location and extract the full line text that has the diagnostic
if (info.start) {
const { line, character } = ts.getLineAndCharacterOfPosition(info.file, info.start);
note.location.line = line + 1;
note.location.column = character;
// The start position for the slice is the first character of the error line
const lineStartPosition = ts.getPositionOfLineAndCharacter(info.file, line, 0);
// The end position for the slice is the first character of the next line or the length of
// the entire file if the line is the last line of the file (getPositionOfLineAndCharacter
// will error if a nonexistent line is passed).
const { line: lastLineOfFile } = ts.getLineAndCharacterOfPosition(
info.file,
info.file.text.length - 1,
);
const lineEndPosition =
line < lastLineOfFile
? ts.getPositionOfLineAndCharacter(info.file, line + 1, 0)
: info.file.text.length;
note.location.lineText = info.file.text.slice(lineStartPosition, lineEndPosition).trimEnd();
}
}
return note;
}
/**
* Converts a TypeScript Diagnostic message into an esbuild compatible message object.
* @param diagnostic The TypeScript diagnostic to convert.
* @returns An esbuild diagnostic message as a PartialMessage object
*/
function convertTypeScriptDiagnostic(diagnostic: ts.Diagnostic): PartialMessage {
let codePrefix = 'TS';
let code = `${diagnostic.code}`;
if (diagnostic.source === 'ngtsc') {
codePrefix = 'NG';
// Remove `-99` Angular prefix from diagnostic code
code = code.slice(3);
}
const message: PartialMessage = {
...convertTypeScriptDiagnosticInfo(diagnostic, `${codePrefix}${code}: `),
// Store original diagnostic for reference if needed downstream
detail: diagnostic,
};
if (diagnostic.relatedInformation?.length) {
message.notes = diagnostic.relatedInformation.map((info) =>
convertTypeScriptDiagnosticInfo(info),
);
}
return message;
}
const USING_WINDOWS = platform() === 'win32';
const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g');
export class SourceFileCache extends Map<string, ts.SourceFile> {
readonly modifiedFiles = new Set<string>();
readonly babelFileCache = new Map<string, Uint8Array>();
readonly typeScriptFileCache = new Map<string, Uint8Array>();
invalidate(files: Iterable<string>): void {
this.modifiedFiles.clear();
for (let file of files) {
this.babelFileCache.delete(file);
this.typeScriptFileCache.delete(pathToFileURL(file).href);
// Normalize separators to allow matching TypeScript Host paths
if (USING_WINDOWS) {
file = file.replace(WINDOWS_SEP_REGEXP, path.posix.sep);
}
this.delete(file);
this.modifiedFiles.add(file);
}
}
}
export interface CompilerPluginOptions {
sourcemap: boolean;
tsconfig: string;
advancedOptimizations?: boolean;
thirdPartySourcemaps?: boolean;
fileReplacements?: Record<string, string>;
sourceFileCache?: SourceFileCache;
}
// eslint-disable-next-line max-lines-per-function
export function createCompilerPlugin(
pluginOptions: CompilerPluginOptions,
styleOptions: BundleStylesheetOptions & { inlineStyleLanguage: string },
): Plugin {
return {
name: 'angular-compiler',
// eslint-disable-next-line max-lines-per-function
async setup(build: PluginBuild): Promise<void> {
let setupWarnings: PartialMessage[] | undefined;
// Initialize a worker pool for JavaScript transformations
const javascriptTransformer = new JavaScriptTransformer(pluginOptions, maxWorkers);
const { GLOBAL_DEFS_FOR_TERSER_WITH_AOT, readConfiguration } =
await AngularCompilation.loadCompilerCli();
// Setup defines based on the values provided by the Angular compiler-cli
build.initialOptions.define ??= {};
for (const [key, value] of Object.entries(GLOBAL_DEFS_FOR_TERSER_WITH_AOT)) {
if (key in build.initialOptions.define) {
// Skip keys that have been manually provided
continue;
}
if (key === 'ngDevMode') {
// ngDevMode is already set based on the builder's script optimization option
continue;
}
// esbuild requires values to be a string (actual strings need to be quoted).
// In this case, all provided values are booleans.
build.initialOptions.define[key] = value.toString();
}
// The tsconfig is loaded in setup instead of in start to allow the esbuild target build option to be modified.
// esbuild build options can only be modified in setup prior to starting the build.
const {
options: compilerOptions,
rootNames,
errors: configurationDiagnostics,
} = profileSync('NG_READ_CONFIG', () =>
readConfiguration(pluginOptions.tsconfig, {
noEmitOnError: false,
suppressOutputPathCheck: true,
outDir: undefined,
inlineSources: pluginOptions.sourcemap,
inlineSourceMap: pluginOptions.sourcemap,
sourceMap: false,
mapRoot: undefined,
sourceRoot: undefined,
declaration: false,
declarationMap: false,
allowEmptyCodegenFiles: false,
annotationsAs: 'decorators',
enableResourceInlining: false,
}),
);
if (compilerOptions.target === undefined || compilerOptions.target < ts.ScriptTarget.ES2022) {
// If 'useDefineForClassFields' is already defined in the users project leave the value as is.
// Otherwise fallback to false due to https://github.com/microsoft/TypeScript/issues/45995
// which breaks the deprecated `@Effects` NGRX decorator and potentially other existing code as well.
compilerOptions.target = ts.ScriptTarget.ES2022;
compilerOptions.useDefineForClassFields ??= false;
(setupWarnings ??= []).push({
text:
'TypeScript compiler options "target" and "useDefineForClassFields" are set to "ES2022" and ' +
'"false" respectively by the Angular CLI.',
location: { file: pluginOptions.tsconfig },
notes: [
{
text:
'To control ECMA version and features use the Browerslist configuration. ' +
'For more information, see https://angular.io/guide/build#configuring-browser-compatibility',
},
],
});
}
// The file emitter created during `onStart` that will be used during the build in `onLoad` callbacks for TS files
let fileEmitter: FileEmitter | undefined;
// The stylesheet resources from component stylesheets that will be added to the build results output files
let stylesheetResourceFiles: OutputFile[];
let compilation: AngularCompilation | undefined;
build.onStart(async () => {
const result: OnStartResult = {
warnings: setupWarnings,
};
// Reset the setup warnings so that they are only shown during the first build.
setupWarnings = undefined;
// Reset debug performance tracking
resetCumulativeDurations();
// Reset stylesheet resource output files
stylesheetResourceFiles = [];
// Create Angular compiler host options
const hostOptions: AngularHostOptions = {
fileReplacements: pluginOptions.fileReplacements,
modifiedFiles: pluginOptions.sourceFileCache?.modifiedFiles,
sourceFileCache: pluginOptions.sourceFileCache,
async transformStylesheet(data, containingFile, stylesheetFile) {
// Stylesheet file only exists for external stylesheets
const filename = stylesheetFile ?? containingFile;
const stylesheetResult = await bundleComponentStylesheet(
// TODO: Evaluate usage of a fast hash instead
`${++componentStyleCounter}`,
styleOptions.inlineStyleLanguage,
data,
filename,
!stylesheetFile,
styleOptions,
);
const { contents, resourceFiles, errors, warnings } = stylesheetResult;
(result.errors ??= []).push(...errors);
(result.warnings ??= []).push(...warnings);
stylesheetResourceFiles.push(...resourceFiles);
return contents;
},
};
// Create new compilation if first build; otherwise, use existing for rebuilds
compilation ??= new AngularCompilation();
// Initialize the Angular compilation for the current build.
// In watch mode, previous build state will be reused.
const { affectedFiles } = await compilation.initialize(
rootNames,
compilerOptions,
hostOptions,
configurationDiagnostics,
);
// Clear affected files from the cache (if present)
if (pluginOptions.sourceFileCache) {
for (const affected of affectedFiles) {
pluginOptions.sourceFileCache.typeScriptFileCache.delete(
pathToFileURL(affected.fileName).href,
);
}
}
profileSync('NG_DIAGNOSTICS_TOTAL', () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
for (const diagnostic of compilation!.collectDiagnostics()) {
const message = convertTypeScriptDiagnostic(diagnostic);
if (diagnostic.category === ts.DiagnosticCategory.Error) {
(result.errors ??= []).push(message);
} else {
(result.warnings ??= []).push(message);
}
}
});
fileEmitter = compilation.createFileEmitter();
return result;
});
build.onLoad(
{ filter: compilerOptions.allowJs ? /\.[cm]?[jt]sx?$/ : /\.[cm]?tsx?$/ },
(args) =>
profileAsync(
'NG_EMIT_TS*',
async () => {
assert.ok(fileEmitter, 'Invalid plugin execution order');
const request = pluginOptions.fileReplacements?.[args.path] ?? args.path;
// The filename is currently used as a cache key. Since the cache is memory only,
// the options cannot change and do not need to be represented in the key. If the
// cache is later stored to disk, then the options that affect transform output
// would need to be added to the key as well as a check for any change of content.
let contents = pluginOptions.sourceFileCache?.typeScriptFileCache.get(
pathToFileURL(request).href,
);
if (contents === undefined) {
const typescriptResult = await fileEmitter(request);
if (!typescriptResult?.content) {
// No TS result indicates the file is not part of the TypeScript program.
// If allowJs is enabled and the file is JS then defer to the next load hook.
if (compilerOptions.allowJs && /\.[cm]?js$/.test(request)) {
return undefined;
}
// Otherwise return an error
return {
errors: [
createMissingFileError(
request,
args.path,
build.initialOptions.absWorkingDir ?? '',
),
],
};
}
contents = await javascriptTransformer.transformData(
request,
typescriptResult.content,
true /* skipLinker */,
);
pluginOptions.sourceFileCache?.typeScriptFileCache.set(
pathToFileURL(request).href,
contents,
);
}
return {
contents,
loader: 'js',
};
},
true,
),
);
build.onLoad({ filter: /\.[cm]?js$/ }, (args) =>
profileAsync(
'NG_EMIT_JS*',
async () => {
// The filename is currently used as a cache key. Since the cache is memory only,
// the options cannot change and do not need to be represented in the key. If the
// cache is later stored to disk, then the options that affect transform output
// would need to be added to the key as well as a check for any change of content.
let contents = pluginOptions.sourceFileCache?.babelFileCache.get(args.path);
if (contents === undefined) {
contents = await javascriptTransformer.transformFile(args.path);
pluginOptions.sourceFileCache?.babelFileCache.set(args.path, contents);
}
return {
contents,
loader: 'js',
};
},
true,
),
);
build.onEnd((result) => {
if (stylesheetResourceFiles.length) {
result.outputFiles?.push(...stylesheetResourceFiles);
}
logCumulativeDurations();
});
},
};
}
function createMissingFileError(request: string, original: string, root: string): PartialMessage {
const error = {
text: `File '${path.relative(root, request)}' is missing from the TypeScript compilation.`,
notes: [
{
text: `Ensure the file is part of the TypeScript program via the 'files' or 'include' property.`,
},
],
};
if (request !== original) {
error.notes.push({
text: `File is requested from a file replacement of '${path.relative(root, original)}'.`,
});
}
return error;
}