-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (39 loc) · 1.34 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
var moduleExportsIdentifier = '__MIX_IMPORT_MODULE_EXPORTS__';
module.exports = function (babel) {
var { types } = babel;
var hasImportDeclaration, moduleExportsPaths;
return {
visitor: {
Program: {
enter() {
hasImportDeclaration = false;
moduleExportsPaths = [];
},
exit(path) {
if (hasImportDeclaration && moduleExportsPaths.length) {
path.node.body.unshift(types.variableDeclaration('var', [types.variableDeclarator(types.Identifier(moduleExportsIdentifier))]));
path.node.body.push(types.exportDefaultDeclaration(types.Identifier(moduleExportsIdentifier)));
moduleExportsPaths.forEach(function(p) {
p.replaceWith(types.Identifier(moduleExportsIdentifier));
});
}
}
},
AssignmentExpression(path) {
var left = path.node.left;
var leftPath = path.get('left');
if (
moduleExportsPaths.indexOf(leftPath) < 0 &&
left.type === 'MemberExpression' &&
left.object.name === 'module' &&
(left.property.name || left.property.value) === 'exports' // module.exports || module['exports']
) {
moduleExportsPaths.push(leftPath);
}
},
ImportDeclaration() {
hasImportDeclaration = true;
}
}
};
};