Skip to content

Commit 9f2d3f7

Browse files
committed
fix(@angular/build): externalize Angular dependencies in Vitest runner
Marks common Angular packages and RxJS as external within the Vitest test runner's build configuration. This allows Vite to perform dependency pre-bundling on these libraries, which can significantly improve build and rebuild performance during test execution. The implementation introduces a constant to hold the list of packages and merges them with any externalDependencies that may already exist in the base build options, preserving user configuration.
1 parent f2248ba commit 9f2d3f7

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/build-options.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ import { NormalizedUnitTestBuilderOptions, injectTestingPolyfills } from '../../
1414
import { findTests, getTestEntrypoints } from '../../test-discovery';
1515
import { RunnerOptions } from '../api';
1616

17+
/**
18+
* A list of Angular related packages that should be marked as external.
19+
* This allows Vite to pre-bundle them, improving performance.
20+
*/
21+
const ANGULAR_PACKAGES_TO_EXTERNALIZE = [
22+
'@angular/core',
23+
'@angular/common',
24+
'@angular/platform-browser',
25+
'@angular/compiler',
26+
'@angular/router',
27+
'@angular/forms',
28+
'@angular/animations',
29+
'rxjs',
30+
];
31+
1732
function createTestBedInitVirtualFile(
1833
providersFile: string | undefined,
1934
projectSourceRoot: string,
@@ -83,6 +98,11 @@ export async function getVitestBuildOptions(
8398
});
8499
entryPoints.set('init-testbed', 'angular:test-bed-init');
85100

101+
const externalDependencies = new Set(['vitest', ...ANGULAR_PACKAGES_TO_EXTERNALIZE]);
102+
if (baseBuildOptions.externalDependencies) {
103+
baseBuildOptions.externalDependencies.forEach((dep) => externalDependencies.add(dep));
104+
}
105+
86106
const buildOptions: Partial<ApplicationBuilderInternalOptions> = {
87107
...baseBuildOptions,
88108
watch,
@@ -101,7 +121,7 @@ export async function getVitestBuildOptions(
101121
outputHashing: adjustOutputHashing(baseBuildOptions.outputHashing),
102122
optimization: false,
103123
entryPoints,
104-
externalDependencies: ['vitest', '@vitest/browser/context'],
124+
externalDependencies: [...externalDependencies],
105125
};
106126

107127
buildOptions.polyfills = injectTestingPolyfills(buildOptions.polyfills);

0 commit comments

Comments
 (0)