-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbabel.config.js
94 lines (93 loc) · 6 KB
/
babel.config.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
/* eslint-env node */
module.exports = function(api) {
api.cache(true);
return {
"presets": [
"@babel/preset-env",
["@babel/preset-flow", {
"allowDeclareFields": true
}]
],
"env": {
"test": {
"plugins": ["istanbul"]
},
"production": {
"plugins": [
"@babel/plugin-transform-modules-umd",
// Custom transformer for making VirtualClock work nicely as a UMD module
({template}) => ({
post(fileMap) {
fileMap.path.traverse({
AssignmentExpression(path) {
const expressionTarget = path.get('left');
const expressionValue = path.get('right');
// Match assignment of default to exports
if (
path.parentPath.isExpressionStatement() &&
path.parentPath.parentPath.isBlockStatement() &&
path.parentPath.parentPath.parentPath.isFunctionExpression() &&
expressionTarget.matchesPattern('_exports.default') &&
!(
expressionValue.isUnaryExpression({ operator: "void" }) &&
expressionValue.get('argument').isNumericLiteral({ value: 0 })
)
) {
path.insertAfter(template('return ' + expressionTarget)());
path.insertAfter(template(expressionTarget + '.default = ' + expressionTarget)());
}
// Match assignment of exports to global
if (
path.parentPath.isExpressionStatement() &&
path.parentPath.parentPath.isBlockStatement() &&
path.parentPath.parentPath.parentPath.isIfStatement() &&
expressionTarget.matchesPattern('global', true) &&
expressionValue.matchesPattern('mod.exports') &&
path.scope.hasBinding('factory')
) {
path.parentPath.parentPath.replaceWith(template(`
{
// Assign to both titlecased and camelcased version, for backwards compatibility
${expressionTarget} = ${expressionTarget.toString().replace(/\.([a-z])/, match => match.toUpperCase())} = factory({});
}
`)());
}
},
IfStatement(path) {
// Match UMD global function
if (
path.parentPath.isIfStatement() &&
path.get('test').isBinaryExpression({ operator: "!==" }) &&
path.get('test').get('left').isUnaryExpression({ operator: "typeof" }) &&
path.get('test').get('left').get('argument').isIdentifier({ name: 'exports' }) &&
path.get('test').get('right').isStringLiteral({ value: "undefined" }) &&
path.get('consequent').isBlockStatement() &&
path.get('consequent').get('body').length === 1 &&
!(
path.parentPath.get('test').isLogicalExpression({ operator: "&&" }) &&
path.parentPath.get('test').get('left').isBinaryExpression({ operator: "!==" }) &&
path.parentPath.get('test').get('left').get('left').isUnaryExpression({ operator: "typeof" }) &&
path.parentPath.get('test').get('left').get('left').get('argument').isIdentifier({ name: 'module' }) &&
path.parentPath.get('test').get('left').get('right').isStringLiteral({ value: "undefined" }) &&
path.parentPath.get('test').get('right').isBinaryExpression({ operator: "!==" }) &&
path.parentPath.get('test').get('right').get('left').isUnaryExpression({ operator: "typeof" }) &&
path.parentPath.get('test').get('right').get('left').get('argument').matchesPattern('module.exports') &&
path.parentPath.get('test').get('right').get('right').isStringLiteral({ value: "undefined" })
) &&
path.scope.hasBinding('factory')
) {
path.replaceWith(template(`
if (typeof module !== "undefined" && typeof module.exports !== "undefined") {
module.exports = factory(module.exports);
} else ${path}
`)());
}
}
});
}
})
]
}
}
};
};