diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts index 1aaea15da4a3..11402c465ddc 100644 --- a/packages/angular/pwa/pwa/index.ts +++ b/packages/angular/pwa/pwa/index.ts @@ -13,6 +13,7 @@ import { apply, chain, externalSchematic, + filter, mergeWith, move, template, @@ -151,15 +152,20 @@ export default function (options: PwaOptions): Rule { // Setup service worker schematic options const { title, ...swOptions } = options; - await writeWorkspace(host, workspace); - let assetsDir = posix.join(sourcePath, 'assets'); + const assetsDir = posix.join(sourcePath, 'assets'); + // `Tree.exists()` only matches files, so probe the directory for contained entries. + const assetsDirEntry = host.getDir(assetsDir); + const hasAssetsDir = + !host.exists(assetsDir) && + (assetsDirEntry.subfiles.length > 0 || assetsDirEntry.subdirs.length > 0); + let iconsPath: string; - if (host.exists(assetsDir)) { - // Add manifest to asset configuration - const assetEntry = posix.join( - project.sourceRoot ?? posix.join(project.root, 'src'), - 'manifest.webmanifest', - ); + let manifestDir: string; + let iconsDir: string; + if (hasAssetsDir) { + // Pre-v18 project layout: the manifest is served from the application root and the + // icons from the existing `assets` directory, which the build already copies. + const assetEntry = posix.join(sourcePath, 'manifest.webmanifest'); for (const target of [...buildTargets, ...testTargets]) { if (target.options) { if (Array.isArray(target.options.assets)) { @@ -171,16 +177,31 @@ export default function (options: PwaOptions): Rule { target.options = { assets: [assetEntry] }; } } - iconsPath = 'assets'; + iconsPath = 'assets/icons'; + manifestDir = sourcePath; + iconsDir = assetsDir; } else { - assetsDir = posix.join(project.root, 'public'); iconsPath = 'icons'; + manifestDir = iconsDir = posix.join(project.root, 'public'); } + // Written after the asset additions above so that they are persisted. + await writeWorkspace(host, workspace); + return chain([ externalSchematic('@schematics/angular', 'service-worker', swOptions), mergeWith( - apply(url('./files/assets'), [template({ ...options, iconsPath }), move(assetsDir)]), + apply(url('./files/assets'), [ + filter((path) => !path.startsWith('/icons/')), + template({ ...options, iconsPath }), + move(manifestDir), + ]), + ), + mergeWith( + apply(url('./files/assets'), [ + filter((path) => path.startsWith('/icons/')), + move(iconsDir), + ]), ), ...[...indexFiles].map((path) => updateIndexFile(path)), ]); diff --git a/packages/angular/pwa/pwa/index_spec.ts b/packages/angular/pwa/pwa/index_spec.ts index d8393d405623..3fc580ce61e1 100644 --- a/packages/angular/pwa/pwa/index_spec.ts +++ b/packages/angular/pwa/pwa/index_spec.ts @@ -222,4 +222,43 @@ describe('PWA Schematic', () => { expect(swFlag).toBe('projects/bar/ngsw-config.json'); }); }); + + describe('legacy src/assets project layout', () => { + beforeEach(() => { + appTree.create('/projects/bar/src/assets/.gitkeep', ''); + appTree.delete('/projects/bar/public/favicon.ico'); + + const config = JSON.parse(appTree.readContent('/angular.json')); + const build = config.projects.bar.architect.build; + build.options.assets = ['projects/bar/src/favicon.ico', 'projects/bar/src/assets']; + appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); + }); + + it('should place the manifest at the source root and the icons in the assets directory', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + expect(tree.exists('/projects/bar/src/manifest.webmanifest')).toBeTrue(); + expect(tree.exists('/projects/bar/src/assets/icons/icon-72x72.png')).toBeTrue(); + expect(tree.exists('/projects/bar/public/manifest.webmanifest')).toBeFalse(); + expect(tree.exists('/projects/bar/public/icons/icon-72x72.png')).toBeFalse(); + }); + + it('should reference the icons relative to the application root in the manifest', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + const manifest = JSON.parse(tree.readContent('/projects/bar/src/manifest.webmanifest')); + for (const icon of manifest.icons) { + expect(icon.src).toMatch(/^assets\/icons\/icon-\d+x\d+\.png/); + } + }); + + it('should add the manifest to the build assets in the workspace configuration', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + const config = JSON.parse(tree.readContent('/angular.json')); + expect(config.projects.bar.architect.build.options.assets).toContain( + 'projects/bar/src/manifest.webmanifest', + ); + }); + }); });