Skip to content

Commit

Permalink
Clean up the code to find modules from entry points.
Browse files Browse the repository at this point in the history
The comments mistakenly suggest that entry-point based pruning happens here. It actually happens in JSModuleGraph#manageDependencies. The only thing happening here is setting the ModuleType on reachable inputs.

We also don't need to accumulate and return an ordered input list since it's not used anywhere.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218591483
  • Loading branch information
tjgq authored and EatingW committed Oct 30, 2018
1 parent c25901c commit fe6a73b
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -1726,7 +1726,7 @@ Node parseInputs() {
}

if (options.getDependencyOptions().needsManagement()) {
findDependenciesFromEntryPoints(
findModulesFromEntryPoints(
options.getLanguageIn().toFeatureSet().has(Feature.MODULES),
options.processCommonJSModules);
} else if (options.needsTranspilationFrom(FeatureSet.ES6_MODULES)
Expand Down Expand Up @@ -1869,16 +1869,17 @@ void orderInputs() {
}

/**
* Find dependencies by recursively traversing each dependency of an input starting with the entry
* points. Causes a full parse of each file, but since the file is reachable by walking the graph,
* this would be required in later compilation passes regardless.
* Find modules by recursively traversing dependencies starting with the entry points.
*
* <p>Inputs which are not reachable during graph traversal will be dropped.
* <p>Causes a regex parse of every file, and a full parse of every file reachable from the entry
* points (which would be required by later compilation passes regardless).
*
* <p>If the dependency mode is set to LOOSE, inputs for which the deps package did not find a
* provide statement or detect as a module will be treated as entry points.
* <p>If the dependency mode is set to LOOSE, inputs which the regex parse does not identify as ES
* modules and which do not contain any provide statements are considered to be additional entry
* points.
*/
void findDependenciesFromEntryPoints(boolean supportEs6Modules, boolean supportCommonJSModules) {
private void findModulesFromEntryPoints(
boolean supportEs6Modules, boolean supportCommonJSModules) {
hoistExterns();
List<CompilerInput> entryPoints = new ArrayList<>();
Map<String, CompilerInput> inputsByProvide = new HashMap<>();
Expand Down Expand Up @@ -1907,7 +1908,7 @@ void findDependenciesFromEntryPoints(boolean supportEs6Modules, boolean supportC

Set<CompilerInput> workingInputSet = Sets.newHashSet(moduleGraph.getAllInputs());
for (CompilerInput entryPoint : entryPoints) {
depthFirstDependenciesFromInput(
findModulesFromInput(
entryPoint,
/* wasImportedByModule = */ false,
workingInputSet,
Expand All @@ -1918,25 +1919,23 @@ void findDependenciesFromEntryPoints(boolean supportEs6Modules, boolean supportC
}
}

/** For a given input, order it's dependencies in a depth first traversal */
private List<CompilerInput> depthFirstDependenciesFromInput(
/** Traverse an input's dependencies to find additional modules. */
private void findModulesFromInput(
CompilerInput input,
boolean wasImportedByModule,
Set<CompilerInput> inputs,
Map<String, CompilerInput> inputsByIdentifier,
Map<String, CompilerInput> inputsByProvide,
boolean supportEs6Modules,
boolean supportCommonJSModules) {
List<CompilerInput> orderedInputs = new ArrayList<>();
if (!inputs.remove(input)) {
// It's possible for a module to be included as both a script
// and a module in the same compilation. In these cases, it should
// be forced to be a module.
if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) {
input.setJsModuleType(CompilerInput.ModuleType.IMPORTED_SCRIPT);
}

return orderedInputs;
return;
}

FindModuleDependencies findDeps =
Expand Down Expand Up @@ -1965,19 +1964,16 @@ private List<CompilerInput> depthFirstDependenciesFromInput(
}

if (requiredInput != null) {
orderedInputs.addAll(
depthFirstDependenciesFromInput(
requiredInput,
requiredByModuleImport,
inputs,
inputsByIdentifier,
inputsByProvide,
supportEs6Modules,
supportCommonJSModules));
}
}
orderedInputs.add(input);
return orderedInputs;
findModulesFromInput(
requiredInput,
requiredByModuleImport,
inputs,
inputsByIdentifier,
inputsByProvide,
supportEs6Modules,
supportCommonJSModules);
}
}
}

/**
Expand Down

0 comments on commit fe6a73b

Please sign in to comment.