-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrewriteModulePath.js
More file actions
32 lines (30 loc) · 923 Bytes
/
rewriteModulePath.js
File metadata and controls
32 lines (30 loc) · 923 Bytes
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
// this script refer to self.__package
const url = require("url");
const path = require("path");
export default function rewriteModulePath({ types }) {
return {
pre(file) {
this.types = types;
},
visitor: {
ImportDeclaration(nodePath) {
const importTarget = nodePath.node.source.value;
const isRelative = importTarget[0] === ".";
if (isRelative) {
nodePath.node.source.value = importTarget;
} else if (importTarget.includes("https://")) {
return;
} else {
const pkg = this.opts.package;
const version =
typeof self === "object" &&
pkg &&
pkg.dependencies &&
pkg.dependencies[importTarget];
const target = importTarget + (!!version ? "@" + version : "");
nodePath.node.source.value = `https://dev.jspm.io/${target}`;
}
}
}
};
}