-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (97 loc) · 3.82 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
const generate = require('@babel/generator').default;
const globToRegExp = require('glob-to-regexp');
module.exports = function(source) {
const ast = parser.parse(source, {
sourceType: 'module',
});
let modifyFlag = false;
traverse(ast, {
MetaProperty(path) {
if (path.node.meta.name === 'import') {
modifyFlag = true;
let editPath = path.parentPath;
if (editPath.type === 'MemberExpression') {
const porpertyName = editPath.node.property.name;
if (porpertyName === 'globEager') {
editPath = editPath.parentPath;
// ---
const parentNode = editPath.parentPath.node;
if (parentNode.type === 'VariableDeclarator') {
const globSplit = editPath.node.arguments[0].value.split('/');
const fileNameRegrexString = globToRegExp(globSplit.pop()).source.replace('^', '');
let useSubdirectories = globSplit[globSplit.length - 1] === '*';
if (useSubdirectories) globSplit.pop();
const identifierName = parentNode.id.name;
parentNode.init = t.objectExpression([]);
editPath.parentPath.parentPath.insertAfter(
t.expressionStatement(
t.callExpression(
t.arrowFunctionExpression(
[t.identifier('r')],
t.callExpression(
t.memberExpression(
t.callExpression(t.memberExpression(t.identifier('r'), t.identifier('keys')), []),
t.identifier('forEach')
),
[
t.arrowFunctionExpression(
[t.identifier('key')],
t.assignmentExpression(
'=',
t.memberExpression(t.identifier(identifierName), t.identifier('key'), true),
t.callExpression(t.identifier('r'), [t.identifier('key')])
)
)
]
)
),
[
t.callExpression(
t.memberExpression(t.identifier('require'), t.identifier('context')),
[
t.stringLiteral(globSplit.join('/')),
t.booleanLiteral(useSubdirectories),
t.regExpLiteral(fileNameRegrexString)
]
)
]
)
)
);
return;
}
} else
if (porpertyName === 'env' && editPath.parentPath.node.property.name === 'DEV') {
editPath.parentPath.replaceWith(
t.binaryExpression(
'===',
t.memberExpression(
t.memberExpression(t.identifier('process'), t.identifier('env')),
t.identifier('NODE_ENV')
),
t.stringLiteral('development')
)
);
return;
}
} else
if (editPath.type === 'UnaryExpression' && editPath.node.operator === '!') {
editPath = path;
}
editPath.replaceWith(t.booleanLiteral(false));
}
}
});
// if (modifyFlag) {
// const generated = generate(ast, {}, source);
// console.log('-------------');
// console.log(__filename + ':start');
// console.log(generated.code);
// console.log(__filename + ':end');
// console.log('-------------');
// }
return modifyFlag ? generate(ast, {}, source).code : source;
};