-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
45 lines (42 loc) · 1.43 KB
/
index.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 readPkg = require('read-pkg');
const { walk } = require('estree-walker');
const MagicString = require('magic-string');
const padStr = str => `'${str}'`;
module.exports = ({ transform = (name, version) => `https://unpkg.com/${name}@${version}?type=module` } = {}) => {
const pkg = readPkg.sync();
const cache = {};
return {
name: 'unpkg',
options(opts) {
let deps = (pkg && pkg.dependencies) || {};
Object.keys(deps).forEach(dep => {
const manifest = readPkg.sync(require.resolve(`${dep}/package.json`));
if (manifest.module) cache[manifest.name] = transform(manifest.name, manifest.version);
});
let external = Object.values(cache);
if (Array.isArray(opts.external)) {
external = Array.from(new Set(opts.external.concat(external)));
}
return Object.assign({}, opts, { external });
},
transform(code, id) {
const ast = this.parse(code);
const magicString = new MagicString(code);
walk(ast, {
enter(node, parent) {
if (node.type === 'Literal' && parent.type === 'ImportDeclaration') {
if (cache[node.value])
magicString.overwrite(node.start, node.end, padStr(cache[node.value]), {
storeName: false
});
return node;
}
}
});
return {
code: magicString.toString(),
map: magicString.generateMap()
};
}
};
};