This repository has been archived by the owner on Oct 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlsAsset.js
58 lines (53 loc) · 1.71 KB
/
UrlsAsset.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
const Asset = require('parcel-bundler').Asset;
const isURL = require('./utils/is-url');
const urlJoin = require('./utils/urlJoin');
class UrlsAsset extends Asset {
constructor(...args) {
super(...args);
this.type = 'json';
}
async collectDependencies() {
for (let key of Object.keys(this.ast)) {
const originalPath = this.ast[key];
const assetPath = this.addURLDependency(originalPath);
if (!assetPath) throw new Error(`Cannot resolve dependency '${originalPath}'`);
if (!isURL(assetPath)) {
this.ast[key] = urlJoin(this.options.publicURL, assetPath);
} else {
this.ast[key] = assetPath;
}
}
}
parse(code) {
const mapping = {};
code.split('\n').map(p => p.replace(/#.*/, '')).map(s => s.trim()).forEach(
line => {
const parts = line.split(/\s*:\s*/);
const key = parts[0].trim();
const path = (parts[1] || parts[0]).trim();
if (key && path)
mapping[key] = path
}
);
return mapping;
}
async generate() {
return [
{
type: 'json',
value: JSON.stringify(this.ast, null, 1),
final: true,
},
{
type: 'js',
value: `module.exports = ${JSON.stringify(this.ast, null, 1)};`,
},
{
type: 'urls',
value: Object.keys(this.ast).map(path => `${path}: ${this.ast[path]}\n`).join(''),
final: true,
}
];
}
}
module.exports = UrlsAsset;