/
WebManifestTransformer.js
68 lines (62 loc) 路 1.68 KB
/
WebManifestTransformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// @flow
// https://developer.mozilla.org/en-US/docs/Web/Manifest
import type {SchemaEntity} from '@parcel/utils';
import invariant from 'assert';
import {parse} from '@mischnic/json-sourcemap';
import {getJSONSourceLocation} from '@parcel/diagnostic';
import {Transformer} from '@parcel/plugin';
import {validateSchema} from '@parcel/utils';
const RESOURCES_SCHEMA = {
type: 'array',
items: {
type: 'object',
properties: {
src: {
type: 'string',
__validate: s => {
if (s.length === 0) {
return 'Must not be empty';
}
},
},
},
required: ['src'],
},
};
const MANIFEST_SCHEMA: SchemaEntity = {
type: 'object',
properties: {
icons: RESOURCES_SCHEMA,
screenshots: RESOURCES_SCHEMA,
},
};
export default (new Transformer({
async transform({asset}) {
const source = await asset.getCode();
const {data, pointers} = parse(source);
validateSchema.diagnostic(
MANIFEST_SCHEMA,
{source, map: {data, pointers}, filePath: asset.filePath},
'@parcel/transformer-webmanifest',
'Invalid webmanifest',
);
for (const key of ['icons', 'screenshots']) {
const list = data[key];
if (list) {
invariant(Array.isArray(list));
for (let i = 0; i < list.length; i++) {
const res = list[i];
res.src = asset.addURLDependency(res.src, {
loc: {
filePath: asset.filePath,
...getJSONSourceLocation(pointers[`/${key}/${i}/src`], 'value'),
},
});
}
}
}
asset.type = 'webmanifest';
asset.setCode(JSON.stringify(data));
return [asset];
},
}): Transformer);