Skip to content

Commit 11684cb

Browse files
ChadKillingsworthlauraharker
authored andcommitted
When --dependency_mode is set to STRICT or LOOSE, order dependencies in a deterministic depth-first order from entry points.
Avoids a full parse on every input when CommonJS module processing is enabled. ES6 and CommonJS modules no longer generate synthetic goog.require and goog.provide calls. ES6 Module Transpilation no longer depends on Closure-Library primitives. Roll forward of [] now that [] has been fixed. Closes #2641 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=172648115
1 parent a2e6e20 commit 11684cb

12 files changed

+858
-453
lines changed

src/com/google/javascript/jscomp/Compiler.java

+154-19
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import java.io.Serializable;
6565
import java.util.AbstractSet;
6666
import java.util.ArrayList;
67-
import java.util.Collection;
6867
import java.util.Collections;
6968
import java.util.HashMap;
7069
import java.util.HashSet;
@@ -1748,7 +1747,19 @@ Node parseInputs() {
17481747
options.moduleResolutionMode,
17491748
processJsonInputs(inputs));
17501749
}
1750+
} else {
1751+
// Use an empty module loader if we're not actually dealing with modules.
1752+
this.moduleLoader = ModuleLoader.EMPTY;
1753+
}
17511754

1755+
if (options.getDependencyOptions().needsManagement()) {
1756+
findDependenciesFromEntryPoints(
1757+
options.getLanguageIn().toFeatureSet().has(Feature.MODULES),
1758+
options.processCommonJSModules,
1759+
options.transformAMDToCJSModules);
1760+
} else if (options.needsTranspilationOf(Feature.MODULES)
1761+
|| options.transformAMDToCJSModules
1762+
|| options.processCommonJSModules) {
17521763
if (options.getLanguageIn().toFeatureSet().has(Feature.MODULES)) {
17531764
parsePotentialModules(inputs);
17541765
}
@@ -1781,12 +1792,12 @@ Node parseInputs() {
17811792
}
17821793
}
17831794

1784-
if (!inputsToRewrite.isEmpty()) {
1785-
forceToEs6Modules(inputsToRewrite.values());
1795+
for (CompilerInput input : inputsToRewrite.values()) {
1796+
forceInputToPathBasedModule(
1797+
input,
1798+
options.getLanguageIn().toFeatureSet().has(Feature.MODULES),
1799+
options.processCommonJSModules);
17861800
}
1787-
} else {
1788-
// Use an empty module loader if we're not actually dealing with modules.
1789-
this.moduleLoader = ModuleLoader.EMPTY;
17901801
}
17911802

17921803
orderInputs();
@@ -1894,6 +1905,142 @@ void orderInputs() {
18941905
}
18951906
}
18961907

