diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts index a3a2b2847c78..1aaea15da4a3 100644 --- a/packages/angular/pwa/pwa/index.ts +++ b/packages/angular/pwa/pwa/index.ts @@ -108,23 +108,32 @@ export default function (options: PwaOptions): Rule { // Find all index.html files in build targets const indexFiles = new Set(); let checkForDefaultIndex = false; - for (const target of buildTargets) { - if (typeof target.options?.index === 'string') { - indexFiles.add(target.options.index); - } else if (target.options?.index === undefined) { + const collectIndexFile = (index: unknown): void => { + if (typeof index === 'string') { + indexFiles.add(index); + } else if (index === undefined) { checkForDefaultIndex = true; + } else if ( + typeof index === 'object' && + index !== null && + 'input' in index && + typeof index.input === 'string' + ) { + // The `index` option also accepts an `{ input, output }` object; the manifest + // link belongs in the source file referenced by `input`. + indexFiles.add(index.input); } + }; + + for (const target of buildTargets) { + collectIndexFile(target.options?.index); if (!target.configurations) { continue; } for (const options of Object.values(target.configurations)) { - if (typeof options?.index === 'string') { - indexFiles.add(options.index); - } else if (options?.index === undefined) { - checkForDefaultIndex = true; - } + collectIndexFile(options?.index); } } diff --git a/packages/angular/pwa/pwa/index_spec.ts b/packages/angular/pwa/pwa/index_spec.ts index 42d698ce8f8f..d8393d405623 100644 --- a/packages/angular/pwa/pwa/index_spec.ts +++ b/packages/angular/pwa/pwa/index_spec.ts @@ -117,6 +117,21 @@ describe('PWA Schematic', () => { ); }); + it('should update the index file when it is specified as an object', async () => { + const config = JSON.parse(appTree.readContent('/angular.json')); + config.projects.bar.architect.build.options.index = { + input: 'projects/bar/src/app-index.html', + output: 'index.html', + }; + appTree.overwrite('/angular.json', JSON.stringify(config, null, 2)); + appTree.rename('/projects/bar/src/index.html', '/projects/bar/src/app-index.html'); + + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + const content = tree.readContent('projects/bar/src/app-index.html'); + + expect(content).toMatch(//); + }); + it('should not add noscript element to the index file if already present', async () => { let index = appTree.readContent('projects/bar/src/index.html'); index = index.replace('', '');