From 6530aa11bed5ef67d611e8aed268bd20345cf0e6 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Mon, 25 Mar 2024 14:52:48 +0000 Subject: [PATCH] feat(@schematics/angular): replace `assets` with `public` directory The `assets` directory is confusing for the users and commonly users place "assets" which are not meant to be copied but instead processed by the build system. This causes some files both bundled and copied. With this change we rename the `assets` directory to `public` and also move the `favicon.ico` inside this newly created directory. --- .../angular_devkit/build_angular/index.md | 2 +- .../{root => assets}/manifest.webmanifest | 0 packages/angular/pwa/pwa/index.ts | 42 ++++++++++-------- packages/angular/pwa/pwa/index_spec.ts | 19 ++------ .../src/builders/application/schema.json | 3 +- .../src/builders/browser-esbuild/schema.json | 3 +- .../src/builders/browser/schema.json | 3 +- .../src/builders/karma/schema.json | 3 +- .../src/builders/server/schema.json | 3 +- .../src/builders/web-test-runner/schema.json | 3 +- .../src/tools/webpack/utils/helpers.ts | 2 +- .../src/utils/normalize-asset-patterns.ts | 9 ++-- .../{src => public}/favicon.ico.template | Bin .../common-files/src/assets/.gitkeep.template | 0 .../schematics/angular/application/index.ts | 4 +- .../angular/application/index_spec.ts | 12 ++--- .../files/ngsw-config.json.template | 3 +- .../angular/service-worker/index.ts | 6 --- .../angular/service-worker/index_spec.ts | 21 --------- tests/legacy-cli/e2e/tests/build/assets.ts | 22 ++++----- tests/legacy-cli/e2e/tests/build/css-urls.ts | 7 ++- .../e2e/tests/build/multiple-configs.ts | 1 - .../build/prerender/http-requests-assets.ts | 4 +- .../e2e/tests/commands/add/add-pwa.ts | 7 +-- .../e2e/tests/commands/add/npm-env-vars.ts | 8 ++-- .../e2e/tests/commands/add/registry-option.ts | 2 +- .../e2e/tests/commands/add/secure-registry.ts | 8 ++-- .../e2e/tests/commands/add/yarn-env-vars.ts | 8 ++-- .../e2e/tests/commands/config/config-get.ts | 8 ++-- .../e2e/tests/commands/serve/assets.ts | 12 ++--- .../serve/ssr-http-requests-assets.ts | 4 +- tests/legacy-cli/e2e/utils/fs.ts | 2 +- 32 files changed, 102 insertions(+), 129 deletions(-) rename packages/angular/pwa/pwa/files/{root => assets}/manifest.webmanifest (100%) rename packages/schematics/angular/application/files/common-files/{src => public}/favicon.ico.template (100%) delete mode 100644 packages/schematics/angular/application/files/common-files/src/assets/.gitkeep.template diff --git a/goldens/public-api/angular_devkit/build_angular/index.md b/goldens/public-api/angular_devkit/build_angular/index.md index f912d7001e8c..19a46f29ecdf 100644 --- a/goldens/public-api/angular_devkit/build_angular/index.md +++ b/goldens/public-api/angular_devkit/build_angular/index.md @@ -79,7 +79,7 @@ export interface AssetPatternObject { glob: string; ignore?: string[]; input: string; - output: string; + output?: string; } // @public diff --git a/packages/angular/pwa/pwa/files/root/manifest.webmanifest b/packages/angular/pwa/pwa/files/assets/manifest.webmanifest similarity index 100% rename from packages/angular/pwa/pwa/files/root/manifest.webmanifest rename to packages/angular/pwa/pwa/files/assets/manifest.webmanifest diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts index f817c4764905..2710247a619f 100644 --- a/packages/angular/pwa/pwa/index.ts +++ b/packages/angular/pwa/pwa/index.ts @@ -104,23 +104,6 @@ export default function (options: PwaOptions): Rule { } } - // Add manifest to asset configuration - const assetEntry = posix.join( - project.sourceRoot ?? posix.join(project.root, 'src'), - 'manifest.webmanifest', - ); - for (const target of [...buildTargets, ...testTargets]) { - if (target.options) { - if (Array.isArray(target.options.assets)) { - target.options.assets.push(assetEntry); - } else { - target.options.assets = [assetEntry]; - } - } else { - target.options = { assets: [assetEntry] }; - } - } - // Find all index.html files in build targets const indexFiles = new Set(); for (const target of buildTargets) { @@ -146,11 +129,32 @@ export default function (options: PwaOptions): Rule { const { title, ...swOptions } = options; await writeWorkspace(host, workspace); + let assetsDir = posix.join(sourcePath, 'assets'); + + if (host.exists(assetsDir)) { + // Add manifest to asset configuration + const assetEntry = posix.join( + project.sourceRoot ?? posix.join(project.root, 'src'), + 'manifest.webmanifest', + ); + for (const target of [...buildTargets, ...testTargets]) { + if (target.options) { + if (Array.isArray(target.options.assets)) { + target.options.assets.push(assetEntry); + } else { + target.options.assets = [assetEntry]; + } + } else { + target.options = { assets: [assetEntry] }; + } + } + } else { + assetsDir = posix.join(project.root, 'public'); + } return chain([ externalSchematic('@schematics/angular', 'service-worker', swOptions), - mergeWith(apply(url('./files/root'), [template({ ...options }), move(sourcePath)])), - mergeWith(apply(url('./files/assets'), [move(posix.join(sourcePath, 'assets'))])), + mergeWith(apply(url('./files/assets'), [template({ ...options }), move(assetsDir)])), ...[...indexFiles].map((path) => updateIndexFile(path)), ]); }; diff --git a/packages/angular/pwa/pwa/index_spec.ts b/packages/angular/pwa/pwa/index_spec.ts index e6b0d4e576bb..e538af1ee625 100644 --- a/packages/angular/pwa/pwa/index_spec.ts +++ b/packages/angular/pwa/pwa/index_spec.ts @@ -54,7 +54,7 @@ describe('PWA Schematic', () => { it('should create icon files', async () => { const dimensions = [72, 96, 128, 144, 152, 192, 384, 512]; - const iconPath = '/projects/bar/src/assets/icons/icon-'; + const iconPath = '/projects/bar/public/icons/icon-'; const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); dimensions.forEach((d) => { @@ -74,13 +74,13 @@ describe('PWA Schematic', () => { it('should create a manifest file', async () => { const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); - expect(tree.exists('/projects/bar/src/manifest.webmanifest')).toBeTrue(); + expect(tree.exists('/projects/bar/public/manifest.webmanifest')).toBeTrue(); }); it('should set the name & short_name in the manifest file', async () => { const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); - const manifestText = tree.readContent('/projects/bar/src/manifest.webmanifest'); + const manifestText = tree.readContent('/projects/bar/public/manifest.webmanifest'); const manifest = JSON.parse(manifestText); expect(manifest.name).toEqual(defaultOptions.title); @@ -91,7 +91,7 @@ describe('PWA Schematic', () => { const options = { ...defaultOptions, title: undefined }; const tree = await schematicRunner.runSchematic('ng-add', options, appTree); - const manifestText = tree.readContent('/projects/bar/src/manifest.webmanifest'); + const manifestText = tree.readContent('/projects/bar/public/manifest.webmanifest'); const manifest = JSON.parse(manifestText); expect(manifest.name).toEqual(defaultOptions.project); @@ -125,17 +125,6 @@ describe('PWA Schematic', () => { expect(content).toMatch(/