Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): fix webpack config transform for …
Browse files Browse the repository at this point in the history
…karma
  • Loading branch information
jkrems committed Dec 5, 2024
1 parent b8b561d commit fb41d18
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
13 changes: 12 additions & 1 deletion modules/testing/builder/src/builder-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export interface BuilderHarnessExecutionOptions {
outputLogsOnException: boolean;
useNativeFileWatching: boolean;
signal: AbortSignal;
additionalExecuteArguments: unknown[];
}

interface BuilderHandlerFnWithVarArgs<T> extends BuilderHandlerFn<T> {
(input: T, context: BuilderContext, ...args: unknown[]): BuilderOutputLike;
}

/**
Expand Down Expand Up @@ -256,7 +261,13 @@ export class BuilderHarness<T> {
mergeMap((validator) => validator(targetOptions)),
map((validationResult) => validationResult.data),
mergeMap((data) =>
convertBuilderOutputToObservable(this.builderHandler(data as T & json.JsonObject, context)),
convertBuilderOutputToObservable(
(this.builderHandler as BuilderHandlerFnWithVarArgs<T>)(
data as T & json.JsonObject,
context,
...(options.additionalExecuteArguments ?? []),
),
),
),
map((buildResult) => ({ result: buildResult, error: undefined })),
catchError((error) => {
Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ LARGE_SPECS = {
"@npm//karma-jasmine",
"@npm//karma-jasmine-html-reporter",
"@npm//puppeteer",
"@npm//webpack",
],
},
"protractor": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function execute(
karmaOptions?: (options: KarmaConfigOptions) => KarmaConfigOptions;
} = {},
): Observable<BuilderOutput> {
return from(initializeBrowser(options, context)).pipe(
return from(initializeBrowser(options, context, transforms.webpackConfiguration)).pipe(
switchMap(async ([karma, webpackConfig]) => {
const projectName = context.target?.project;
if (!projectName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { Configuration } from 'webpack';

import { execute } from '../../index';
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup';
import { ExecutionTransformer } from '../../../../transforms';

describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget, isApplicationBuilder) => {
describe('Option: Custom file loader', () => {
beforeEach(async () => {
if (isApplicationBuilder) {
pending('not implemented yet for application builder');
}
await setupTarget(harness);
});

beforeEach(async () => {
await harness.writeFiles({
'src/number-webpack-loader.js': `
module.exports = (source) => {
return 'export const DOUBLED = ' + (Number(source) * 2) + ';\\n';
};`,
'src/app/app.number': `42`,
'src/app/app.number.d.ts': `export const DOUBLED: number;`,
'src/app/app.component.spec.ts': `
import { DOUBLED } from './app.number';
describe('Custom webpack transform', () => {
it('generates expected export', () => {
expect(DOUBLED).toBe(84);
});
});`,
});
});

it('applies the webpack configuration transform', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
});

const webpackConfiguration: ExecutionTransformer<Configuration> = (config: Configuration) => {
config.module ??= {};
config.module.rules ??= [];
config.module.rules.push({
test: /\.number$/,
loader: './src/number-webpack-loader.js',
});
return config;
};

const { result } = await harness.executeOnce({
additionalExecuteArguments: [
{
webpackConfiguration,
},
],
});
expect(result?.success).toBeTrue();
});
});
});

0 comments on commit fb41d18

Please sign in to comment.