Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions packages/angular/pwa/pwa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,32 @@ export default function (options: PwaOptions): Rule {
// Find all index.html files in build targets
const indexFiles = new Set<string>();
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);
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/angular/pwa/pwa/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<link rel="manifest" href="manifest.webmanifest">/);
});

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('</body>', '<noscript>NO JAVASCRIPT</noscript></body>');
Expand Down
Loading