Skip to content

Commit

Permalink
Parse shortcut icons in web app manifests (#8660)
Browse files Browse the repository at this point in the history
  • Loading branch information
grimsteel committed Nov 27, 2022
1 parent c0f5351 commit bfb707e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
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

0 comments on commit bfb707e

Please sign in to comment.