Skip to content

Commit 9ee6986

Browse files
committed
refactor(@angular-devkit/build-angular): remove as any usage in karma builder
Refactor the Karma builder's `execute` function to directly handle dynamic imports within conditional blocks. This eliminates the need for `getExecuteWithBuilder` and the resulting union types that required `as any` casting. This improves type safety by allowing TypeScript to verify the arguments passed to the specific builder implementations (Esbuild vs Webpack).
1 parent e865c64 commit 9ee6986

File tree

1 file changed

+10
-31
lines changed
  • packages/angular_devkit/build_angular/src/builders/karma

1 file changed

+10
-31
lines changed

packages/angular_devkit/build_angular/src/builders/karma/index.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ export async function* execute(
4242
// Check Angular version.
4343
assertCompatibleAngularVersion(context.workspaceRoot);
4444

45-
const [useEsbuild, executeWithBuilder] = await getExecuteWithBuilder(options, context);
46-
47-
if (useEsbuild) {
45+
if (await checkForEsbuild(options, context)) {
4846
if (transforms.webpackConfiguration) {
4947
context.logger.warn(
5048
`This build is using the application builder but transforms.webpackConfiguration was provided. The transform will be ignored.`,
@@ -59,13 +57,18 @@ export async function* execute(
5957
options.polyfills = [options.polyfills];
6058
}
6159

62-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
63-
yield* executeWithBuilder(options as any, context, transforms);
60+
const { executeKarmaBuilder } = await import('@angular/build');
61+
62+
yield* executeKarmaBuilder(
63+
options as unknown as import('@angular/build').KarmaBuilderOptions,
64+
context,
65+
transforms,
66+
);
6467
} else {
6568
const karmaOptions = getBaseKarmaOptions(options, context);
69+
const { execute } = await import('./browser_builder');
6670

67-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
68-
yield* executeWithBuilder(options as any, context, karmaOptions, transforms);
71+
yield* execute(options, context, karmaOptions, transforms);
6972
}
7073
}
7174

@@ -168,30 +171,6 @@ function getBuiltInKarmaConfig(
168171
export type { KarmaBuilderOptions };
169172
export default createBuilder<KarmaBuilderOptions>(execute);
170173

171-
async function getExecuteWithBuilder(
172-
options: KarmaBuilderOptions,
173-
context: BuilderContext,
174-
): Promise<
175-
[
176-
boolean,
177-
(
178-
| (typeof import('@angular/build'))['executeKarmaBuilder']
179-
| (typeof import('./browser_builder'))['execute']
180-
),
181-
]
182-
> {
183-
const useEsbuild = await checkForEsbuild(options, context);
184-
let execute;
185-
if (useEsbuild) {
186-
const { executeKarmaBuilder } = await import('@angular/build');
187-
execute = executeKarmaBuilder;
188-
} else {
189-
const browserBuilderModule = await import('./browser_builder');
190-
execute = browserBuilderModule.execute;
191-
}
192-
193-
return [useEsbuild, execute];
194-
}
195174

196175
async function checkForEsbuild(
197176
options: KarmaBuilderOptions,

0 commit comments

Comments
 (0)