@@ -136,11 +136,7 @@ function normalizePolyfills(polyfills) {
136136async function collectEntrypoints ( options , context , projectSourceRoot ) {
137137 // Glob for files to test.
138138 const testFiles = await ( 0 , find_tests_1 . findTests ) ( options . include ?? [ ] , options . exclude ?? [ ] , context . workspaceRoot , projectSourceRoot ) ;
139- const entryPoints = new Set ( [
140- ...testFiles ,
141- '@angular-devkit/build-angular/src/builders/karma/init_test_bed.js' ,
142- ] ) ;
143- return entryPoints ;
139+ return new Set ( testFiles ) ;
144140}
145141async function initializeApplication ( options , context , karmaOptions , transforms = { } ) {
146142 if ( transforms . webpackConfiguration ) {
@@ -153,6 +149,14 @@ async function initializeApplication(options, context, karmaOptions, transforms
153149 collectEntrypoints ( options , context , projectSourceRoot ) ,
154150 fs . rm ( outputPath , { recursive : true , force : true } ) ,
155151 ] ) ;
152+ let mainName = 'init_test_bed' ;
153+ if ( options . main ) {
154+ entryPoints . add ( options . main ) ;
155+ mainName = path . basename ( options . main , path . extname ( options . main ) ) ;
156+ }
157+ else {
158+ entryPoints . add ( '@angular-devkit/build-angular/src/builders/karma/init_test_bed.js' ) ;
159+ }
156160 const instrumentForCoverage = options . codeCoverage
157161 ? createInstrumentationFilter ( projectSourceRoot , getInstrumentationExcludedPaths ( context . workspaceRoot , options . codeCoverageExclude ?? [ ] ) )
158162 : undefined ;
@@ -188,8 +192,15 @@ async function initializeApplication(options, context, karmaOptions, transforms
188192 karmaOptions . files . push (
189193 // Serve polyfills first.
190194 { pattern : `${ outputPath } /polyfills.js` , type : 'module' } ,
191- // Allow loading of chunk-* files but don't include them all on load.
192- { pattern : `${ outputPath } /{chunk,worker}-*.js` , type : 'module' , included : false } ) ;
195+ // Serve global setup script.
196+ { pattern : `${ outputPath } /${ mainName } .js` , type : 'module' } ,
197+ // Serve all source maps.
198+ { pattern : `${ outputPath } /*.map` , included : false } ) ;
199+ if ( hasChunkOrWorkerFiles ( buildOutput . files ) ) {
200+ karmaOptions . files . push (
201+ // Allow loading of chunk-* files but don't include them all on load.
202+ { pattern : `${ outputPath } /{chunk,worker}-*.js` , type : 'module' , included : false } ) ;
203+ }
193204 karmaOptions . files . push (
194205 // Serve remaining JS on page load, these are the test entrypoints.
195206 { pattern : `${ outputPath } /*.js` , type : 'module' } ) ;
@@ -221,6 +232,11 @@ async function initializeApplication(options, context, karmaOptions, transforms
221232 }
222233 return [ karma , parsedKarmaConfig , buildOptions ] ;
223234}
235+ function hasChunkOrWorkerFiles ( files ) {
236+ return Object . keys ( files ) . some ( ( filename ) => {
237+ return / (?: ^ | \/ ) (?: w o r k e r | c h u n k ) [ ^ / ] + \. j s $ / . test ( filename ) ;
238+ } ) ;
239+ }
224240async function writeTestFiles ( files , testDir ) {
225241 const directoryExists = new Set ( ) ;
226242 // Writes the test related output files to disk and ensures the containing directories are present
0 commit comments