Skip to content
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

Parse shortcut icons in web app manifests #8660

Merged
merged 5 commits into from Nov 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,5 +13,17 @@
"sizes": "100x100",
"type": "image/png"
}
],
"shortcuts": [
{
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
]
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -12,5 +12,24 @@
},
{}
],
"screenshots": {}
"screenshots": {},
"shortcuts": [
{
"icons": {}
},
{
"icons": [
{
"src": "icon.png",
"sizes": "100x100",
"type": "image/png"
},
{
"sizes": "100x100",
"type": "image/png"
},
{}
]
}
]
}
Expand Up @@ -13,5 +13,17 @@
"sizes": "100x100",
"type": "image/png"
}
],
"shortcuts": [
{
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
]
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions packages/core/integration-tests/test/webmanifest.js
Expand Up @@ -26,6 +26,10 @@ describe('webmanifest', function () {
type: 'png',
assets: ['screenshot.png'],
},
{
type: 'png',
assets: ['shortcut-icon.png'],
},
]);

const manifest = await outputFS.readFile(
Expand All @@ -34,6 +38,7 @@ describe('webmanifest', function () {
);
assert(/screenshot\.[0-9a-f]+\.png/.test(manifest));
assert(/icon\.[0-9a-f]+\.png/.test(manifest));
assert(/shortcut-icon\.[0-9a-f]+\.png/.test(manifest));
});

it('should support .json', async function () {
Expand All @@ -58,6 +63,10 @@ describe('webmanifest', function () {
type: 'png',
assets: ['screenshot.png'],
},
{
type: 'png',
assets: ['shortcut-icon.png'],
},
]);

const manifest = await outputFS.readFile(
Expand All @@ -66,6 +75,7 @@ describe('webmanifest', function () {
);
assert(/screenshot\.[0-9a-f]+\.png/.test(manifest));
assert(/icon\.[0-9a-f]+\.png/.test(manifest));
assert(/shortcut-icon\.[0-9a-f]+\.png/.test(manifest));
});

it('should throw on malformed icons and screenshots', async function () {
Expand Down Expand Up @@ -124,6 +134,39 @@ describe('webmanifest', function () {
line: 15,
},
},
{
end: {
column: 17,
line: 18,
},
message: 'Expected type array',
start: {
column: 16,
line: 18,
},
},
{
end: {
column: 9,
line: 30,
},
message: 'Missing property src',
start: {
column: 9,
line: 27,
},
},
{
end: {
column: 10,
line: 31,
},
message: 'Missing property src',
start: {
column: 9,
line: 31,
},
},
],
},
],
Expand Down
31 changes: 31 additions & 0 deletions packages/transformers/webmanifest/src/WebManifestTransformer.js
Expand Up @@ -30,6 +30,15 @@ const MANIFEST_SCHEMA: SchemaEntity = {
properties: {
icons: RESOURCES_SCHEMA,
screenshots: RESOURCES_SCHEMA,
shortcuts: {
type: 'array',
items: {
type: 'object',
properties: {
icons: RESOURCES_SCHEMA,
},
},
},
},
};

Expand Down Expand Up @@ -61,6 +70,28 @@ export default (new Transformer({
}
}

if (data.shortcuts) {
invariant(Array.isArray(data.shortcuts));
for (let i = 0; i < data.shortcuts.length; i++) {
const list = data.shortcuts[i].icons;
if (list) {
invariant(Array.isArray(list));
for (let j = 0; j < list.length; j++) {
const res = list[j];
res.src = asset.addURLDependency(res.src, {
loc: {
filePath: asset.filePath,
...getJSONSourceLocation(
pointers[`/shortcuts/${i}/icons/${j}/src`],
'value',
),
},
});
}
}
}
}

asset.type = 'webmanifest';
asset.setCode(JSON.stringify(data));
return [asset];
Expand Down