Skip to content

Commit 6c319e4

Browse files
committed
fix(@angular-devkit/build-angular): fix webpack config transform for karma
(cherry picked from commit fb41d18)
1 parent f46d0b1 commit 6c319e4

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

Diff for: modules/testing/builder/src/builder-harness.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export interface BuilderHarnessExecutionOptions {
5454
outputLogsOnException: boolean;
5555
useNativeFileWatching: boolean;
5656
signal: AbortSignal;
57+
additionalExecuteArguments: unknown[];
58+
}
59+
60+
interface BuilderHandlerFnWithVarArgs<T> extends BuilderHandlerFn<T> {
61+
(input: T, context: BuilderContext, ...args: unknown[]): BuilderOutputLike;
5762
}
5863

5964
/**
@@ -256,7 +261,13 @@ export class BuilderHarness<T> {
256261
mergeMap((validator) => validator(targetOptions)),
257262
map((validationResult) => validationResult.data),
258263
mergeMap((data) =>
259-
convertBuilderOutputToObservable(this.builderHandler(data as T & json.JsonObject, context)),
264+
convertBuilderOutputToObservable(
265+
(this.builderHandler as BuilderHandlerFnWithVarArgs<T>)(
266+
data as T & json.JsonObject,
267+
context,
268+
...(options.additionalExecuteArguments ?? []),
269+
),
270+
),
260271
),
261272
map((buildResult) => ({ result: buildResult, error: undefined })),
262273
catchError((error) => {

Diff for: packages/angular_devkit/build_angular/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ LARGE_SPECS = {
321321
"@npm//karma-jasmine",
322322
"@npm//karma-jasmine-html-reporter",
323323
"@npm//puppeteer",
324+
"@npm//webpack",
324325
],
325326
},
326327
"protractor": {

Diff for: packages/angular_devkit/build_angular/src/builders/karma/browser_builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function execute(
3434
karmaOptions?: (options: KarmaConfigOptions) => KarmaConfigOptions;
3535
} = {},
3636
): Observable<BuilderOutput> {
37-
return from(initializeBrowser(options, context)).pipe(
37+
return from(initializeBrowser(options, context, transforms.webpackConfiguration)).pipe(
3838
switchMap(async ([karma, webpackConfig]) => {
3939
const projectName = context.target?.project;
4040
if (!projectName) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { Configuration } from 'webpack';
10+
11+
import { execute } from '../../index';
12+
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
13+
import { ExecutionTransformer } from '../../../../transforms';
14+
15+
describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget, isApplicationBuilder) => {
16+
describe('Option: Custom file loader', () => {
17+
beforeEach(async () => {
18+
if (isApplicationBuilder) {
19+
pending('not implemented yet for application builder');
20+
}
21+
await setupTarget(harness);
22+
});
23+
24+
beforeEach(async () => {
25+
await harness.writeFiles({
26+
'src/number-webpack-loader.js': `
27+
module.exports = (source) => {
28+
return 'export const DOUBLED = ' + (Number(source) * 2) + ';\\n';
29+
};`,
30+
'src/app/app.number': `42`,
31+
'src/app/app.number.d.ts': `export const DOUBLED: number;`,
32+
'src/app/app.component.spec.ts': `
33+
import { DOUBLED } from './app.number';
34+
describe('Custom webpack transform', () => {
35+
it('generates expected export', () => {
36+
expect(DOUBLED).toBe(84);
37+
});
38+
});`,
39+
});
40+
});
41+
42+
it('applies the webpack configuration transform', async () => {
43+
harness.useTarget('test', {
44+
...BASE_OPTIONS,
45+
});
46+
47+
const webpackConfiguration: ExecutionTransformer<Configuration> = (config: Configuration) => {
48+
config.module ??= {};
49+
config.module.rules ??= [];
50+
config.module.rules.push({
51+
test: /\.number$/,
52+
loader: './src/number-webpack-loader.js',
53+
});
54+
return config;
55+
};
56+
57+
const { result } = await harness.executeOnce({
58+
additionalExecuteArguments: [
59+
{
60+
webpackConfiguration,
61+
},
62+
],
63+
});
64+
expect(result?.success).toBeTrue();
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)