forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication_builder.ts
643 lines (557 loc) · 21.4 KB
/
application_builder.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/**
* @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 type { BuilderContext, BuilderOutput } from '@angular-devkit/architect';
import type { Config, ConfigOptions, FilePattern, InlinePluginDef, Server } from 'karma';
import { randomUUID } from 'node:crypto';
import * as fs from 'node:fs/promises';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { createRequire } from 'node:module';
import * as path from 'node:path';
import { ReadableStreamController } from 'node:stream/web';
import { globSync } from 'tinyglobby';
import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
import { emitFilesToDisk } from '../../tools/esbuild/utils';
import { buildApplicationInternal } from '../application/index';
import { ApplicationBuilderInternalOptions } from '../application/options';
import { Result, ResultFile, ResultKind } from '../application/results';
import { OutputHashing } from '../application/schema';
import { findTests, getTestEntrypoints } from './find-tests';
import { Schema as KarmaBuilderOptions } from './schema';
const localResolve = createRequire(__filename).resolve;
const isWindows = process.platform === 'win32';
interface BuildOptions extends ApplicationBuilderInternalOptions {
// We know that it's always a string since we set it.
outputPath: string;
}
class ApplicationBuildError extends Error {
constructor(message: string) {
super(message);
this.name = 'ApplicationBuildError';
}
}
interface ServeFileFunction {
(
filepath: string,
rangeHeader: string | string[] | undefined,
response: ServerResponse,
transform?: (c: string | Uint8Array) => string | Uint8Array,
content?: string | Uint8Array,
doNotCache?: boolean,
): void;
}
interface LatestBuildFiles {
files: Record<string, ResultFile | undefined>;
}
const LATEST_BUILD_FILES_TOKEN = 'angularLatestBuildFiles';
class AngularAssetsMiddleware {
static readonly $inject = ['serveFile', LATEST_BUILD_FILES_TOKEN];
static readonly NAME = 'angular-test-assets';
constructor(
private readonly serveFile: ServeFileFunction,
private readonly latestBuildFiles: LatestBuildFiles,
) {}
handle(req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => unknown) {
const url = new URL(`http://${req.headers['host']}${req.url}`);
// Remove the leading slash from the URL path and convert to platform specific.
// The latest build files will use the platform path separator.
let pathname = url.pathname.slice(1);
if (isWindows) {
pathname = pathname.replaceAll(path.posix.sep, path.win32.sep);
}
const file = this.latestBuildFiles.files[pathname];
if (!file) {
next();
return;
}
switch (file.origin) {
case 'disk':
this.serveFile(file.inputPath, undefined, res);
break;
case 'memory':
// Include pathname to help with Content-Type headers.
this.serveFile(`/unused/${url.pathname}`, undefined, res, undefined, file.contents, true);
break;
}
}
static createPlugin(initialFiles: LatestBuildFiles): InlinePluginDef {
return {
[LATEST_BUILD_FILES_TOKEN]: ['value', { files: { ...initialFiles.files } }],
[`middleware:${AngularAssetsMiddleware.NAME}`]: [
'factory',
Object.assign((...args: ConstructorParameters<typeof AngularAssetsMiddleware>) => {
const inst = new AngularAssetsMiddleware(...args);
return inst.handle.bind(inst);
}, AngularAssetsMiddleware),
],
};
}
}
class AngularPolyfillsPlugin {
static readonly $inject = ['config.files'];
static readonly NAME = 'angular-polyfills';
static createPlugin(
polyfillsFile: FilePattern,
jasmineCleanupFiles: FilePattern,
): InlinePluginDef {
return {
// This has to be a "reporter" because reporters run _after_ frameworks
// and karma-jasmine-html-reporter injects additional scripts that may
// depend on Jasmine but aren't modules - which means that they would run
// _before_ all module code (including jasmine).
[`reporter:${AngularPolyfillsPlugin.NAME}`]: [
'factory',
Object.assign((files: (string | FilePattern)[]) => {
// The correct order is zone.js -> jasmine -> zone.js/testing.
// Jasmine has to see the patched version of the global `setTimeout`
// function so it doesn't cache the unpatched version. And /testing
// needs to see the global `jasmine` object so it can patch it.
const polyfillsIndex = 0;
files.splice(polyfillsIndex, 0, polyfillsFile);
// Insert just before test_main.js.
const zoneTestingIndex = files.findIndex((f) => {
if (typeof f === 'string') {
return false;
}
return f.pattern.endsWith('/test_main.js');
});
if (zoneTestingIndex === -1) {
throw new Error('Could not find test entrypoint file.');
}
files.splice(zoneTestingIndex, 0, jasmineCleanupFiles);
// We need to ensure that all files are served as modules, otherwise
// the order in the files list gets really confusing: Karma doesn't
// set defer on scripts, so all scripts with type=js will run first,
// even if type=module files appeared earlier in `files`.
for (const f of files) {
if (typeof f === 'string') {
throw new Error(`Unexpected string-based file: "${f}"`);
}
if (f.included === false) {
// Don't worry about files that aren't included on the initial
// page load. `type` won't affect them.
continue;
}
if (f.pattern.endsWith('.js') && 'js' === (f.type ?? 'js')) {
f.type = 'module';
}
}
// Add browser sourcemap support as a classic script
files.unshift({
pattern: localResolve('source-map-support/browser-source-map-support.js'),
included: true,
watched: false,
});
}, AngularPolyfillsPlugin),
],
};
}
}
function injectKarmaReporter(
buildOptions: BuildOptions,
buildIterator: AsyncIterator<Result>,
karmaConfig: Config & ConfigOptions,
controller: ReadableStreamController<BuilderOutput>,
) {
const reporterName = 'angular-progress-notifier';
interface RunCompleteInfo {
exitCode: number;
}
interface KarmaEmitter {
refreshFiles(): void;
}
class ProgressNotifierReporter {
static $inject = ['emitter', LATEST_BUILD_FILES_TOKEN];
constructor(
private readonly emitter: KarmaEmitter,
private readonly latestBuildFiles: LatestBuildFiles,
) {
this.startWatchingBuild();
}
private startWatchingBuild() {
void (async () => {
// This is effectively "for await of but skip what's already consumed".
let isDone = false; // to mark the loop condition as "not constant".
while (!isDone) {
const { done, value: buildOutput } = await buildIterator.next();
if (done) {
isDone = true;
break;
}
if (buildOutput.kind === ResultKind.Failure) {
controller.enqueue({ success: false, message: 'Build failed' });
} else if (
buildOutput.kind === ResultKind.Incremental ||
buildOutput.kind === ResultKind.Full
) {
if (buildOutput.kind === ResultKind.Full) {
this.latestBuildFiles.files = buildOutput.files;
} else {
this.latestBuildFiles.files = {
...this.latestBuildFiles.files,
...buildOutput.files,
};
}
await writeTestFiles(buildOutput.files, buildOptions.outputPath);
this.emitter.refreshFiles();
}
}
})();
}
onRunComplete = function (_browsers: unknown, results: RunCompleteInfo) {
if (results.exitCode === 0) {
controller.enqueue({ success: true });
} else {
controller.enqueue({ success: false });
}
};
}
karmaConfig.reporters ??= [];
karmaConfig.reporters.push(reporterName);
karmaConfig.plugins ??= [];
karmaConfig.plugins.push({
[`reporter:${reporterName}`]: [
'factory',
Object.assign(
(...args: ConstructorParameters<typeof ProgressNotifierReporter>) =>
new ProgressNotifierReporter(...args),
ProgressNotifierReporter,
),
],
});
}
export function execute(
options: KarmaBuilderOptions,
context: BuilderContext,
karmaOptions: ConfigOptions,
transforms: {
// The karma options transform cannot be async without a refactor of the builder implementation
karmaOptions?: (options: ConfigOptions) => ConfigOptions;
} = {},
): AsyncIterable<BuilderOutput> {
let karmaServer: Server;
return new ReadableStream({
async start(controller) {
let init;
try {
init = await initializeApplication(options, context, karmaOptions, transforms);
} catch (err) {
if (err instanceof ApplicationBuildError) {
controller.enqueue({ success: false, message: err.message });
controller.close();
return;
}
throw err;
}
const [karma, karmaConfig, buildOptions, buildIterator] = init;
// If `--watch` is explicitly enabled or if we are keeping the Karma
// process running, we should hook Karma into the build.
if (buildIterator) {
injectKarmaReporter(buildOptions, buildIterator, karmaConfig, controller);
}
// Close the stream once the Karma server returns.
karmaServer = new karma.Server(karmaConfig as Config, (exitCode) => {
controller.enqueue({ success: exitCode === 0 });
controller.close();
});
await karmaServer.start();
},
async cancel() {
await karmaServer?.stop();
},
});
}
async function getProjectSourceRoot(context: BuilderContext): Promise<string> {
// We have already validated that the project name is set before calling this function.
const projectName = context.target?.project;
if (!projectName) {
return context.workspaceRoot;
}
const projectMetadata = await context.getProjectMetadata(projectName);
const sourceRoot = (projectMetadata.sourceRoot ?? projectMetadata.root ?? '') as string;
return path.join(context.workspaceRoot, sourceRoot);
}
function normalizePolyfills(polyfills: string | string[] | undefined): [string[], string[]] {
if (typeof polyfills === 'string') {
polyfills = [polyfills];
} else if (!polyfills) {
polyfills = [];
}
const jasmineGlobalEntryPoint = localResolve('./polyfills/jasmine_global.js');
const jasmineGlobalCleanupEntrypoint = localResolve('./polyfills/jasmine_global_cleanup.js');
const sourcemapEntrypoint = localResolve('./polyfills/init_sourcemaps.js');
const zoneTestingEntryPoint = 'zone.js/testing';
const polyfillsExludingZoneTesting = polyfills.filter((p) => p !== zoneTestingEntryPoint);
return [
polyfillsExludingZoneTesting.concat([jasmineGlobalEntryPoint, sourcemapEntrypoint]),
polyfillsExludingZoneTesting.length === polyfills.length
? [jasmineGlobalCleanupEntrypoint]
: [jasmineGlobalCleanupEntrypoint, zoneTestingEntryPoint],
];
}
async function collectEntrypoints(
options: KarmaBuilderOptions,
context: BuilderContext,
projectSourceRoot: string,
): Promise<Map<string, string>> {
// Glob for files to test.
const testFiles = await findTests(
options.include ?? [],
options.exclude ?? [],
context.workspaceRoot,
projectSourceRoot,
);
return getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot: context.workspaceRoot });
}
// eslint-disable-next-line max-lines-per-function
async function initializeApplication(
options: KarmaBuilderOptions,
context: BuilderContext,
karmaOptions: ConfigOptions,
transforms: {
karmaOptions?: (options: ConfigOptions) => ConfigOptions;
} = {},
): Promise<
[typeof import('karma'), Config & ConfigOptions, BuildOptions, AsyncIterator<Result> | null]
> {
const outputPath = path.join(context.workspaceRoot, 'dist/test-out', randomUUID());
const projectSourceRoot = await getProjectSourceRoot(context);
const [karma, entryPoints] = await Promise.all([
import('karma'),
collectEntrypoints(options, context, projectSourceRoot),
fs.rm(outputPath, { recursive: true, force: true }),
]);
const mainName = 'test_main';
if (options.main) {
entryPoints.set(mainName, options.main);
} else {
entryPoints.set(mainName, localResolve('./polyfills/init_test_bed.js'));
}
const instrumentForCoverage = options.codeCoverage
? createInstrumentationFilter(
projectSourceRoot,
getInstrumentationExcludedPaths(context.workspaceRoot, options.codeCoverageExclude ?? []),
)
: undefined;
const [polyfills, jasmineCleanup] = normalizePolyfills(options.polyfills);
for (let idx = 0; idx < jasmineCleanup.length; ++idx) {
entryPoints.set(`jasmine-cleanup-${idx}`, jasmineCleanup[idx]);
}
const buildOptions: BuildOptions = {
assets: options.assets,
entryPoints,
tsConfig: options.tsConfig,
outputPath,
preserveSymlinks: options.preserveSymlinks,
aot: options.aot,
index: false,
outputHashing: OutputHashing.None,
optimization: false,
sourceMap: options.codeCoverage
? {
scripts: true,
styles: true,
vendor: true,
}
: options.sourceMap,
instrumentForCoverage,
styles: options.styles,
scripts: options.scripts,
polyfills,
webWorkerTsConfig: options.webWorkerTsConfig,
watch: options.watch ?? !karmaOptions.singleRun,
stylePreprocessorOptions: options.stylePreprocessorOptions,
inlineStyleLanguage: options.inlineStyleLanguage,
fileReplacements: options.fileReplacements,
define: options.define,
loader: options.loader,
externalDependencies: options.externalDependencies,
};
// Build tests with `application` builder, using test files as entry points.
const [buildOutput, buildIterator] = await first(
buildApplicationInternal(buildOptions, context),
{ cancel: !buildOptions.watch },
);
if (buildOutput.kind === ResultKind.Failure) {
throw new ApplicationBuildError('Build failed');
} else if (buildOutput.kind !== ResultKind.Full) {
throw new ApplicationBuildError(
'A full build result is required from the application builder.',
);
}
// Write test files
await writeTestFiles(buildOutput.files, buildOptions.outputPath);
// We need to add this to the beginning *after* the testing framework has
// prepended its files. The output path is required for each since they are
// added later in the test process via a plugin.
const polyfillsFile: FilePattern = {
pattern: `${outputPath}/polyfills.js`,
included: true,
served: true,
type: 'module',
watched: false,
};
const jasmineCleanupFiles: FilePattern = {
pattern: `${outputPath}/jasmine-cleanup-*.js`,
included: true,
served: true,
type: 'module',
watched: false,
};
karmaOptions.basePath = outputPath;
karmaOptions.files ??= [];
if (options.scripts?.length) {
// This should be more granular to support named bundles.
// However, it replicates the behavior of the Karma Webpack-based builder.
karmaOptions.files.push({
pattern: `scripts.js`,
watched: false,
type: 'js',
});
}
karmaOptions.files.push(
// Serve global setup script.
{ pattern: `${mainName}.js`, type: 'module', watched: false },
// Serve all source maps.
{ pattern: `*.map`, included: false, watched: false },
// These are the test entrypoints.
{ pattern: `spec-*.js`, type: 'module', watched: false },
);
if (hasChunkOrWorkerFiles(buildOutput.files)) {
karmaOptions.files.push(
// Allow loading of chunk-* files but don't include them all on load.
{
pattern: `{chunk,worker}-*.js`,
type: 'module',
included: false,
watched: false,
},
);
}
if (options.styles?.length) {
// Serve CSS outputs on page load, these are the global styles.
karmaOptions.files.push({ pattern: `*.css`, type: 'css', watched: false });
}
const parsedKarmaConfig: Config & ConfigOptions = await karma.config.parseConfig(
options.karmaConfig && path.resolve(context.workspaceRoot, options.karmaConfig),
transforms.karmaOptions ? transforms.karmaOptions(karmaOptions) : karmaOptions,
{ promiseConfig: true, throwErrors: true },
);
// Check for jsdom which does not support executing ESM scripts.
// If present, remove jsdom and issue a warning.
const updatedBrowsers = parsedKarmaConfig.browsers?.filter((browser) => browser !== 'jsdom');
if (parsedKarmaConfig.browsers?.length !== updatedBrowsers?.length) {
parsedKarmaConfig.browsers = updatedBrowsers;
context.logger.warn(
`'jsdom' does not support ESM code execution and cannot be used for karma testing.` +
` The 'jsdom' entry has been removed from the 'browsers' option.`,
);
}
// Remove the webpack plugin/framework:
// Alternative would be to make the Karma plugin "smart" but that's a tall order
// with managing unneeded imports etc..
parsedKarmaConfig.plugins ??= [];
const pluginLengthBefore = parsedKarmaConfig.plugins.length;
parsedKarmaConfig.plugins = parsedKarmaConfig.plugins.filter(
(plugin: string | InlinePluginDef) => {
if (typeof plugin === 'string') {
return plugin !== 'framework:@angular-devkit/build-angular';
}
return !plugin['framework:@angular-devkit/build-angular'];
},
);
parsedKarmaConfig.frameworks ??= [];
parsedKarmaConfig.frameworks = parsedKarmaConfig.frameworks.filter(
(framework: string) => framework !== '@angular-devkit/build-angular',
);
const pluginLengthAfter = parsedKarmaConfig.plugins.length;
if (pluginLengthBefore !== pluginLengthAfter) {
context.logger.warn(
`Ignoring framework "@angular-devkit/build-angular" from karma config file because it's not compatible with the application builder.`,
);
}
parsedKarmaConfig.plugins.push(AngularAssetsMiddleware.createPlugin(buildOutput));
parsedKarmaConfig.middleware ??= [];
parsedKarmaConfig.middleware.push(AngularAssetsMiddleware.NAME);
parsedKarmaConfig.plugins.push(
AngularPolyfillsPlugin.createPlugin(polyfillsFile, jasmineCleanupFiles),
);
parsedKarmaConfig.reporters ??= [];
parsedKarmaConfig.reporters.push(AngularPolyfillsPlugin.NAME);
// When using code-coverage, auto-add karma-coverage.
// This was done as part of the karma plugin for webpack.
if (
options.codeCoverage &&
!parsedKarmaConfig.reporters?.some((r: string) => r === 'coverage' || r === 'coverage-istanbul')
) {
parsedKarmaConfig.reporters = (parsedKarmaConfig.reporters ?? []).concat(['coverage']);
}
return [karma, parsedKarmaConfig, buildOptions, buildIterator];
}
function hasChunkOrWorkerFiles(files: Record<string, unknown>): boolean {
return Object.keys(files).some((filename) => {
return /(?:^|\/)(?:worker|chunk)[^/]+\.js$/.test(filename);
});
}
export async function writeTestFiles(files: Record<string, ResultFile>, testDir: string) {
const directoryExists = new Set<string>();
// Writes the test related output files to disk and ensures the containing directories are present
await emitFilesToDisk(Object.entries(files), async ([filePath, file]) => {
if (file.type !== BuildOutputFileType.Browser && file.type !== BuildOutputFileType.Media) {
return;
}
const fullFilePath = path.join(testDir, filePath);
// Ensure output subdirectories exist
const fileBasePath = path.dirname(fullFilePath);
if (fileBasePath && !directoryExists.has(fileBasePath)) {
await fs.mkdir(fileBasePath, { recursive: true });
directoryExists.add(fileBasePath);
}
if (file.origin === 'memory') {
// Write file contents
await fs.writeFile(fullFilePath, file.contents);
} else {
// Copy file contents
await fs.copyFile(file.inputPath, fullFilePath, fs.constants.COPYFILE_FICLONE);
}
});
}
/** Returns the first item yielded by the given generator and cancels the execution. */
async function first<T>(
generator: AsyncIterable<T>,
{ cancel }: { cancel: boolean },
): Promise<[T, AsyncIterator<T> | null]> {
if (!cancel) {
const iterator: AsyncIterator<T> = generator[Symbol.asyncIterator]();
const firstValue = await iterator.next();
if (firstValue.done) {
throw new Error('Expected generator to emit at least once.');
}
return [firstValue.value, iterator];
}
for await (const value of generator) {
return [value, null];
}
throw new Error('Expected generator to emit at least once.');
}
function createInstrumentationFilter(includedBasePath: string, excludedPaths: Set<string>) {
return (request: string): boolean => {
return (
!excludedPaths.has(request) &&
!/\.(e2e|spec)\.tsx?$|[\\/]node_modules[\\/]/.test(request) &&
request.startsWith(includedBasePath)
);
};
}
function getInstrumentationExcludedPaths(root: string, excludedPaths: string[]): Set<string> {
const excluded = new Set<string>();
for (const excludeGlob of excludedPaths) {
const excludePath = excludeGlob[0] === '/' ? excludeGlob.slice(1) : excludeGlob;
globSync(excludePath, { cwd: root }).forEach((p) => excluded.add(path.join(root, p)));
}
return excluded;
}