Skip to content

Commit

Permalink
perf(@angular-devkit/build-angular): use single JS transformer instan…
Browse files Browse the repository at this point in the history
…ce 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.
  • Loading branch information
clydin authored and dgp1130 committed Sep 8, 2023
1 parent 1a6a139 commit 4b67d2a
Showing 1 changed file with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export async function* serveWithVite(
serverOptions.servePath = browserOptions.baseHref;
}

// Setup the prebundling transformer that will be shared across Vite prebundling requests
const prebundleTransformer = new JavaScriptTransformer(
// Always enable JIT linking to support applications built with and without AOT.
// In a development environment the additional scope information does not
// have a negative effect unlike production where final output size is relevant.
{ sourcemap: true, jit: true },
1,
);

// dynamically import Vite for ESM compatibility
const { createServer, normalizePath } = await import('vite');

Expand Down Expand Up @@ -99,6 +108,7 @@ export async function* serveWithVite(
browserOptions.preserveSymlinks,
browserOptions.externalDependencies,
!!browserOptions.ssr,
prebundleTransformer,
);

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

if (server) {
let deferred: () => void;
context.addTeardown(async () => {
await server?.close();
deferred?.();
});
await new Promise<void>((resolve) => (deferred = resolve));
}
// Add cleanup logic via a builder teardown
let deferred: () => void;
context.addTeardown(async () => {
await server?.close();
await prebundleTransformer.close();
deferred?.();
});
await new Promise<void>((resolve) => (deferred = resolve));
}

function handleUpdate(
Expand Down Expand Up @@ -241,6 +251,7 @@ export async function setupServer(
preserveSymlinks: boolean | undefined,
prebundleExclude: string[] | undefined,
ssr: boolean,
prebundleTransformer: JavaScriptTransformer,
): Promise<InlineConfig> {
const proxy = await loadProxyConfiguration(
serverOptions.workspaceRoot,
Expand Down Expand Up @@ -494,21 +505,12 @@ export async function setupServer(
{
name: 'angular-vite-optimize-deps',
setup(build) {
const transformer = new JavaScriptTransformer(
// Always enable JIT linking to support applications built with and without AOT.
// In a development environment the additional scope information does not
// have a negative effect unlike production where final output size is relevant.
{ sourcemap: !!build.initialOptions.sourcemap, jit: true },
1,
);

build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
return {
contents: await transformer.transformFile(args.path),
contents: await prebundleTransformer.transformFile(args.path),
loader: 'js',
};
});
build.onEnd(() => transformer.close());
},
},
],
Expand Down

0 comments on commit 4b67d2a

Please sign in to comment.