-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsURLAsset.js
45 lines (40 loc) · 1.38 KB
/
AsURLAsset.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
const path = require('path');
const Asset = require('parcel-bundler/src/Asset');
const urlJoin = require("parcel-bundler/src/utils/urlJoin");
class AsURLAsset extends Asset {
constructor(filename, options) {
super(filename, options);
this.type = "as-url";
const { dir, name } = path.parse(filename);
this.srcName = name;
this.srcDir = path.relative(options.rootDir, dir);
this.dst = null;
};
// The default `getDependencies` checks to see if this asset has
// any contents before collecting dependencies - but, as it's
// entirely virtual, we fail that test.
async getDependencies() {
await this.collectDependencies();
}
collectDependencies() {
// The .as-url asset is in the same directory as the target
// asset. So, just import it as a local dependency. The return
// value of addURLDependency is a hash that gets
// post-processed into a relative-to-root path, so anchor it
// to the public URL.
this.dst = urlJoin(
this.options.publicURL,
this.addURLDependency(path.join(".", this.srcName)),
);
};
generate() {
const code = `export default ${JSON.stringify(this.dst)};`;
return [
{
type: "js",
value: code,
}
];
};
};
module.exports = AsURLAsset;