|
64 | 64 | import java.io.Serializable;
|
65 | 65 | import java.util.AbstractSet;
|
66 | 66 | import java.util.ArrayList;
|
67 |
| -import java.util.Collection; |
68 | 67 | import java.util.Collections;
|
69 | 68 | import java.util.HashMap;
|
70 | 69 | import java.util.HashSet;
|
@@ -1748,7 +1747,19 @@ Node parseInputs() {
|
1748 | 1747 | options.moduleResolutionMode,
|
1749 | 1748 | processJsonInputs(inputs));
|
1750 | 1749 | }
|
| 1750 | + } else { |
| 1751 | + // Use an empty module loader if we're not actually dealing with modules. |
| 1752 | + this.moduleLoader = ModuleLoader.EMPTY; |
| 1753 | + } |
1751 | 1754 |
|
| 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) { |
1752 | 1763 | if (options.getLanguageIn().toFeatureSet().has(Feature.MODULES)) {
|
1753 | 1764 | parsePotentialModules(inputs);
|
1754 | 1765 | }
|
@@ -1781,12 +1792,12 @@ Node parseInputs() {
|
1781 | 1792 | }
|
1782 | 1793 | }
|
1783 | 1794 |
|
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); |
1786 | 1800 | }
|
1787 |
| - } else { |
1788 |
| - // Use an empty module loader if we're not actually dealing with modules. |
1789 |
| - this.moduleLoader = ModuleLoader.EMPTY; |
1790 | 1801 | }
|
1791 | 1802 |
|
1792 | 1803 | orderInputs();
|
@@ -1894,6 +1905,142 @@ void orderInputs() {
|
1894 | 1905 | }
|
1895 | 1906 | }
|
1896 | 1907 |
|
| 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 | + |
1897 | 2044 | /**
|
1898 | 2045 | * Hoists inputs with the @externs annotation into the externs list.
|
1899 | 2046 | */
|
@@ -2003,18 +2150,6 @@ Map<String, String> processJsonInputs(List<CompilerInput> inputsToProcess) {
|
2003 | 2150 | return rewriteJson.getPackageJsonMainEntries();
|
2004 | 2151 | }
|
2005 | 2152 |
|
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 |
| - } |
2018 | 2153 |
|
2019 | 2154 | private List<CompilerInput> parsePotentialModules(List<CompilerInput> inputsToProcess) {
|
2020 | 2155 | List<CompilerInput> filteredInputs = new ArrayList<>();
|
@@ -2053,7 +2188,7 @@ void processAMDAndCommonJSModules() {
|
2053 | 2188 | new TransformAMDToCJSModule(this).process(null, root);
|
2054 | 2189 | }
|
2055 | 2190 | if (options.processCommonJSModules) {
|
2056 |
| - ProcessCommonJSModules cjs = new ProcessCommonJSModules(this, true); |
| 2191 | + ProcessCommonJSModules cjs = new ProcessCommonJSModules(this); |
2057 | 2192 | cjs.process(null, root);
|
2058 | 2193 | }
|
2059 | 2194 | }
|
|
0 commit comments