Skip to content

Commit cc3cc8d

Browse files
committed
perf(@ngtools/webpack): reduce resource processing during JIT initial lazy route analysis
During JIT mode, deprecated string-form lazy routes are discovered by analyzing the application via the compiler-cli. This process, however, does not need any of the component resources nor does the CLI need to load them. In JIT mode, a no-op resource loader is now used that will provide the compiler with blank templates and styles. This can greatly reduce the amount of processing required during an initial JIT build, especially for large projects.
1 parent 69f636f commit cc3cc8d

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

packages/ngtools/webpack/src/ivy/host.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { createHash } from 'crypto';
1010
import * as path from 'path';
1111
import * as ts from 'typescript';
1212
import { NgccProcessor } from '../ngcc_processor';
13-
import { WebpackResourceLoader } from '../resource_loader';
13+
import { ResourceLoader } from '../resource_loader';
1414
import { normalizePath } from './paths';
1515

1616
export function augmentHostWithResources(
1717
host: ts.CompilerHost,
18-
resourceLoader: WebpackResourceLoader,
18+
resourceLoader: ResourceLoader,
1919
options: { directTemplateLoading?: boolean } = {},
2020
) {
2121
const resourceHost = host as CompilerHost;

packages/ngtools/webpack/src/ivy/plugin.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { findLazyRoutes } from '../lazy_routes';
2020
import { NgccProcessor } from '../ngcc_processor';
2121
import { TypeScriptPathsPlugin } from '../paths-plugin';
22-
import { WebpackResourceLoader } from '../resource_loader';
22+
import { NoopResourceLoader, ResourceLoader, WebpackResourceLoader } from '../resource_loader';
2323
import { addError, addWarning } from '../webpack-diagnostics';
2424
import { isWebpackFiveOrHigher, mergeResolverMainFields } from '../webpack-version';
2525
import { SourceFileCache } from './cache';
@@ -158,7 +158,9 @@ export class AngularWebpackPlugin {
158158
});
159159

160160
let ngccProcessor: NgccProcessor | undefined;
161-
const resourceLoader = new WebpackResourceLoader();
161+
const resourceLoader = this.pluginOptions.jitMode
162+
? new NoopResourceLoader()
163+
: new WebpackResourceLoader();
162164
let previousUnused: Set<string> | undefined;
163165
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (thisCompilation) => {
164166
const compilation = thisCompilation as WebpackCompilation;
@@ -231,7 +233,8 @@ export class AngularWebpackPlugin {
231233
// Setup resource loading
232234
resourceLoader.update(compilation, changedFiles);
233235
augmentHostWithResources(host, resourceLoader, {
234-
directTemplateLoading: this.pluginOptions.directTemplateLoading,
236+
directTemplateLoading:
237+
!this.pluginOptions.jitMode && this.pluginOptions.directTemplateLoading,
235238
});
236239

237240
// Setup source file adjustment options
@@ -403,7 +406,7 @@ export class AngularWebpackPlugin {
403406
rootNames: string[],
404407
host: CompilerHost,
405408
diagnosticsReporter: DiagnosticsReporter,
406-
resourceLoader: WebpackResourceLoader,
409+
resourceLoader: ResourceLoader,
407410
) {
408411
// Create the Angular specific program that contains the Angular compiler
409412
const angularProgram = new NgtscProgram(

packages/ngtools/webpack/src/resource_loader.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ import * as vm from 'vm';
1313
import { RawSource } from 'webpack-sources';
1414
import { normalizePath } from './ivy/paths';
1515

16+
export interface ResourceLoader {
17+
get(file: string): Promise<string>;
18+
getModifiedResourceFiles(): Set<string>;
19+
getResourceDependencies(file: string): Iterable<string>;
20+
setAffectedResources(file: string, resources: Iterable<string>): void;
21+
update(
22+
parentCompilation: import('webpack').compilation.Compilation,
23+
changedFiles?: Iterable<string>,
24+
): void;
25+
}
26+
1627
const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
1728
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
1829
const LibraryTemplatePlugin = require('webpack/lib/LibraryTemplatePlugin');
@@ -25,6 +36,23 @@ interface CompilationOutput {
2536
success: boolean;
2637
}
2738

39+
export class NoopResourceLoader implements ResourceLoader {
40+
async get(): Promise<string> {
41+
return '';
42+
}
43+
44+
getModifiedResourceFiles(): Set<string> {
45+
return new Set();
46+
}
47+
48+
getResourceDependencies(): Iterable<string> {
49+
return [];
50+
}
51+
52+
setAffectedResources(): void {}
53+
update(): void {}
54+
}
55+
2856
export class WebpackResourceLoader {
2957
private _parentCompilation: any;
3058
private _fileDependencies = new Map<string, Set<string>>();

0 commit comments

Comments
 (0)