From f0d4fc49beceb96ffc3d836e911ac622221e62d9 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 13 Jun 2025 14:12:07 -0400 Subject: [PATCH] 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. --- .../src/builders/karma/application_builder.ts | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts index 3bfc13df6c74..d33469a45ef6 100644 --- a/packages/angular/build/src/builders/karma/application_builder.ts +++ b/packages/angular/build/src/builders/karma/application_builder.ts @@ -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 @@ -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'), @@ -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(); + 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 }, @@ -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);