-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathinline-requires.js
156 lines (136 loc) · 4.76 KB
/
inline-requires.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* This transform inlines top-level require(...) aliases with to enable lazy
* loading of dependencies.
*
* Continuing with the example above, this replaces all references to `Foo` in
* the module to `require('ModuleFoo')`.
*/
module.exports = function fbjsInlineRequiresTransform(babel) {
var t = babel.types;
function buildRequireCall(name, inlineRequiredDependencyMap) {
var call = t.callExpression(
t.identifier('require'),
[t.stringLiteral(inlineRequiredDependencyMap.get(name))]
);
call.new = true;
return call;
}
function inlineRequire(path, inlineRequiredDependencyMap, identifierToPathsMap) {
var node = path.node;
if (path.isReferenced()) {
path.replaceWith(buildRequireCall(node.name, inlineRequiredDependencyMap));
var paths = identifierToPathsMap.get(node.name);
if (paths) {
paths.delete(path);
}
}
}
return {
visitor: {
Program(_, state) {
/**
* Map of require(...) aliases to module names.
*
* `Foo` is an alias for `require('ModuleFoo')` in the following example:
* var Foo = require('ModuleFoo');
*/
state.inlineRequiredDependencyMap = new Map();
/**
* Map of variable names that have not yet been inlined.
* We track them in case we later remove their require()s,
* In which case we have to come back and update them.
*/
state.identifierToPathsMap = new Map();
},
/**
* Collect top-level require(...) aliases.
*/
CallExpression: function(path, state) {
var node = path.node;
if (isTopLevelRequireAlias(path)) {
var varName = path.parent.id.name;
var moduleName = node.arguments[0].value;
var inlineRequiredDependencyMap = state.inlineRequiredDependencyMap;
var identifierToPathsMap = state.identifierToPathsMap;
inlineRequiredDependencyMap.set(varName, moduleName);
// If we removed require() statements for variables we've already seen,
// We need to do a second pass on this program to replace them with require().
var maybePaths = identifierToPathsMap.get(varName);
if (maybePaths) {
maybePaths.forEach(path =>
inlineRequire(path, inlineRequiredDependencyMap, identifierToPathsMap));
maybePaths.delete(varName);
}
// Remove the declaration.
path.parentPath.parentPath.remove();
// And the associated binding in the scope.
path.scope.removeBinding(varName);
}
},
/**
* Inline require(...) aliases.
*/
Identifier: function(path, state) {
var node = path.node;
var parent = path.parent;
var scope = path.scope;
var identifierToPathsMap = state.identifierToPathsMap;
if (!shouldInlineRequire(node, scope, state.inlineRequiredDependencyMap)) {
// Monitor this name in case we later remove its require().
// This won't happen often but if it does we need to come back and update here.
var paths = identifierToPathsMap.get(node.name);
if (paths) {
paths.add(path);
} else {
identifierToPathsMap.set(node.name, new Set([path]));
}
return;
}
if (
parent.type === 'AssignmentExpression' &&
path.isBindingIdentifier() &&
!scope.bindingIdentifierEquals(node.name, node)
) {
throw new Error(
'Cannot assign to a require(...) alias, ' + node.name +
'. Line: ' + node.loc.start.line + '.'
);
}
inlineRequire(path, state.inlineRequiredDependencyMap, identifierToPathsMap);
},
},
};
};
function isTopLevelRequireAlias(path) {
return (
isRequireCall(path.node) &&
path.parent.type === 'VariableDeclarator' &&
path.parent.id.type === 'Identifier' &&
path.parentPath.parent.type === 'VariableDeclaration' &&
path.parentPath.parent.declarations.length === 1 &&
path.parentPath.parentPath.parent.type === 'Program'
);
}
function shouldInlineRequire(node, scope, inlineRequiredDependencyMap) {
return (
inlineRequiredDependencyMap.has(node.name) &&
!scope.hasBinding(node.name, true /* noGlobals */)
);
}
function isRequireCall(node) {
return (
!node.new &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
node['arguments'].length === 1 &&
node['arguments'][0].type === 'StringLiteral'
);
}