Skip to content

Commit 6530aa1

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 6530aa1

File tree

32 files changed

+102
-129
lines changed

32 files changed

+102
-129
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/index.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,6 @@ export default function (options: PwaOptions): Rule {
104104
}
105105
}
106106

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-
124107
// Find all index.html files in build targets
125108
const indexFiles = new Set<string>();
126109
for (const target of buildTargets) {
@@ -146,11 +129,32 @@ export default function (options: PwaOptions): Rule {
146129
const { title, ...swOptions } = options;
147130

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

150155
return chain([
151156
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'))])),
157+
mergeWith(apply(url('./files/assets'), [template({ ...options }), move(assetsDir)])),
154158
...[...indexFiles].map((path) => updateIndexFile(path)),
155159
]);
156160
};

packages/angular/pwa/pwa/index_spec.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('PWA Schematic', () => {
5454

5555
it('should create icon files', async () => {
5656
const dimensions = [72, 96, 128, 144, 152, 192, 384, 512];
57-
const iconPath = '/projects/bar/src/assets/icons/icon-';
57+
const iconPath = '/projects/bar/public/icons/icon-';
5858
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
5959

6060
dimensions.forEach((d) => {
@@ -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)