Skip to content

Commit 4b67d2a

Browse files
clydindgp1130
authored andcommitted
perf(@angular-devkit/build-angular): use single JS transformer instance during dev-server prebundling
By setting up a single instance of the `JavaTransformer`, the Vite-based development server will now have a fixed and controllable number of worker threads available to process prebundling requests. This avoids a potentially large number of initial worker threads when a new application with a large number of dependencies is first used with the development server. This is particularly beneficial for web container setups which may not be able to efficiently handle the number of workers.
1 parent 1a6a139 commit 4b67d2a

File tree

1 file changed

+20
-18
lines changed
  • packages/angular_devkit/build_angular/src/builders/dev-server

1 file changed

+20
-18
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ export async function* serveWithVite(
6565
serverOptions.servePath = browserOptions.baseHref;
6666
}
6767

68+
// Setup the prebundling transformer that will be shared across Vite prebundling requests
69+
const prebundleTransformer = new JavaScriptTransformer(
70+
// Always enable JIT linking to support applications built with and without AOT.
71+
// In a development environment the additional scope information does not
72+
// have a negative effect unlike production where final output size is relevant.
73+
{ sourcemap: true, jit: true },
74+
1,
75+
);
76+
6877
// dynamically import Vite for ESM compatibility
6978
const { createServer, normalizePath } = await import('vite');
7079

@@ -99,6 +108,7 @@ export async function* serveWithVite(
99108
browserOptions.preserveSymlinks,
100109
browserOptions.externalDependencies,
101110
!!browserOptions.ssr,
111+
prebundleTransformer,
102112
);
103113

104114
server = await createServer(serverConfiguration);
@@ -114,14 +124,14 @@ export async function* serveWithVite(
114124
yield { success: true, port: listeningAddress?.port } as unknown as DevServerBuilderOutput;
115125
}
116126

117-
if (server) {
118-
let deferred: () => void;
119-
context.addTeardown(async () => {
120-
await server?.close();
121-
deferred?.();
122-
});
123-
await new Promise<void>((resolve) => (deferred = resolve));
124-
}
127+
// Add cleanup logic via a builder teardown
128+
let deferred: () => void;
129+
context.addTeardown(async () => {
130+
await server?.close();
131+
await prebundleTransformer.close();
132+
deferred?.();
133+
});
134+
await new Promise<void>((resolve) => (deferred = resolve));
125135
}
126136

127137
function handleUpdate(
@@ -241,6 +251,7 @@ export async function setupServer(
241251
preserveSymlinks: boolean | undefined,
242252
prebundleExclude: string[] | undefined,
243253
ssr: boolean,
254+
prebundleTransformer: JavaScriptTransformer,
244255
): Promise<InlineConfig> {
245256
const proxy = await loadProxyConfiguration(
246257
serverOptions.workspaceRoot,
@@ -494,21 +505,12 @@ export async function setupServer(
494505
{
495506
name: 'angular-vite-optimize-deps',
496507
setup(build) {
497-
const transformer = new JavaScriptTransformer(
498-
// Always enable JIT linking to support applications built with and without AOT.
499-
// In a development environment the additional scope information does not
500-
// have a negative effect unlike production where final output size is relevant.
501-
{ sourcemap: !!build.initialOptions.sourcemap, jit: true },
502-
1,
503-
);
504-
505508
build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
506509
return {
507-
contents: await transformer.transformFile(args.path),
510+
contents: await prebundleTransformer.transformFile(args.path),
508511
loader: 'js',
509512
};
510513
});
511-
build.onEnd(() => transformer.close());
512514
},
513515
},
514516
],

0 commit comments

Comments
 (0)