Skip to content

Commit 56f426e

Browse files
committed
fix(@angular/build): include custom bundle name scripts with karma
When using the `karma` builder with custom "scripts" option entries that contain the `bundleName` suboption, the output files for these scripts will now correctly be injected into the test index HTML content. Previously only the default `scripts.js` output file would be used. (cherry picked from commit 049e688)
1 parent e6d27bd commit 56f426e

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class AngularPolyfillsPlugin {
119119
static createPlugin(
120120
polyfillsFile: FilePattern,
121121
jasmineCleanupFiles: FilePattern,
122+
scriptsFiles: FilePattern[],
122123
): InlinePluginDef {
123124
return {
124125
// This has to be a "reporter" because reporters run _after_ frameworks
@@ -161,16 +162,14 @@ class AngularPolyfillsPlugin {
161162
// page load. `type` won't affect them.
162163
continue;
163164
}
164-
if (f.pattern === 'scripts.js') {
165-
// Don't consider "scripts" option files as module types.
166-
// This should be expanded if custom scripts bundle names support is added.
167-
continue;
168-
}
169165
if (f.pattern.endsWith('.js') && 'js' === (f.type ?? 'js')) {
170166
f.type = 'module';
171167
}
172168
}
173169

170+
// Add "scripts" option files as classic scripts
171+
files.unshift(...scriptsFiles);
172+
174173
// Add browser sourcemap support as a classic script
175174
files.unshift({
176175
pattern: localResolve('source-map-support/browser-source-map-support.js'),
@@ -493,17 +492,28 @@ async function initializeApplication(
493492

494493
karmaOptions.basePath = outputPath;
495494

496-
karmaOptions.files ??= [];
495+
const scriptsFiles: FilePattern[] = [];
497496
if (options.scripts?.length) {
498-
// This should be more granular to support named bundles.
499-
// However, it replicates the behavior of the Karma Webpack-based builder.
500-
karmaOptions.files.push({
501-
pattern: `scripts.js`,
502-
watched: false,
503-
type: 'js',
504-
});
497+
const outputScripts = new Set<string>();
498+
for (const scriptEntry of options.scripts) {
499+
const outputName =
500+
typeof scriptEntry === 'string'
501+
? 'scripts.js'
502+
: `${scriptEntry.bundleName ?? 'scripts'}.js`;
503+
504+
if (outputScripts.has(outputName)) {
505+
continue;
506+
}
507+
outputScripts.add(outputName);
508+
scriptsFiles.push({
509+
pattern: `${outputPath}/${outputName}`,
510+
watched: false,
511+
type: 'js',
512+
});
513+
}
505514
}
506515

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

579589
parsedKarmaConfig.plugins.push(
580-
AngularPolyfillsPlugin.createPlugin(polyfillsFile, jasmineCleanupFiles),
590+
AngularPolyfillsPlugin.createPlugin(polyfillsFile, jasmineCleanupFiles, scriptsFiles),
581591
);
582592
parsedKarmaConfig.reporters ??= [];
583593
parsedKarmaConfig.reporters.push(AngularPolyfillsPlugin.NAME);

0 commit comments

Comments
 (0)