1908+
/**
1909+
* Find dependencies by recursively traversing each dependency of an input starting with the entry
1910+
* points. Causes a full parse of each file, but since the file is reachable by walking the graph,
1911+
* this would be required in later compilation passes regardless.
1912+
*
1913+
* <p>Inputs which are not reachable during graph traversal will be dropped.
1914+
*
1915+
* <p>If the dependency mode is set to LOOSE, inputs for which the deps package did not find a
1916+
* provide statement or detect as a module will be treated as entry points.
1917+
*/
1918+
void findDependenciesFromEntryPoints(
1919+
boolean supportEs6Modules, boolean supportCommonJSModules, boolean supportAmdModules) {
1920+
hoistExterns();
1921+
List<CompilerInput> entryPoints = new ArrayList<>();
1922+
Map<String, CompilerInput> inputsByProvide = new HashMap<>();
1923+
Map<String, CompilerInput> inputsByIdentifier = new HashMap<>();
1924+
for (CompilerInput input : inputs) {
1925+
if (!options.getDependencyOptions().shouldDropMoochers() && input.getProvides().isEmpty()) {
1926+
entryPoints.add(input);
1927+
}
1928+
inputsByIdentifier.put(
1929+
ModuleIdentifier.forFile(input.getPath().toString()).toString(), input);
1930+
for (String provide : input.getProvides()) {
1931+
if (!provide.startsWith("module$")) {
1932+
inputsByProvide.put(provide, input);
1933+
}
1934+
}
1935+
}
1936+
for (ModuleIdentifier moduleIdentifier : options.getDependencyOptions().getEntryPoints()) {
1937+
CompilerInput input = inputsByIdentifier.get(moduleIdentifier.toString());
1938+
if (input != null) {
1939+
entryPoints.add(input);
1940+
}
1941+
}
1942+
1943+
Set<CompilerInput> workingInputSet = new HashSet<>(inputs);
1944+
List<CompilerInput> orderedInputs = new ArrayList<>();
1945+
for (CompilerInput entryPoint : entryPoints) {
1946+
orderedInputs.addAll(
1947+
depthFirstDependenciesFromInput(
1948+
entryPoint,
1949+
/* wasImportedByModule = */ false,
1950+
workingInputSet,
1951+
inputsByIdentifier,
1952+
inputsByProvide,
1953+
supportEs6Modules,
1954+
supportCommonJSModules,
1955+
supportAmdModules));
1956+
}
1957+
1958+
// TODO(ChadKillingsworth) Move this into the standard compilation passes
1959+
if (supportCommonJSModules) {
1960+
for (CompilerInput input : orderedInputs) {
1961+
new ProcessCommonJSModules(this)
1962+
.process(/* externs */ null, input.getAstRoot(this), /* forceModuleDetection */ false);
1963+
}
1964+
}
1965+
}
1966+
1967+
/** For a given input, order it's dependencies in a depth first traversal */
1968+
private List<CompilerInput> depthFirstDependenciesFromInput(
1969+
CompilerInput input,
1970+
boolean wasImportedByModule,
1971+
Set<CompilerInput> inputs,
1972+
Map<String, CompilerInput> inputsByIdentifier,
1973+
Map<String, CompilerInput> inputsByProvide,
1974+
boolean supportEs6Modules,
1975+
boolean supportCommonJSModules,
1976+
boolean supportAmdModules) {
1977+
List<CompilerInput> orderedInputs = new ArrayList<>();
1978+
if (!inputs.remove(input)) {
1979+
// It's possible for a module to be included as both a script
1980+
// and a module in the same compilation. In these cases, it should
1981+
// be forced to be a module.
1982+
if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) {
1983+
forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules);
1984+
}
1985+
1986+
return orderedInputs;
1987+
}
1988+
1989+
if (supportAmdModules) {
1990+
new TransformAMDToCJSModule(this).process(null, input.getAstRoot(this));
1991+
}
1992+
1993+
FindModuleDependencies findDeps =
1994+
new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules);
1995+
findDeps.process(input.getAstRoot(this));
1996+
1997+
// If this input was imported by another module, it is itself a module
1998+
// so we force it to be detected as such.
1999+
if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) {
2000+
forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules);
2001+
}
2002+
2003+
for (String requiredNamespace : input.getRequires()) {
2004+
CompilerInput requiredInput = null;
2005+
boolean requiredByModuleImport = false;
2006+
if (inputsByProvide.containsKey(requiredNamespace)) {
2007+
requiredInput = inputsByProvide.get(requiredNamespace);
2008+
} else if (inputsByIdentifier.containsKey(requiredNamespace)) {
2009+
requiredByModuleImport = true;
2010+
requiredInput = inputsByIdentifier.get(requiredNamespace);
2011+
}
2012+
2013+
if (requiredInput != null) {
2014+
orderedInputs.addAll(
2015+
depthFirstDependenciesFromInput(
2016+
requiredInput,
2017+
requiredByModuleImport,
2018+
inputs,
2019+
inputsByIdentifier,
2020+
inputsByProvide,
2021+
supportEs6Modules,
2022+
supportCommonJSModules,
2023+
supportAmdModules));
2024+
}
2025+
}
2026+
orderedInputs.add(input);
2027+
return orderedInputs;
2028+
}
2029+
2030+
private void forceInputToPathBasedModule(
2031+
CompilerInput input, boolean supportEs6Modules, boolean supportCommonJSModules) {
2032+
2033+
if (supportEs6Modules) {
2034+
FindModuleDependencies findDeps =
2035+
new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules);
2036+
findDeps.convertToEs6Module(input.getAstRoot(this));
2037+
input.setJsModuleType(CompilerInput.ModuleType.ES6);
2038+
} else if (supportCommonJSModules) {
2039+
new ProcessCommonJSModules(this).process(null, input.getAstRoot(this), true);
2040+
input.setJsModuleType(CompilerInput.ModuleType.COMMONJS);
2041+
}
2042+
}
2043+
18972044
/**
18982045
* Hoists inputs with the @externs annotation into the externs list.
18992046
*/
@@ -2003,18 +2150,6 @@ Map<String, String> processJsonInputs(List<CompilerInput> inputsToProcess) {
20032150
return rewriteJson.getPackageJsonMainEntries();
20042151
}
20052152

