Skip to content

Commit 1399e7f

Browse files
committed
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.
1 parent 9d3aa46 commit 1399e7f

File tree

32 files changed

+101
-117
lines changed

32 files changed

+101
-117
lines changed

goldens/public-api/angular_devkit/build_angular/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export interface AssetPatternObject {
7979
glob: string;
8080
ignore?: string[];
8181
input: string;
82-
output: string;
82+
output?: string;
8383
}
8484

8585
// @public

packages/angular/pwa/pwa/files/root/manifest.webmanifest renamed to packages/angular/pwa/pwa/files/assets/manifest.webmanifest

File renamed without changes.

packages/angular/pwa/pwa/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
externalSchematic,
1616
mergeWith,
1717
move,
18-
template,
1918
url,
2019
} from '@angular-devkit/schematics';
2120
import { readWorkspace, writeWorkspace } from '@schematics/angular/utility';
@@ -104,23 +103,6 @@ export default function (options: PwaOptions): Rule {
104103
}
105104
}
106105

107-
// Add manifest to asset configuration
108-
const assetEntry = posix.join(
109-
project.sourceRoot ?? posix.join(project.root, 'src'),
110-
'manifest.webmanifest',
111-
);
112-
for (const target of [...buildTargets, ...testTargets]) {
113-
if (target.options) {
114-
if (Array.isArray(target.options.assets)) {
115-
target.options.assets.push(assetEntry);
116-
} else {
117-
target.options.assets = [assetEntry];
118-
}
119-
} else {
120-
target.options = { assets: [assetEntry] };
121-
}
122-
}
123-
124106
// Find all index.html files in build targets
125107
const indexFiles = new Set<string>();
126108
for (const target of buildTargets) {
@@ -146,11 +128,32 @@ export default function (options: PwaOptions): Rule {
146128
const { title, ...swOptions } = options;
147129

148130
await writeWorkspace(host, workspace);
131+
let assetsDir = posix.join(sourcePath, 'assets');
132+
133+
if (host.exists(assetsDir)) {
134+
// Add manifest to asset configuration
135+
const assetEntry = posix.join(
136+
project.sourceRoot ?? posix.join(project.root, 'src'),
137+
'manifest.webmanifest',
138+
);
139+
for (const target of [...buildTargets, ...testTargets]) {
140+
if (target.options) {
141+
if (Array.isArray(target.options.assets)) {
142+
target.options.assets.push(assetEntry);
143+
} else {
144+
target.options.assets = [assetEntry];
145+
}
146+
} else {
147+
target.options = { assets: [assetEntry] };
148+
}
149+
}
150+
} else {
151+
assetsDir = posix.join(project.root, 'public');
152+
}
149153

150154
return chain([
151155
externalSchematic('@schematics/angular', 'service-worker', swOptions),
152-
mergeWith(apply(url('./files/root'), [template({ ...options }), move(sourcePath)])),
153-
mergeWith(apply(url('./files/assets'), [move(posix.join(sourcePath, 'assets'))])),
156+
mergeWith(apply(url('./files/assets'), [move(assetsDir)])),
154157
...[...indexFiles].map((path) => updateIndexFile(path)),
155158
]);
156159
};

packages/angular/pwa/pwa/index_spec.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ describe('PWA Schematic', () => {
7474

7575
it('should create a manifest file', async () => {
7676
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
77-
expect(tree.exists('/projects/bar/src/manifest.webmanifest')).toBeTrue();
77+
expect(tree.exists('/projects/bar/public/manifest.webmanifest')).toBeTrue();
7878
});
7979

8080
it('should set the name & short_name in the manifest file', async () => {
8181
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
8282

83-
const manifestText = tree.readContent('/projects/bar/src/manifest.webmanifest');
83+
const manifestText = tree.readContent('/projects/bar/public/manifest.webmanifest');
8484
const manifest = JSON.parse(manifestText);
8585

8686
expect(manifest.name).toEqual(defaultOptions.title);
@@ -91,7 +91,7 @@ describe('PWA Schematic', () => {
9191
const options = { ...defaultOptions, title: undefined };
9292
const tree = await schematicRunner.runSchematic('ng-add', options, appTree);
9393

94-
const manifestText = tree.readContent('/projects/bar/src/manifest.webmanifest');
94+
const manifestText = tree.readContent('/projects/bar/public/manifest.webmanifest');
9595
const manifest = JSON.parse(manifestText);
9696

9797
expect(manifest.name).toEqual(defaultOptions.project);
@@ -125,17 +125,6 @@ describe('PWA Schematic', () => {
125125
expect(content).toMatch(/<noscript>NO JAVASCRIPT<\/noscript>/);
126126
});
127127

128-
it('should update the build and test assets configuration', async () => {
129-
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
130-
const configText = tree.readContent('/angular.json');
131-
const config = JSON.parse(configText);
132-
const targets = config.projects.bar.architect;
133-
134-
['build', 'test'].forEach((target) => {
135-
expect(targets[target].options.assets).toContain('projects/bar/src/manifest.webmanifest');
136-
});
137-
});
138-
139128
describe('Legacy browser builder', () => {
140129
function convertBuilderToLegacyBrowser(): void {
141130
const config = JSON.parse(appTree.readContent('/angular.json'));

packages/angular_devkit/build_angular/src/builders/application/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,12 @@
560560
},
561561
"output": {
562562
"type": "string",
563+
"default": "",
563564
"description": "Absolute path within the output."
564565
}
565566
},
566567
"additionalProperties": false,
567-
"required": ["glob", "input", "output"]
568+
"required": ["glob", "input"]
568569
},
569570
{
570571
"type": "string"

packages/angular_devkit/build_angular/src/builders/browser-esbuild/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,12 @@
466466
},
467467
"output": {
468468
"type": "string",
469+
"default": "",
469470
"description": "Absolute path within the output."
470471
}
471472
},
472473
"additionalProperties": false,
473-
"required": ["glob", "input", "output"]
474+
"required": ["glob", "input"]
474475
},
475476
{
476477
"type": "string"

packages/angular_devkit/build_angular/src/builders/browser/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,12 @@
454454
},
455455
"output": {
456456
"type": "string",
457+
"default": "",
457458
"description": "Absolute path within the output."
458459
}
459460
},
460461
"additionalProperties": false,
461-
"required": ["glob", "input", "output"]
462+
"required": ["glob", "input"]
462463
},
463464
{
464465
"type": "string"

packages/angular_devkit/build_angular/src/builders/karma/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
},
291291
"output": {
292292
"type": "string",
293+
"default": "",
293294
"description": "Absolute path within the output."
294295
},
295296
"ignore": {
@@ -301,7 +302,7 @@
301302
}
302303
},
303304
"additionalProperties": false,
304-
"required": ["glob", "input", "output"]
305+
"required": ["glob", "input"]
305306
},
306307
{
307308
"type": "string"

packages/angular_devkit/build_angular/src/builders/server/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,12 @@
251251
},
252252
"output": {
253253
"type": "string",
254+
"default": "",
254255
"description": "Absolute path within the output."
255256
}
256257
},
257258
"additionalProperties": false,
258-
"required": ["glob", "input", "output"]
259+
"required": ["glob", "input"]
259260
},
260261
{
261262
"type": "string"

packages/angular_devkit/build_angular/src/builders/web-test-runner/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
},
270270
"output": {
271271
"type": "string",
272+
"default": "",
272273
"description": "Absolute path within the output."
273274
},
274275
"ignore": {
@@ -280,7 +281,7 @@
280281
}
281282
},
282283
"additionalProperties": false,
283-
"required": ["glob", "input", "output"]
284+
"required": ["glob", "input"]
284285
},
285286
{
286287
"type": "string"

0 commit comments

Comments
 (0)