|
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;
|
@@ -1750,7 +1749,19 @@ Node parseInputs() {
|
1750 | 1749 | options.moduleResolutionMode,
|
1751 | 1750 | processJsonInputs(inputs));
|
1752 | 1751 | }
|
| 1752 | + } else { |
| 1753 | + // Use an empty module loader if we're not actually dealing with modules. |
| 1754 | + this.moduleLoader = ModuleLoader.EMPTY; |
| 1755 | + } |
1753 | 1756 |
|
| 1757 | + if (options.getDependencyOptions().needsManagement()) { |
| 1758 | + findDependenciesFromEntryPoints( |
| 1759 | + options.getLanguageIn().toFeatureSet().has(Feature.MODULES), |
| 1760 | + options.processCommonJSModules, |
| 1761 | + options.transformAMDToCJSModules); |
| 1762 | + } else if (options.needsTranspilationOf(Feature.MODULES) |
| 1763 | + || options.transformAMDToCJSModules |
| 1764 | + || options.processCommonJSModules) { |
1754 | 1765 | if (options.getLanguageIn().toFeatureSet().has(Feature.MODULES)) {
|
1755 | 1766 | parsePotentialModules(inputs);
|
1756 | 1767 | }
|
@@ -1783,12 +1794,12 @@ Node parseInputs() {
|
1783 | 1794 | }
|
1784 | 1795 | }
|
1785 | 1796 |
|
1786 |
| - if (!inputsToRewrite.isEmpty()) { |
1787 |
| - forceToEs6Modules(inputsToRewrite.values()); |
| 1797 | + for (CompilerInput input : inputsToRewrite.values()) { |
| 1798 | + forceInputToPathBasedModule( |
| 1799 | + input, |
| 1800 | + options.getLanguageIn().toFeatureSet().has(Feature.MODULES), |
| 1801 | + options.processCommonJSModules); |
1788 | 1802 | }
|
1789 |
| - } else { |
1790 |
| - // Use an empty module loader if we're not actually dealing with modules. |
1791 |
| - this.moduleLoader = ModuleLoader.EMPTY; |
1792 | 1803 | }
|
1793 | 1804 |
|
1794 | 1805 | orderInputs();
|
@@ -1896,6 +1907,142 @@ void orderInputs() {
|
1896 | 1907 | }
|
1897 | 1908 | }
|
1898 | 1909 |
|
| 1910 | + /** |
| 1911 | + * Find dependencies by recursively traversing each dependency of an input starting with the entry |
| 1912 | + * points. Causes a full parse of each file, but since the file is reachable by walking the graph, |
| 1913 | + * this would be required in later compilation passes regardless. |
| 1914 | + * |
| 1915 | + * <p>Inputs which are not reachable during graph traversal will be dropped. |
| 1916 | + * |
| 1917 | + * <p>If the dependency mode is set to LOOSE, inputs for which the deps package did not find a |
| 1918 | + * provide statement or detect as a module will be treated as entry points. |
| 1919 | + */ |
| 1920 | + void findDependenciesFromEntryPoints( |
| 1921 | + boolean supportEs6Modules, boolean supportCommonJSModules, boolean supportAmdModules) { |
| 1922 | + hoistExterns(); |
| 1923 | + List<CompilerInput> entryPoints = new ArrayList<>(); |
| 1924 | + Map<String, CompilerInput> inputsByProvide = new HashMap<>(); |
| 1925 | + Map<String, CompilerInput> inputsByIdentifier = new HashMap<>(); |
| 1926 | + for (CompilerInput input : inputs) { |
| 1927 | + if (!options.getDependencyOptions().shouldDropMoochers() && input.getProvides().isEmpty()) { |
| 1928 | + entryPoints.add(input); |
| 1929 | + } |
| 1930 | + inputsByIdentifier.put( |
| 1931 | + ModuleIdentifier.forFile(input.getPath().toString()).toString(), input); |
| 1932 | + for (String provide : input.getProvides()) { |
| 1933 | + if (!provide.startsWith("module$")) { |
| 1934 | + inputsByProvide.put(provide, input); |
| 1935 | + } |
| 1936 | + } |
| 1937 | + } |
| 1938 | + for (ModuleIdentifier moduleIdentifier : options.getDependencyOptions().getEntryPoints()) { |
| 1939 | + CompilerInput input = inputsByIdentifier.get(moduleIdentifier.toString()); |
| 1940 | + if (input != null) { |
| 1941 | + entryPoints.add(input); |
| 1942 | + } |
| 1943 | + } |
| 1944 | + |
| 1945 | + Set<CompilerInput> workingInputSet = new HashSet<>(inputs); |
| 1946 | + List<CompilerInput> orderedInputs = new ArrayList<>(); |
| 1947 | + for (CompilerInput entryPoint : entryPoints) { |
| 1948 | + orderedInputs.addAll( |
| 1949 | + depthFirstDependenciesFromInput( |
| 1950 | + entryPoint, |
| 1951 | + /* wasImportedByModule = */ false, |
| 1952 | + workingInputSet, |
| 1953 | + inputsByIdentifier, |
| 1954 | + inputsByProvide, |
| 1955 | + supportEs6Modules, |
| 1956 | + supportCommonJSModules, |
| 1957 | + supportAmdModules)); |
| 1958 | + } |
| 1959 | + |
| 1960 | + // TODO(ChadKillingsworth) Move this into the standard compilation passes |
| 1961 | + if (supportCommonJSModules) { |
| 1962 | + for (CompilerInput input : orderedInputs) { |
| 1963 | + new ProcessCommonJSModules(this) |
| 1964 | + .process(/* externs */ null, input.getAstRoot(this), /* forceModuleDetection */ false); |
| 1965 | + } |
| 1966 | + } |
| 1967 | + } |
| 1968 | + |
| 1969 | + /** For a given input, order it's dependencies in a depth first traversal */ |
| 1970 | + private List<CompilerInput> depthFirstDependenciesFromInput( |
| 1971 | + CompilerInput input, |
| 1972 | + boolean wasImportedByModule, |
| 1973 | + Set<CompilerInput> inputs, |
| 1974 | + Map<String, CompilerInput> inputsByIdentifier, |
| 1975 | + Map<String, CompilerInput> inputsByProvide, |
| 1976 | + boolean supportEs6Modules, |
| 1977 | + boolean supportCommonJSModules, |
| 1978 | + boolean supportAmdModules) { |
| 1979 | + List<CompilerInput> orderedInputs = new ArrayList<>(); |
| 1980 | + if (!inputs.remove(input)) { |
| 1981 | + // It's possible for a module to be included as both a script |
| 1982 | + // and a module in the same compilation. In these cases, it should |
| 1983 | + // be forced to be a module. |
| 1984 | + if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) { |
| 1985 | + forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules); |
| 1986 | + } |
| 1987 | + |
| 1988 | + return orderedInputs; |
| 1989 | + } |
| 1990 | + |
| 1991 | + if (supportAmdModules) { |
| 1992 | + new TransformAMDToCJSModule(this).process(null, input.getAstRoot(this)); |
| 1993 | + } |
| 1994 | + |
| 1995 | + FindModuleDependencies findDeps = |
| 1996 | + new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules); |
| 1997 | + findDeps.process(input.getAstRoot(this)); |
| 1998 | + |
| 1999 | + // If this input was imported by another module, it is itself a module |
| 2000 | + // so we force it to be detected as such. |
| 2001 | + if (wasImportedByModule && input.getJsModuleType() == CompilerInput.ModuleType.NONE) { |
| 2002 | + forceInputToPathBasedModule(input, supportEs6Modules, supportCommonJSModules); |
| 2003 | + } |
| 2004 | + |
| 2005 | + for (String requiredNamespace : input.getRequires()) { |
| 2006 | + CompilerInput requiredInput = null; |
| 2007 | + boolean requiredByModuleImport = false; |
| 2008 | + if (inputsByProvide.containsKey(requiredNamespace)) { |
| 2009 | + requiredInput = inputsByProvide.get(requiredNamespace); |
| 2010 | + } else if (inputsByIdentifier.containsKey(requiredNamespace)) { |
| 2011 | + requiredByModuleImport = true; |
| 2012 | + requiredInput = inputsByIdentifier.get(requiredNamespace); |
| 2013 | + } |
| 2014 | + |
| 2015 | + if (requiredInput != null) { |
| 2016 | + orderedInputs.addAll( |
| 2017 | + depthFirstDependenciesFromInput( |
| 2018 | + requiredInput, |
| 2019 | + requiredByModuleImport, |
| 2020 | + inputs, |
| 2021 | + inputsByIdentifier, |
| 2022 | + inputsByProvide, |
| 2023 | + supportEs6Modules, |
| 2024 | + supportCommonJSModules, |
| 2025 | + supportAmdModules)); |
| 2026 | + } |
| 2027 | + } |
| 2028 | + orderedInputs.add(input); |
| 2029 | + return orderedInputs; |
| 2030 | + } |
| 2031 | + |
| 2032 | + private void forceInputToPathBasedModule( |
| 2033 | + CompilerInput input, boolean supportEs6Modules, boolean supportCommonJSModules) { |
| 2034 | + |
| 2035 | + if (supportEs6Modules) { |
| 2036 | + FindModuleDependencies findDeps = |
| 2037 | + new FindModuleDependencies(this, supportEs6Modules, supportCommonJSModules); |
| 2038 | + findDeps.convertToEs6Module(input.getAstRoot(this)); |
| 2039 | + input.setJsModuleType(CompilerInput.ModuleType.ES6); |
| 2040 | + } else if (supportCommonJSModules) { |
| 2041 | + new ProcessCommonJSModules(this).process(null, input.getAstRoot(this), true); |
| 2042 | + input.setJsModuleType(CompilerInput.ModuleType.COMMONJS); |
| 2043 | + } |
| 2044 | + } |
| 2045 | + |
1899 | 2046 | /**
|
1900 | 2047 | * Hoists inputs with the @externs annotation into the externs list.
|
1901 | 2048 | */
|
@@ -2005,18 +2152,6 @@ Map<String, String> processJsonInputs(List<CompilerInput> inputsToProcess) {
|
2005 | 2152 | return rewriteJson.getPackageJsonMainEntries();
|
2006 | 2153 | }
|
2007 | 2154 |
|
2008 |
| - void forceToEs6Modules(Collection<CompilerInput> inputsToProcess) { |
2009 |
| - for (CompilerInput input : inputsToProcess) { |
2010 |
| - input.setCompiler(this); |
2011 |
| - input.addProvide(input.getPath().toModuleName()); |
2012 |
| - Node root = input.getAstRoot(this); |
2013 |
| - if (root == null) { |
2014 |
| - continue; |
2015 |
| - } |
2016 |
| - Es6RewriteModules moduleRewriter = new Es6RewriteModules(this); |
2017 |
| - moduleRewriter.forceToEs6Module(root); |
2018 |
| - } |
2019 |
| - } |
2020 | 2155 |
|
2021 | 2156 | private List<CompilerInput> parsePotentialModules(List<CompilerInput> inputsToProcess) {
|
2022 | 2157 | List<CompilerInput> filteredInputs = new ArrayList<>();
|
@@ -2055,7 +2190,7 @@ void processAMDAndCommonJSModules() {
|
2055 | 2190 | new TransformAMDToCJSModule(this).process(null, root);
|
2056 | 2191 | }
|
2057 | 2192 | if (options.processCommonJSModules) {
|
2058 |
| - ProcessCommonJSModules cjs = new ProcessCommonJSModules(this, true); |
| 2193 | + ProcessCommonJSModules cjs = new ProcessCommonJSModules(this); |
2059 | 2194 | cjs.process(null, root);
|
2060 | 2195 | }
|
2061 | 2196 | }
|
|
0 commit comments