|
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; |
@@ -1749,7 +1748,19 @@ Node parseInputs() { |
1749 | 1748 | options.moduleResolutionMode, |
1750 | 1749 | processJsonInputs(inputs)); |
1751 | 1750 | } |
| 1751 | + } else { |
| 1752 | + // Use an empty module loader if we're not actually dealing with modules. |
| 1753 | + this.moduleLoader = ModuleLoader.EMPTY; |
| 1754 | + } |
1752 | 1755 |
|
| 1756 | + if (options.getDependencyOptions().needsManagement()) { |
| 1757 | + findDependenciesFromEntryPoints( |
| 1758 | + options.getLanguageIn().toFeatureSet().has(Feature.MODULES), |
| 1759 | + options.processCommonJSModules, |
| 1760 | + options.transformAMDToCJSModules); |
| 1761 | + } else if (options.needsTranspilationFrom(FeatureSet.ES6_MODULES) |
| 1762 | + || options.transformAMDToCJSModules |
| 1763 | + || options.processCommonJSModules) { |
1753 | 1764 | if (options.getLanguageIn().toFeatureSet().has(Feature.MODULES)) { |
1754 | 1765 | parsePotentialModules(inputs); |
1755 | 1766 | } |
@@ -1782,12 +1793,12 @@ Node parseInputs() { |
1782 | 1793 | } |
1783 | 1794 | } |
1784 | 1795 |
|
1785 | | - if (!inputsToRewrite.isEmpty()) { |
1786 | | - forceToEs6Modules(inputsToRewrite.values()); |
| 1796 | + for (CompilerInput input : inputsToRewrite.values()) { |
| 1797 | + forceInputToPathBasedModule( |
| 1798 | + input, |
| 1799 | + options.getLanguageIn().toFeatureSet().has(Feature.MODULES), |
| 1800 | + options.processCommonJSModules); |
1787 | 1801 | } |
1788 | | - } else { |
1789 | | - // Use an empty module loader if we're not actually dealing with modules. |
1790 | | - this.moduleLoader = ModuleLoader.EMPTY; |
1791 | 1802 | } |
1792 | 1803 |
|
1793 | 1804 | orderInputs(); |
@@ -1894,6 +1905,141 @@ 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 | + 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).process(null, input.getAstRoot(this), false); |
| 1962 | + } |
| 1963 | + } |
| 1964 | + } |
| 1965 | + |
| 1966 | + /** For a given input, order it's dependencies in a depth first traversal */ |
| 1967 | + List<CompilerInput> depthFirstDependenciesFromInput( |
| 1968 | + CompilerInput input, |
| 1969 | + boolean wasImportedByModule, |
| 1970 | + Set<CompilerInput> inputs, |
| 1971 | + Map<String, CompilerInput> inputsByIdentifier, |
| 1972 | + Map<String, CompilerInput> inputsByProvide, |
| 1973 | + boolean supportEs6Modules, |
| 1974 | + boolean supportCommonJSModules, |
| 1975 | + boolean supportAmdModules) { |
| 1976 | + List<CompilerInput> orderedInputs = new ArrayList<>(); |
| 1977 | + if (!inputs.remove(input)) { |
| 1978 | + // It's possible for a module to be included as both a script |
| 1979 | + // and a module in the same compilation. In these cases, it should |
| 1980 | + // be forced to be a module. |
| 1981 | + if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) { |
| 1982 | + forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules); |
| 1983 | + } |
| 1984 | + |
| 1985 | + return orderedInputs; |
| 1986 | + } |
| 1987 | + |
| 1988 | + if (supportAmdModules) { |
| 1989 | + new TransformAMDToCJSModule(this).process(null, input.getAstRoot(this)); |
| 1990 | + } |
| 1991 | + |
| 1992 | + FindModuleDependencies findDeps = |
| 1993 | + new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules); |
| 1994 | + findDeps.process(input.getAstRoot(this)); |
| 1995 | + |
| 1996 | + // If this input was imported by another module, it is itself a module |
| 1997 | + // so we force it to be detected as such. |
| 1998 | + if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) { |
| 1999 | + forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules); |
| 2000 | + } |
| 2001 | + |
| 2002 | + for (String requiredNamespace : input.getRequires()) { |
| 2003 | + CompilerInput requiredInput = null; |
| 2004 | + boolean requiredByModuleImport = false; |
| 2005 | + if (inputsByProvide.containsKey(requiredNamespace)) { |
| 2006 | + requiredInput = inputsByProvide.get(requiredNamespace); |
| 2007 | + } else if (inputsByIdentifier.containsKey(requiredNamespace)) { |
| 2008 | + requiredByModuleImport = true; |
| 2009 | + requiredInput = inputsByIdentifier.get(requiredNamespace); |
| 2010 | + } |
| 2011 | + |
| 2012 | + if (requiredInput != null) { |
| 2013 | + orderedInputs.addAll( |
| 2014 | + depthFirstDependenciesFromInput( |
| 2015 | + requiredInput, |
| 2016 | + requiredByModuleImport, |
| 2017 | + inputs, |
| 2018 | + inputsByIdentifier, |
| 2019 | + inputsByProvide, |
| 2020 | + supportEs6Modules, |
| 2021 | + supportCommonJSModules, |
| 2022 | + supportAmdModules)); |
| 2023 | + } |
| 2024 | + } |
| 2025 | + orderedInputs.add(input); |
| 2026 | + return orderedInputs; |
| 2027 | + } |
| 2028 | + |
| 2029 | + private void forceInputToPathBasedModule( |
| 2030 | + CompilerInput input, boolean supportEs6Modules, boolean supportCommonJSModules) { |
| 2031 | + |
| 2032 | + if (supportEs6Modules) { |
| 2033 | + FindModuleDependencies findDeps = |
| 2034 | + new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules); |
| 2035 | + findDeps.convertToEs6Module(input.getAstRoot(this)); |
| 2036 | + input.setJsModuleType(CompilerInput.ModuleType.ES6); |
| 2037 | + } else if (supportCommonJSModules) { |
| 2038 | + new ProcessCommonJSModules(this).process(null, input.getAstRoot(this), true); |
| 2039 | + input.setJsModuleType(CompilerInput.ModuleType.COMMONJS); |
| 2040 | + } |
| 2041 | + } |
| 2042 | + |
1897 | 2043 | /** |
1898 | 2044 | * Hoists inputs with the @externs annotation into the externs list. |
1899 | 2045 | */ |
@@ -2041,18 +2187,6 @@ Map<String, String> processJsonInputs(List<CompilerInput> inputsToProcess) { |
2041 | 2187 | return rewriteJson.getPackageJsonMainEntries(); |
2042 | 2188 | } |
2043 | 2189 |
|
2044 | | - void forceToEs6Modules(Collection<CompilerInput> inputsToProcess) { |
2045 | | - for (CompilerInput input : inputsToProcess) { |
2046 | | - input.setCompiler(this); |
2047 | | - input.addProvide(input.getPath().toModuleName()); |
2048 | | - Node root = input.getAstRoot(this); |
2049 | | - if (root == null) { |
2050 | | - continue; |
2051 | | - } |
2052 | | - Es6RewriteModules moduleRewriter = new Es6RewriteModules(this); |
2053 | | - moduleRewriter.forceToEs6Module(root); |
2054 | | - } |
2055 | | - } |
2056 | 2190 |
|
2057 | 2191 | private List<CompilerInput> parsePotentialModules(List<CompilerInput> inputsToProcess) { |
2058 | 2192 | List<CompilerInput> filteredInputs = new ArrayList<>(); |
@@ -2091,7 +2225,7 @@ void processAMDAndCommonJSModules() { |
2091 | 2225 | new TransformAMDToCJSModule(this).process(null, root); |
2092 | 2226 | } |
2093 | 2227 | if (options.processCommonJSModules) { |
2094 | | - ProcessCommonJSModules cjs = new ProcessCommonJSModules(this, true); |
| 2228 | + ProcessCommonJSModules cjs = new ProcessCommonJSModules(this); |
2095 | 2229 | cjs.process(null, root); |
2096 | 2230 | } |
2097 | 2231 | } |
|
0 commit comments