Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d47386e

Browse files
committed
refactor(@angular/build): breakdown Karma application initialization
The main `initializeApplication` function within the Karma builder was overly large and handled several distinct responsibilities. This change refactors the function by breaking it down into three smaller, more focused functions: - `setupBuildOptions`: Gathers and configures the esbuild build options. - `runEsbuild`: Executes the esbuild build process. - `configureKarma`: Sets up the Karma configuration based on the build output. This decomposition improves the readability, testability, and maintainability of the Karma builder's application initialization process.
1 parent b99e266 commit d47386e

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

‎packages/angular/build/src/builders/karma/application_builder.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ export function execute(
9494
});
9595
}
9696

97-
// eslint-disable-next-line max-lines-per-function
9897
async function initializeApplication(
9998
options: NormalizedKarmaBuilderOptions,
10099
context: BuilderContext,
@@ -103,14 +102,41 @@ async function initializeApplication(
103102
): Promise<
104103
[typeof import('karma'), Config & ConfigOptions, BuildOptions, AsyncIterator<Result> | null]
105104
> {
106-
const outputPath = path.join(context.workspaceRoot,'dist/test-out',randomUUID());
105+
const karma = awaitimport('karma');
107106
const projectSourceRoot = await getProjectSourceRoot(context);
107+
const outputPath = path.join(context.workspaceRoot, 'dist/test-out', randomUUID());
108+
await fs.rm(outputPath, { recursive: true, force: true });
109+
110+
const { buildOptions, mainName } = await setupBuildOptions(
111+
options,
112+
context,
113+
projectSourceRoot,
114+
outputPath,
115+
);
108116

109-
const [karma, entryPoints] = await Promise.all([
110-
import('karma'),
111-
collectEntrypoints(options, context, projectSourceRoot),
112-
fs.rm(outputPath, { recursive: true, force: true }),
113-
]);
117+
const [buildOutput, buildIterator] = await runEsbuild(buildOptions, context, projectSourceRoot);
118+
119+
const karmaConfig = await configureKarma(
120+
karma,
121+
context,
122+
karmaOptions,
123+
options,
124+
buildOptions,
125+
buildOutput,
126+
mainName,
127+
transforms,
128+
);
129+
130+
return [karma, karmaConfig, buildOptions, buildIterator];
131+
}
132+
133+
async function setupBuildOptions(
134+
options: NormalizedKarmaBuilderOptions,
135+
context: BuilderContext,
136+
projectSourceRoot: string,
137+
outputPath: string,
138+
): Promise<{ buildOptions: BuildOptions; mainName: string }> {
139+
const entryPoints = await collectEntrypoints(options, context, projectSourceRoot);
114140

115141
const mainName = 'test_main';
116142
if (options.main) {
@@ -156,6 +182,14 @@ async function initializeApplication(
156182
externalDependencies: options.externalDependencies,
157183
};
158184

185+
return { buildOptions, mainName };
186+
}
187+
188+
async function runEsbuild(
189+
buildOptions: BuildOptions,
190+
context: BuilderContext,
191+
projectSourceRoot: string,
192+
): Promise<[Result & { kind: ResultKind.Full }, AsyncIterator<Result> | null]> {
159193
const virtualTestBedInit = createVirtualModulePlugin({
160194
namespace: 'angular:test-bed-init',
161195
loadContent: async () => {
@@ -166,7 +200,7 @@ async function initializeApplication(
166200
`getTestBed().initTestEnvironment(BrowserTestingModule, platformBrowserTesting(), {`,
167201
` errorOnUnknownElements: true,`,
168202
` errorOnUnknownProperties: true,`,
169-
'});',
203+
`});`,
170204
];
171205

172206
return {
@@ -193,6 +227,21 @@ async function initializeApplication(
193227
// Write test files
194228
await writeTestFiles(buildOutput.files, buildOptions.outputPath);
195229

230+
return [buildOutput, buildIterator];
231+
}
232+
233+
async function configureKarma(
234+
karma: typeof import('karma'),
235+
context: BuilderContext,
236+
karmaOptions: ConfigOptions,
237+
options: NormalizedKarmaBuilderOptions,
238+
buildOptions: BuildOptions,
239+
buildOutput: Result & { kind: ResultKind.Full },
240+
mainName: string,
241+
transforms?: KarmaBuilderTransformsOptions,
242+
): Promise<Config & ConfigOptions> {
243+
const outputPath = buildOptions.outputPath;
244+
196245
// We need to add this to the beginning *after* the testing framework has
197246
// prepended its files. The output path is required for each since they are
198247
// added later in the test process via a plugin.
@@ -352,5 +401,5 @@ async function initializeApplication(
352401
parsedKarmaConfig.reporters = (parsedKarmaConfig.reporters ?? []).concat(['coverage']);
353402
}
354403

355-
return [karma,parsedKarmaConfig,buildOptions,buildIterator];
404+
return parsedKarmaConfig;
356405
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /