Skip to content

fix(@angular/build): include custom bundle name scripts with karma #30536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions packages/angular/build/src/builders/karma/application_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class AngularPolyfillsPlugin {
static createPlugin(
polyfillsFile: FilePattern,
jasmineCleanupFiles: FilePattern,
scriptsFiles: FilePattern[],
): InlinePluginDef {
return {
// This has to be a "reporter" because reporters run _after_ frameworks
Expand Down Expand Up @@ -161,16 +162,14 @@ class AngularPolyfillsPlugin {
// page load. `type` won't affect them.
continue;
}
if (f.pattern === 'scripts.js') {
// Don't consider "scripts" option files as module types.
// This should be expanded if custom scripts bundle names support is added.
continue;
}
if (f.pattern.endsWith('.js') && 'js' === (f.type ?? 'js')) {
f.type = 'module';
}
}

// Add "scripts" option files as classic scripts
files.unshift(...scriptsFiles);

// Add browser sourcemap support as a classic script
files.unshift({
pattern: localResolve('source-map-support/browser-source-map-support.js'),
Expand Down Expand Up @@ -493,17 +492,28 @@ async function initializeApplication(

karmaOptions.basePath = outputPath;

karmaOptions.files ??= [];
const scriptsFiles: FilePattern[] = [];
if (options.scripts?.length) {
// This should be more granular to support named bundles.
// However, it replicates the behavior of the Karma Webpack-based builder.
karmaOptions.files.push({
pattern: `scripts.js`,
watched: false,
type: 'js',
});
const outputScripts = new Set<string>();
for (const scriptEntry of options.scripts) {
const outputName =
typeof scriptEntry === 'string'
? 'scripts.js'
: `${scriptEntry.bundleName ?? 'scripts'}.js`;

if (outputScripts.has(outputName)) {
continue;
}
outputScripts.add(outputName);
scriptsFiles.push({
pattern: `${outputPath}/${outputName}`,
watched: false,
type: 'js',
});
}
}

karmaOptions.files ??= [];
karmaOptions.files.push(
// Serve global setup script.
{ pattern: `${mainName}.js`, type: 'module', watched: false },
Expand Down Expand Up @@ -577,7 +587,7 @@ async function initializeApplication(
parsedKarmaConfig.middleware.push(AngularAssetsMiddleware.NAME);

parsedKarmaConfig.plugins.push(
AngularPolyfillsPlugin.createPlugin(polyfillsFile, jasmineCleanupFiles),
AngularPolyfillsPlugin.createPlugin(polyfillsFile, jasmineCleanupFiles, scriptsFiles),
);
parsedKarmaConfig.reporters ??= [];
parsedKarmaConfig.reporters.push(AngularPolyfillsPlugin.NAME);
Expand Down
Loading