Skip to content

Commit 833f465

Browse files
rishipalcopybara-github
authored andcommitted
Ensure features MODULES, IMPORT_META, DYNAMIC_IMPORT are removed from the compiler's featureSet when compiler does not output modules
These features should exist only when `ChunkOutputType.ES_MODULES` is set as per: https://source.corp.google.com/piper///depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/CompilerOptions.java;rcl=547026935;l=2969 But JSCompiler forgets to remove those 3 features from the featureSet in regular compilation (GLOBAL_NAMESPACE). PiperOrigin-RevId: 551247757
1 parent d58c251 commit 833f465

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.javascript.jscomp;
1818

19+
import com.google.javascript.jscomp.CompilerOptions.ChunkOutputType;
1920
import com.google.javascript.jscomp.Es6RewriteDestructuring.ObjectDestructuringRewriteMode;
2021
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
2122
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
@@ -108,6 +109,18 @@ public static void addEarlyOptimizationTranspilationPasses(
108109
passes.maybeAdd(rewriteCatchWithNoBinding);
109110
}
110111

112+
if (options.getChunkOutputType() != ChunkOutputType.ES_MODULES) {
113+
// Default output mode of JSCompiler is a script, unless chunkOutputType is set to
114+
// `ES_MODULES` where each output chunk is an ES module.
115+
passes.maybeAdd(createFeatureRemovalPass("markModulesRemoved", Feature.MODULES));
116+
// Since import.meta cannot be transpiled, it is passed-through when the output format
117+
// is a module. Otherwise it must be marked removed.
118+
passes.maybeAdd(createFeatureRemovalPass("markImportMetaRemoved", Feature.IMPORT_META));
119+
// Dynamic imports are preserved for open source output only when the chunk output type is
120+
// ES_MODULES
121+
passes.maybeAdd(createFeatureRemovalPass("markDynamicImportRemoved", Feature.DYNAMIC_IMPORT));
122+
}
123+
111124
if (options.needsTranspilationOf(Feature.FOR_AWAIT_OF)
112125
|| options.needsTranspilationOf(Feature.ASYNC_GENERATORS)) {
113126
passes.maybeAdd(rewriteAsyncIteration);

0 commit comments

Comments
 (0)