2006-
void forceToEs6Modules(Collection<CompilerInput> inputsToProcess) {
2007-
for (CompilerInput input : inputsToProcess) {
2008-
input.setCompiler(this);
2009-
input.addProvide(input.getPath().toModuleName());
2010-
Node root = input.getAstRoot(this);
2011-
if (root == null) {
2012-
continue;
2013-
}
2014-
Es6RewriteModules moduleRewriter = new Es6RewriteModules(this);
2015-
moduleRewriter.forceToEs6Module(root);
2016-
}
2017-
}
20182153

20192154
private List<CompilerInput> parsePotentialModules(List<CompilerInput> inputsToProcess) {
20202155
List<CompilerInput> filteredInputs = new ArrayList<>();
@@ -2053,7 +2188,7 @@ void processAMDAndCommonJSModules() {
20532188
new TransformAMDToCJSModule(this).process(null, root);
20542189
}
20552190
if (options.processCommonJSModules) {
2056-
ProcessCommonJSModules cjs = new ProcessCommonJSModules(this, true);
2191+
ProcessCommonJSModules cjs = new ProcessCommonJSModules(this);
20572192
cjs.process(null, root);
20582193
}
20592194
}

src/com/google/javascript/jscomp/CompilerInput.java

+38-7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class CompilerInput implements SourceAst, DependencyInfo {
6161
private DependencyInfo dependencyInfo;
6262
private final List<String> extraRequires = new ArrayList<>();
6363
private final List<String> extraProvides = new ArrayList<>();
64+
private final List<String> orderedRequires = new ArrayList<>();
65+
private boolean hasFullParseDependencyInfo = false;
66+
private ModuleType jsModuleType = ModuleType.NONE;
6467

6568
// An AbstractCompiler for doing parsing.
6669
// We do not want to persist this across serialized state.
@@ -160,6 +163,10 @@ public void setCompiler(AbstractCompiler compiler) {
160163
/** Gets a list of types depended on by this input. */
161164
@Override
162165
public Collection<String> getRequires() {
166+
if (hasFullParseDependencyInfo) {
167+
return orderedRequires;
168+
}
169+
163170
return getDependencyInfo().getRequires();
164171
}
165172

@@ -191,19 +198,36 @@ Collection<String> getKnownProvides() {
191198
extraProvides);
192199
}
193200

194-
// TODO(nicksantos): Remove addProvide/addRequire/removeRequire once
195-
// there is better support for discovering non-closure dependencies.
196-
197201
/**
198-
* Registers a type that this input defines.
202+
* Registers a type that this input defines. Includes both explicitly declared namespaces via
203+
* goog.provide and goog.module calls as well as implicit namespaces provided by module rewriting.
199204
*/
200205
public void addProvide(String provide) {
201206
extraProvides.add(provide);
202207
}
203208

204-
/**
205-
* Registers a type that this input depends on.
206-
*/
209+
/** Registers a type that this input depends on in the order seen in the file. */
210+
public boolean addOrderedRequire(String require) {
211+
if (!orderedRequires.contains(require)) {
212+
orderedRequires.add(require);
213+
return true;
214+
}
215+
return false;
216+
}
217+
218+
public void setHasFullParseDependencyInfo(boolean hasFullParseDependencyInfo) {
219+
this.hasFullParseDependencyInfo = hasFullParseDependencyInfo;
220+
}
221+
222+
public ModuleType getJsModuleType() {
223+
return jsModuleType;
224+
}
225+
226+
public void setJsModuleType(ModuleType moduleType) {
227+
jsModuleType = moduleType;
228+
}
229+
230+
/** Registers a type that this input depends on. */
207231
public void addRequire(String require) {
208232
extraRequires.add(require);
209233
}
@@ -493,4 +517,11 @@ public void reset() {
493517
this.module = null;
494518
this.ast.clearAst();
495519
}
520+
521+
enum ModuleType {
522+
NONE,
523+
GOOG_MODULE,
524+
ES6,
525+
COMMONJS
526+
}
496527
}

src/com/google/javascript/jscomp/DependencyOptions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import java.io.Serializable;
2222
import java.util.Collection;
23-
import java.util.HashSet;
23+
import java.util.LinkedHashSet;
2424
import java.util.Set;
2525

2626
/**
@@ -43,7 +43,7 @@ public final class DependencyOptions implements Serializable {
4343
private boolean sortDependencies = false;
4444
private boolean pruneDependencies = false;
4545
private boolean dropMoochers = false;
46-
private final Set<ModuleIdentifier> entryPoints = new HashSet<>();
46+
private final Set<ModuleIdentifier> entryPoints = new LinkedHashSet<>();
4747

4848
/**
4949
* Enables or disables dependency sorting mode.

0 commit comments

Comments
 (0)