forked from seia-soto/snowpack-plugin-import-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbabel-plugin.js
49 lines (40 loc) · 1.04 KB
/
babel-plugin.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
// Inspired by https://github.com/tbranyen/babel-plugin-resolve-imports-for-browser
// Modified by zhoukekestar @ 20200605
const { types } = require('@babel/core');
let pluginOptions = {}
const visitor = {
exit(path) {
const { specifiers } = path.node;
const source = path.get('source');
if (!source.node) {
return;
}
// If dep is a thrid lib, convert it to cdn.
if (source.node.value[0] !== '.' && source.node.value[0] !== '/') {
// Map to new resource
if (pluginOptions[source.node.value]) {
const newSource = types.stringLiteral(pluginOptions[source.node.value]);
source.replaceWith(newSource);
}
}
},
};
// Babel Plugin
module.exports = options => {
pluginOptions = options
return (api, options) => {
api.assertVersion(7);
return {
visitor: {
Program: {
exit(path) {
path.traverse({
ImportDeclaration: visitor,
// ExportDeclaration: visitor,
});
}
},
},
};
};
}