forked from parcel-bundler/parcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONLDAsset.js
61 lines (54 loc) 路 1.56 KB
/
JSONLDAsset.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
const urlJoin = require('../utils/urlJoin');
const isURL = require('../utils/is-url');
const Asset = require('../Asset');
// A list of all attributes in a schema that may produce a dependency
// Based on https://schema.org/ImageObject
// Section "Instances of ImageObject may appear as values for the following properties"
const SCHEMA_ATTRS = [
'logo',
'photo',
'image',
'thumbnail',
'screenshot',
'primaryImageOfPage'
];
class JSONLDAsset extends Asset {
constructor(name, options) {
super(name, options);
this.type = 'jsonld';
}
parse(content) {
return JSON.parse(content.trim());
}
collectDependencies() {
for (let schemaKey in this.ast) {
// only check for single values, not nested data
// todo: check for nested data
if (
SCHEMA_ATTRS.includes(schemaKey) &&
typeof this.ast[schemaKey] === 'string'
) {
// paths aren't allowed, values must be urls
let assetPath = this.addURLDependency(this.ast[schemaKey]);
if (!isURL(assetPath)) {
assetPath = urlJoin(this.options.publicURL, assetPath);
}
this.ast[schemaKey] = assetPath;
this.isAstDirty = true;
if (this.options.publicURL === '/') {
console.warn(
"Please specify publicURL using --public-url, otherwise schema asset links won't work"
);
}
}
}
}
generate() {
if (this.options.production) {
return JSON.stringify(this.ast);
} else {
return JSON.stringify(this.ast, null, 2);
}
}
}
module.exports = JSONLDAsset;