-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Es6RewriteModulesToCommonJsModules.java
455 lines (394 loc) · 16.5 KB
/
Es6RewriteModulesToCommonJsModules.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright 2018 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.jscomp.deps.ModuleLoader.ModulePath;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.Nullable;
/**
* Rewrites an ES6 module to a CommonJS-like module for the sake of per-file transpilation +
* bunlding (e.g. Closure Bundler). Output is not meant to be type checked.
*/
public class Es6RewriteModulesToCommonJsModules implements CompilerPass {
private static final String JSCOMP_DEFAULT_EXPORT = "$$default";
private static final String MODULE = "$$module";
private static final String EXPORTS = "$$exports";
private static final String REQUIRE = "$$require";
private final AbstractCompiler compiler;
private final String pragma;
public Es6RewriteModulesToCommonJsModules(AbstractCompiler compiler) {
this(compiler, "use strict");
}
@VisibleForTesting
Es6RewriteModulesToCommonJsModules(AbstractCompiler compiler, String pragma) {
this.compiler = compiler;
this.pragma = pragma;
}
@Override
public void process(Node externs, Node root) {
for (Node script : root.children()) {
if (Es6RewriteModules.isEs6ModuleRoot(script)) {
NodeTraversal.traverseEs6(compiler, script, new Rewriter(compiler, script));
}
}
}
private static class LocalQName {
final String qName;
/**
* Node to use for source information. All exported properties are transpiled to ES5 getters so
* debuggers will automatically step into these, even with source maps. When stepping in this
* node will be displayed in the source map. In general it should either be the export itself
* (e.g. export function or class) or the specific name being exported (export specs, const,
* etc).
*/
final Node nodeForSourceInfo;
LocalQName(String qName, Node nodeForSourceInfo) {
this.qName = qName;
this.nodeForSourceInfo = nodeForSourceInfo;
}
}
/**
* Rewrites a single ES6 module into a CommonJS like module designed to be loaded in the
* compiler's module runtime.
*/
private class Rewriter extends AbstractPostOrderCallback {
private Node requireInsertSpot;
private final Node script;
private final Map<String, LocalQName> exportedNameToLocalQName;
private final Set<Node> imports;
private final Set<String> importRequests;
private final AbstractCompiler compiler;
private final ModulePath modulePath;
Rewriter(AbstractCompiler compiler, Node script) {
this.compiler = compiler;
this.script = script;
requireInsertSpot = null;
// TreeMap because ES6 orders the export key using natural ordering.
exportedNameToLocalQName = new TreeMap<>();
importRequests = new LinkedHashSet<>();
imports = new HashSet<>();
modulePath = compiler.getInput(script.getInputId()).getPath();
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getToken()) {
case IMPORT:
visitImport(n);
break;
case EXPORT:
visitExport(t, n, parent);
break;
case SCRIPT:
visitScript(t, n);
break;
case NAME:
maybeRenameImportedValue(t, n);
break;
default:
break;
}
}
/**
* Given an import node gets the name of the var to use for the imported module.
*
* Example:
* import {v} from './foo.js'; use(v);
* Can become:
* const module$foo = require('./foo.js'); use(module$foo.v);
* This method would return "module$foo".
*/
private String getVarNameOfImport(Node importDecl) {
checkState(importDecl.isImport());
return getVarNameOfImport(importDecl.getLastChild().getString());
}
private String getVarNameOfImport(String importRequest) {
return modulePath.resolveModuleAsPath(importRequest).toModuleName();
}
/**
* @return qualified name to use to reference an imported value.
* <p>Examples:
* <ul>
* <li>If referencing an import spec like v in "import {v} from './foo.js'" then this
* would return "module$foo.v".
* <li>If referencing an import star like m in "import * as m from './foo.js'" then this
* would return "module$foo".
* <li>If referencing an import default like d in "import d from './foo.js'" then this
* would return "module$foo.default".
*
* Used to rename references to imported values within this module.
*/
private String getNameOfImportedValue(Node nameNode) {
Node importDecl = nameNode;
while (!importDecl.isImport()) {
importDecl = importDecl.getParent();
}
String moduleName = getVarNameOfImport(importDecl);
if (nameNode.getParent().isImportSpec()) {
return moduleName + "." + nameNode.getParent().getFirstChild().getString();
} else if (nameNode.isImportStar()) {
return moduleName;
} else {
checkState(nameNode.getParent().isImport());
return moduleName + ".default";
}
}
/**
* @param nameNode any variable name that is potentially from an import statement
* @return qualified name to use to reference an imported value if the given node is an imported
* name or null if the value is not imported or if it is in the import statement itself
*/
@Nullable
private String maybeGetNameOfImportedValue(Scope s, Node nameNode) {
checkState(nameNode.isName());
Var var = s.getVar(nameNode.getString());
if (var != null
// variables added implicitly to the scope, like arguments, have a null name node
&& var.getNameNode() != null
&& NodeUtil.isImportedName(var.getNameNode())
&& nameNode != var.getNameNode()) {
return getNameOfImportedValue(var.getNameNode());
}
return null;
}
/**
* Renames the given name node if it is an imported value.
*/
private void maybeRenameImportedValue(NodeTraversal t, Node n) {
checkState(n.isName());
Node parent = n.getParent();
if (parent.isExport()
|| parent.isExportSpec()
|| parent.isImport()
|| parent.isImportSpec()) {
return;
}
String qName = maybeGetNameOfImportedValue(t.getScope(), n);
if (qName != null) {
n.replaceWith(NodeUtil.newQName(compiler, qName));
t.reportCodeChange();
}
}
private void visitScript(NodeTraversal t, Node script) {
checkState(this.script == script);
Node moduleNode = script.getFirstChild();
checkState(moduleNode.isModuleBody());
moduleNode.detach();
script.addChildrenToFront(moduleNode.removeChildren());
// Order here is important. We want the end result to be:
// $jscomp.registerAndLoadModule(function($$require, $$exports, $$module) {
// // First to ensure circular deps can see exports of this module before we require them,
// // and also so that temporal deadzone is respected.
// //<export def>
// // Second so the module definition can reference imported modules, and so any require'd
// // modules are loaded.
// //<requires>
// // And finally last is the actual module definition.
// //<module def>
// }, /* <module path> */, [/* <deps> */]);
// As a result the calls below are in *inverse* order to what we want above so they can keep
// adding to the front of the script.
addRequireCalls();
addExportDef();
registerAndLoadModule(t);
}
/** Adds one call to require per imported module. */
private void addRequireCalls() {
if (!importRequests.isEmpty()) {
for (Node importDecl : imports) {
importDecl.detach();
}
Set<String> importedNames = new HashSet<>();
for (String request : importRequests) {
String varName = getVarNameOfImport(request);
if (importedNames.add(varName)) {
Node requireCall = IR.call(IR.name(REQUIRE), IR.string(request));
requireCall.putBooleanProp(Node.FREE_CALL, true);
Node var = IR.var(IR.name(varName), requireCall);
var.useSourceInfoIfMissingFromForTree(script);
script.addChildAfter(var, requireInsertSpot);
requireInsertSpot = var;
}
}
}
}
/**
* Wraps the entire current module definition in a $jscomp.registerAndLoadModule function.
*/
private void registerAndLoadModule(NodeTraversal t) {
Node block = IR.block();
block.addChildrenToFront(script.removeChildren());
block.addChildToFront(IR.exprResult(IR.string(pragma)));
Node moduleFunction =
IR.function(
IR.name(""),
IR.paramList(IR.name(REQUIRE), IR.name(EXPORTS), IR.name(MODULE)),
block);
Node shallowDeps = new Node(Token.ARRAYLIT);
for (String request : importRequests) {
shallowDeps.addChildToBack(IR.string(request));
}
Node exprResult =
IR.exprResult(
IR.call(
IR.getprop(IR.name("$jscomp"), IR.string("registerAndLoadModule")),
moduleFunction,
// Specifically use the input's name rather than modulePath.toString(). The former
// is the raw path and the latter is encoded (special characters are replaced).
// This is designed to run in a web browser and we want to preserve the URL given
// to us. But the encodings will replace : with - due to windows.
IR.string(t.getInput().getName()),
shallowDeps));
script.addChildToBack(exprResult.useSourceInfoIfMissingFromForTree(script));
compiler.reportChangeToChangeScope(script);
compiler.reportChangeToChangeScope(moduleFunction);
t.reportCodeChange();
}
/** Adds exports to the exports object using Object.defineProperties. */
private void addExportDef() {
if (!exportedNameToLocalQName.isEmpty()) {
Node definePropertiesLit = IR.objectlit();
for (Map.Entry<String, LocalQName> entry : exportedNameToLocalQName.entrySet()) {
addExport(definePropertiesLit, entry.getKey(), entry.getValue());
}
script.addChildToFront(
IR.exprResult(
IR.call(
NodeUtil.newQName(compiler, "Object.defineProperties"),
IR.name(EXPORTS),
definePropertiesLit))
.useSourceInfoIfMissingFromForTree(script));
}
}
/** Adds an ES5 getter to the given object literal to use an an export. */
private void addExport(Node definePropertiesLit, String exportedName, LocalQName localQName) {
Node exportedValue = NodeUtil.newQName(compiler, localQName.qName);
Node getterFunction =
IR.function(IR.name(""), IR.paramList(), IR.block(IR.returnNode(exportedValue)));
getterFunction.useSourceInfoFromForTree(localQName.nodeForSourceInfo);
Node objLit =
IR.objectlit(
IR.stringKey("enumerable", IR.trueNode()), IR.stringKey("get", getterFunction));
definePropertiesLit.addChildToBack(IR.stringKey(exportedName, objLit));
compiler.reportChangeToChangeScope(getterFunction);
}
private void visitImport(Node importDecl) {
importRequests.add(importDecl.getLastChild().getString());
imports.add(importDecl);
}
private void visitExportDefault(NodeTraversal t, Node export, Node parent) {
Node child = export.getFirstChild();
String name = null;
if (child.isFunction() || child.isClass()) {
name = NodeUtil.getName(child);
}
if (name != null) {
Node decl = child.detach();
parent.replaceChild(export, decl);
} else {
name = JSCOMP_DEFAULT_EXPORT;
// Default exports are constant in more ways than one. Not only can they not be
// overwritten but they also act like a const for temporal dead-zone purposes.
Node var = IR.constNode(IR.name(name), export.removeFirstChild());
parent.replaceChild(export, var.useSourceInfoIfMissingFromForTree(export));
}
exportedNameToLocalQName.put("default", new LocalQName(name, export));
t.reportCodeChange();
}
private void visitExportFrom(NodeTraversal t, Node export, Node parent) {
// export {x, y as z} from 'moduleIdentifier';
Node moduleIdentifier = export.getLastChild();
Node importNode = IR.importNode(IR.empty(), IR.empty(), moduleIdentifier.cloneNode());
importNode.useSourceInfoFrom(export);
parent.addChildBefore(importNode, export);
visit(t, importNode, parent);
String moduleName = getVarNameOfImport(moduleIdentifier.getString());
for (Node exportSpec : export.getFirstChild().children()) {
exportedNameToLocalQName.put(
exportSpec.getLastChild().getString(),
new LocalQName(moduleName + "." + exportSpec.getFirstChild().getString(), exportSpec));
}
parent.removeChild(export);
t.reportCodeChange();
}
private void visitExportSpecs(NodeTraversal t, Node export, Node parent) {
// export {Foo};
for (Node exportSpec : export.getFirstChild().children()) {
String localName = exportSpec.getFirstChild().getString();
Var var = t.getScope().getVar(localName);
if (var != null && NodeUtil.isImportedName(var.getNameNode())) {
localName = maybeGetNameOfImportedValue(t.getScope(), exportSpec.getFirstChild());
checkNotNull(localName);
}
exportedNameToLocalQName.put(
exportSpec.getLastChild().getString(), new LocalQName(localName, exportSpec));
}
parent.removeChild(export);
t.reportCodeChange();
}
private void visitExportNameDeclaration(Node declaration) {
// export var Foo;
// export let {a, b:[c,d]} = {};
List<Node> lhsNodes = NodeUtil.findLhsNodesInNode(declaration);
for (Node lhs : lhsNodes) {
checkState(lhs.isName());
String name = lhs.getString();
exportedNameToLocalQName.put(name, new LocalQName(name, lhs));
}
}
private void visitExportDeclaration(NodeTraversal t, Node export, Node parent) {
// export var Foo;
// export function Foo() {}
// etc.
Node declaration = export.getFirstChild();
if (NodeUtil.isNameDeclaration(declaration)) {
visitExportNameDeclaration(declaration);
} else {
checkState(declaration.isFunction() || declaration.isClass());
String name = declaration.getFirstChild().getString();
exportedNameToLocalQName.put(name, new LocalQName(name, export));
}
parent.replaceChild(export, declaration.detach());
t.reportCodeChange();
}
private void visitExport(NodeTraversal t, Node export, Node parent) {
if (export.getBooleanProp(Node.EXPORT_DEFAULT)) {
visitExportDefault(t, export, parent);
} else if (export.getBooleanProp(Node.EXPORT_ALL_FROM)) {
// TODO(johnplaisted)
compiler.report(JSError.make(export, Es6ToEs3Util.CANNOT_CONVERT_YET, "Wildcard export"));
} else if (export.hasTwoChildren()) {
visitExportFrom(t, export, parent);
} else {
if (export.getFirstChild().getToken() == Token.EXPORT_SPECS) {
visitExportSpecs(t, export, parent);
} else {
visitExportDeclaration(t, export, parent);
}
}
}
}
}