-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
73 lines (63 loc) · 2.62 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { importDeclaration, stringLiteral, isStringLiteral, callExpression } from '@babel/types';
import { NODE_BUILTIN_MODULES } from './constants.js';
function err(msg) {
throw new Error('Modules: ' + msg);
}
function alreadyTransformed(modules, moduleName) {
const modulesList = Object.keys(modules).map((m) => transformModule(modules, m));
return modulesList.includes(moduleName);
}
function transformModule(modules, moduleName) {
if (modules[moduleName] == '*') {
return moduleName;
} else if (modules[moduleName]) {
// otherwise append the version
return `${moduleName}-${modules[moduleName]}`;
} else {
err(`${moduleName} is not installed.`);
}
}
function getModuleNameAndPath(value) {
if (!value) {
err('Empty import or require statement.');
}
const splitValue = value.split('/');
if (value.charAt(0) == '@') {
return {
moduleName: splitValue[0] + '/' + splitValue[1],
modulePath: splitValue.slice(2)
}
} else {
return {
moduleName: splitValue[0],
modulePath: splitValue.slice(1)
}
}
}
export default function () {
return {
visitor: {
ImportDeclaration: function (path, state) {
const specifiers = path.node.specifiers;
const { moduleName, modulePath } = getModuleNameAndPath(path.node.source.value);
const modules = Object.assign(state.opts.modules || {}, NODE_BUILTIN_MODULES); // passed in via plugin options -- an object of modules to versions, where '*' represents 'latest'
const newModuleName = alreadyTransformed(modules, moduleName) ? moduleName : transformModule(modules, moduleName);
const newImport = importDeclaration(specifiers, stringLiteral([newModuleName, ...modulePath].join('/')));
path.replaceWith(newImport);
path.skip();
},
CallExpression: function (path, state) {
const callee = path.node.callee;
const args = path.node.arguments || [];
if (callee.name == 'require' && args.length == 1 && isStringLiteral(args[0])) {
const modules = Object.assign(state.opts.modules || {}, NODE_BUILTIN_MODULES); // passed in via plugin options -- an object of modules to versions, where '*' represents 'latest'
const { moduleName, modulePath } = getModuleNameAndPath(args[0].value);
const newModuleName = alreadyTransformed(modules, moduleName) ? moduleName : transformModule(modules, moduleName);
const newCallExpression = callExpression(callee, [stringLiteral([newModuleName, ...modulePath].join('/'))]);
path.replaceWith(newCallExpression);
path.skip();
}
}
}
}
}