-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (49 loc) · 1.61 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
module.exports = function ({ types: t }) {
return {
visitor: {
ExportNamedDeclaration: {
enter({ node }) {
if (node.declaration) {
let declarations;
if (t.isVariableDeclaration(node.declaration)) {
declarations = node.declaration.declarations.filter((d) => t.isVariableDeclarator(d));
} else if (t.isFunctionDeclaration(node.declaration)) {
declarations = [node.declaration];
}
if (declarations) {
// export const X = ...;
declarations.forEach((decl) => {
if (t.isIdentifier(decl.id)) {
const { name } = decl.id;
this.namedExports.push(name);
}
});
}
} else if (node.specifiers) {
node.specifiers.forEach((spec) => {
this.namedExports.push(spec.exported.name);
});
}
},
},
Program: {
enter({ state }) {
this.namedExports = [];
},
exit(path) {
if (this.namedExports.length > 0 && !this.namedExports.includes('__namedExportsOrder')) {
const namedExportsOrder = t.exportNamedDeclaration(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('__namedExportsOrder'),
t.arrayExpression(this.namedExports.map((name) => t.stringLiteral(name)))
),
])
);
path.pushContainer('body', namedExportsOrder);
}
},
},
},
};
};