Skip to content

fix(@schematics/angular): set builders assets option correctly for new applications #27717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2024
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
16 changes: 8 additions & 8 deletions packages/angular/pwa/pwa/files/assets/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,49 @@
"start_url": "./",
"icons": [
{
"src": "assets/icons/icon-72x72.png",
"src": "<%= iconsPath %>/icon-72x72.png",
"sizes": "72x72",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-96x96.png",
"src": "<%= iconsPath %>/icon-96x96.png",
"sizes": "96x96",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-128x128.png",
"src": "<%= iconsPath %>/icon-128x128.png",
"sizes": "128x128",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-144x144.png",
"src": "<%= iconsPath %>/icon-144x144.png",
"sizes": "144x144",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-152x152.png",
"src": "<%= iconsPath %>/icon-152x152.png",
"sizes": "152x152",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-192x192.png",
"src": "<%= iconsPath %>/icon-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-384x384.png",
"src": "<%= iconsPath %>/icon-384x384.png",
"sizes": "384x384",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "assets/icons/icon-512x512.png",
"src": "<%= iconsPath %>/icon-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
Expand Down
8 changes: 6 additions & 2 deletions packages/angular/pwa/pwa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default function (options: PwaOptions): Rule {

await writeWorkspace(host, workspace);
let assetsDir = posix.join(sourcePath, 'assets');

let iconsPath: string;
if (host.exists(assetsDir)) {
// Add manifest to asset configuration
const assetEntry = posix.join(
Expand All @@ -148,13 +148,17 @@ export default function (options: PwaOptions): Rule {
target.options = { assets: [assetEntry] };
}
}
iconsPath = 'assets';
} else {
assetsDir = posix.join(project.root, 'public');
iconsPath = 'icons';
}

return chain([
externalSchematic('@schematics/angular', 'service-worker', swOptions),
mergeWith(apply(url('./files/assets'), [template({ ...options }), move(assetsDir)])),
mergeWith(
apply(url('./files/assets'), [template({ ...options, iconsPath }), move(assetsDir)]),
),
...[...indexFiles].map((path) => updateIndexFile(path)),
]);
};
Expand Down
9 changes: 9 additions & 0 deletions packages/angular/pwa/pwa/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ describe('PWA Schematic', () => {
});
});

it('should reference the icons in the manifest correctly', async () => {
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
const manifestText = tree.readContent('/projects/bar/public/manifest.webmanifest');
const manifest = JSON.parse(manifestText);
for (const icon of manifest.icons) {
expect(icon.src).toMatch(/^icons\/icon-\d+x\d+.png/);
}
});

it('should run the service worker schematic', async () => {
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
const configText = tree.readContent('/angular.json');
Expand Down
4 changes: 2 additions & 2 deletions packages/schematics/angular/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function addAppToWorkspaceFile(
polyfills: ['zone.js'],
tsConfig: `${projectRoot}tsconfig.app.json`,
inlineStyleLanguage,
assets: [{ 'glob': '**/*', 'input': 'public' }],
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],
styles: [`${sourceRoot}/styles.${options.style}`],
scripts: [],
},
Expand Down Expand Up @@ -282,7 +282,7 @@ function addAppToWorkspaceFile(
polyfills: ['zone.js', 'zone.js/testing'],
tsConfig: `${projectRoot}tsconfig.spec.json`,
inlineStyleLanguage,
assets: [{ 'glob': '**/*', 'input': 'public' }],
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],
styles: [`${sourceRoot}/styles.${options.style}`],
scripts: [],
},
Expand Down
10 changes: 9 additions & 1 deletion packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ describe('Application Schematic', () => {

it('should set values in angular.json correctly', async () => {
const options = { ...defaultOptions, projectRoot: '' };

const tree = await schematicRunner.runSchematic('application', options, workspaceTree);

const config = JSON.parse(tree.readContent('/angular.json'));
Expand All @@ -286,12 +285,14 @@ describe('Application Schematic', () => {
const buildOpt = prj.architect.build.options;
expect(buildOpt.index).toEqual('src/index.html');
expect(buildOpt.browser).toEqual('src/main.ts');
expect(buildOpt.assets).toEqual([{ 'glob': '**/*', 'input': 'public' }]);
expect(buildOpt.polyfills).toEqual(['zone.js']);
expect(buildOpt.tsConfig).toEqual('tsconfig.app.json');

const testOpt = prj.architect.test.options;
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
expect(testOpt.karmaConfig).toBeUndefined();
expect(testOpt.assets).toEqual([{ 'glob': '**/*', 'input': 'public' }]);
expect(testOpt.styles).toEqual(['src/styles.css']);
});

Expand Down Expand Up @@ -377,6 +378,13 @@ describe('Application Schematic', () => {
expect(buildOpt.browser).toEqual('foo/src/main.ts');
expect(buildOpt.polyfills).toEqual(['zone.js']);
expect(buildOpt.tsConfig).toEqual('foo/tsconfig.app.json');
expect(buildOpt.assets).toEqual([{ 'glob': '**/*', 'input': 'foo/public' }]);

const testOpt = project.architect.test.options;
expect(testOpt.tsConfig).toEqual('foo/tsconfig.spec.json');
expect(testOpt.karmaConfig).toBeUndefined();
expect(testOpt.assets).toEqual([{ 'glob': '**/*', 'input': 'foo/public' }]);
expect(testOpt.styles).toEqual(['foo/src/styles.css']);

const appTsConfig = readJsonFile(tree, '/foo/tsconfig.app.json');
expect(appTsConfig.extends).toEqual('../tsconfig.json');
Expand Down