forked from Yomguithereal/talisman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-plugin.js
101 lines (89 loc) · 3.17 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
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
99
100
101
module.exports = function(context) {
const t = context.types;
return {
visitor: {
Program: {
exit(path) {
if (path.BABEL_PLUGIN_ADD_MODULE_EXPORTS) {
return;
}
let hasExportDefault = false;
const namedExports = new Set();
path.get('body').forEach(function(p) {
if (p.isExportDefaultDeclaration()) {
hasExportDefault = true;
return;
}
if (p.isExportNamedDeclaration()) {
if (p.node.specifiers.length === 1 && p.node.specifiers[0].exported.name === 'default') {
hasExportDefault = true;
}
else if (p.node.declaration && t.isFunctionDeclaration(p.node.declaration)) {
namedExports.add(p.node.declaration.id.name);
}
else {
p.node.specifiers.forEach(function(specifier) {
namedExports.add(specifier.exported.name);
});
}
return;
}
// NOTE: this is not ideal & kinda loops on Punkt
if (p.isVariableDeclaration()) {
p.container.forEach(function(node) {
if (
t.isExportNamedDeclaration(node) &&
node.declaration &&
node.declaration.declarations
) {
namedExports.add(node.declaration.declarations[0].id.name);
}
if (
t.isExportNamedDeclaration(node) &&
node.declaration &&
t.isFunctionDeclaration(node.declaration)
) {
namedExports.add(node.declaration.id.name)
}
});
}
});
if (hasExportDefault) {
path.pushContainer('body', [
t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(t.identifier('module'), t.identifier('exports')),
t.memberExpression(t.identifier('exports'), t.stringLiteral('default'), true)
))
]);
if (namedExports.size) {
namedExports.forEach(function(name) {
path.pushContainer('body', [
t.expressionStatement(t.assignmentExpression(
'=',
t.memberExpression(
t.memberExpression(t.identifier('exports'), t.stringLiteral('default'), true),
t.identifier(name)
),
t.memberExpression(
t.identifier('exports'),
t.identifier(name)
)
))
]);
});
path.BABEL_PLUGIN_NEED_TO_SHIFT_MODULE = true;
}
}
path.BABEL_PLUGIN_ADD_MODULE_EXPORTS = true;
}
}
},
post(state) {
if (!state.path.BABEL_PLUGIN_NEED_TO_SHIFT_MODULE)
return;
const program = state.path.get('body')[0].node;
program.expression.arguments[2].properties[0].value = t.booleanLiteral(false);
}
};
};