index.js
3.58 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
exports.__esModule = true;
var _create = require("babel-runtime/core-js/object/create");
var _create2 = _interopRequireDefault(_create);
exports.default = function (_ref) {
var t = _ref.types;
function isValidRequireCall(path) {
if (!path.isCallExpression()) return false;
if (!path.get("callee").isIdentifier({ name: "require" })) return false;
if (path.scope.getBinding("require")) return false;
var args = path.get("arguments");
if (args.length !== 1) return false;
var arg = args[0];
if (!arg.isStringLiteral()) return false;
return true;
}
var amdVisitor = {
ReferencedIdentifier: function ReferencedIdentifier(_ref2) {
var node = _ref2.node,
scope = _ref2.scope;
if (node.name === "exports" && !scope.getBinding("exports")) {
this.hasExports = true;
}
if (node.name === "module" && !scope.getBinding("module")) {
this.hasModule = true;
}
},
CallExpression: function CallExpression(path) {
if (!isValidRequireCall(path)) return;
this.bareSources.push(path.node.arguments[0]);
path.remove();
},
VariableDeclarator: function VariableDeclarator(path) {
var id = path.get("id");
if (!id.isIdentifier()) return;
var init = path.get("init");
if (!isValidRequireCall(init)) return;
var source = init.node.arguments[0];
this.sourceNames[source.value] = true;
this.sources.push([id.node, source]);
path.remove();
}
};
return {
inherits: require("babel-plugin-transform-es2015-modules-commonjs"),
pre: function pre() {
this.sources = [];
this.sourceNames = (0, _create2.default)(null);
this.bareSources = [];
this.hasExports = false;
this.hasModule = false;
},
visitor: {
Program: {
exit: function exit(path) {
var _this = this;
if (this.ran) return;
this.ran = true;
path.traverse(amdVisitor, this);
var params = this.sources.map(function (source) {
return source[0];
});
var sources = this.sources.map(function (source) {
return source[1];
});
sources = sources.concat(this.bareSources.filter(function (str) {
return !_this.sourceNames[str.value];
}));
var moduleName = this.getModuleName();
if (moduleName) moduleName = t.stringLiteral(moduleName);
if (this.hasExports) {
sources.unshift(t.stringLiteral("exports"));
params.unshift(t.identifier("exports"));
}
if (this.hasModule) {
sources.unshift(t.stringLiteral("module"));
params.unshift(t.identifier("module"));
}
var node = path.node;
var factory = buildFactory({
PARAMS: params,
BODY: node.body
});
factory.expression.body.directives = node.directives;
node.directives = [];
node.body = [buildDefine({
MODULE_NAME: moduleName,
SOURCES: sources,
FACTORY: factory
})];
}
}
}
};
};
var _babelTemplate = require("babel-template");
var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var buildDefine = (0, _babelTemplate2.default)("\n define(MODULE_NAME, [SOURCES], FACTORY);\n");
var buildFactory = (0, _babelTemplate2.default)("\n (function (PARAMS) {\n BODY;\n })\n");
module.exports = exports["default"];