Skip to content

Commit fec106b

Browse files
committed
fix(@angular/build): enhance Vitest dependency externalization and pre-bundling
This commit refactors the dependency handling for the Vitest runner to improve flexibility and correctness, especially for browser-based tests. The previously hardcoded list of external Angular packages has been removed. Instead, a new `externalPackages: true` option is passed to the application builder, allowing for more dynamic and configurable externalization of packages. This prevents packages from `node_modules` from being bundled into the test entry points. Note: For cases where a dependency should not be pre-bundled, the `optimizeDeps.exclude` option can be used within a custom runner configuration file. (cherry picked from commit 6e0f5da)
1 parent dea6ced commit fec106b

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,6 @@ 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-
3217
function createTestBedInitVirtualFile(
3318
providersFile: string | undefined,
3419
projectSourceRoot: string,
@@ -110,11 +95,10 @@ export async function getVitestBuildOptions(
11095
});
11196
entryPoints.set('init-testbed', 'angular:test-bed-init');
11297

113-
const externalDependencies = new Set(['vitest']);
114-
ANGULAR_PACKAGES_TO_EXTERNALIZE.forEach((dep) => externalDependencies.add(dep));
115-
98+
// The 'vitest' package is always external for testing purposes
99+
const externalDependencies = ['vitest'];
116100
if (baseBuildOptions.externalDependencies) {
117-
baseBuildOptions.externalDependencies.forEach((dep) => externalDependencies.add(dep));
101+
externalDependencies.push(...baseBuildOptions.externalDependencies);
118102
}
119103

120104
const buildOptions: Partial<ApplicationBuilderInternalOptions> = {
@@ -135,7 +119,10 @@ export async function getVitestBuildOptions(
135119
outputHashing: adjustOutputHashing(baseBuildOptions.outputHashing),
136120
optimization: false,
137121
entryPoints,
138-
externalDependencies: [...externalDependencies],
122+
// Enable support for vitest browser prebundling. Excludes can be controlled with a runnerConfig
123+
// and the `optimizeDeps.exclude` option.
124+
externalPackages: true,
125+
externalDependencies,
139126
};
140127

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export class VitestExecutor implements TestExecutor {
231231
coverage,
232232
projectName,
233233
projectSourceRoot: this.options.projectSourceRoot,
234-
optimizeDepsInclude: this.externalMetadata.explicitBrowser,
234+
optimizeDepsInclude: this.externalMetadata.implicitBrowser,
235235
reporters,
236236
setupFiles: testSetupFiles,
237237
projectPlugins,

0 commit comments

Comments
 (0)