Skip to content

Commit 9c24690

Browse files
rishipalcopybara-github
authored andcommitted
Remove the Dynamic Import feature from featureSet when it is rewritten or reported
PiperOrigin-RevId: 552581020
1 parent dca8896 commit 9c24690

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public ForbidDynamicImportUsage(AbstractCompiler compiler) {
3434
@Override
3535
public void process(Node externs, Node root) {
3636
NodeTraversal.traverse(compiler, root, this);
37+
this.compiler.markFeatureNotAllowed(Feature.DYNAMIC_IMPORT);
3738
}
3839

3940
@Override

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/**
4646
* Rewrite dynamic import expressions to account for bundling and module rewriting. Since dynamic
4747
* imports cannot be fully polyfilled, optionally support replacing the expression with a function
48-
* call which indicates an external polyfill is utilized.
48+
* call (alias function) which indicates an external polyfill is utilized.
4949
*
5050
* <p>If the import specifier is a string literal and the module resolver recognizes the target, the
5151
* pass retargets the specifier to the correct output chunk.
@@ -61,14 +61,34 @@
6161
* <pre>
6262
* imprt_('./output-chunk0.js').then(function() { return module$output$chunk0; });
6363
* </pre>
64+
*
65+
* <p>Unlike other "rewrite" passes for which the DefaultPassConfig checks the output language level
66+
* using {@code options.needsTranspilationOf(feature)} before adding a pass, it does not check the
67+
* language level before adding this particular pass. Even when supported by the output level, we
68+
* still invoke this pass if {@code options.shouldAllowDynamicImport()} is set. The only difference
69+
* is that for {@code ES2020}, the dynamic imports syntax can pass through if aliasing is not
70+
* requested, while for {@code < ES2020} they must get rewritten using the alias call.
71+
*
72+
* <pre>
73+
* <p>{@code
74+
* options.shouldAllowDynamicImport() === false?
75+
* ForbidDynamicImportUsage :
76+
* (output >= ES2020) ?
77+
* don't *really* rewrite it unless aliasing is requested :
78+
* rewrite it using the alias call
79+
* }
80+
* </pre>
81+
*
82+
* <p>TODO: b/291319705 For the above reason, the pass should be better named as
83+
* MaybeRewriteDynamicImports.
6484
*/
6585
public class RewriteDynamicImports extends NodeTraversal.AbstractPostOrderCallback
6686
implements CompilerPass {
6787

6888
static final DiagnosticType DYNAMIC_IMPORT_ALIASING_REQUIRED =
6989
DiagnosticType.warning(
7090
"JSC_DYNAMIC_IMPORT_ALIASING_REQUIRED",
71-
"Dynamic import expressions should be aliased for for language level. "
91+
"Dynamic import expressions should be aliased for language level. "
7292
+ "Use the --dynamic_import_alias flag.");
7393

7494
static final DiagnosticType DYNAMIC_IMPORT_INVALID_ALIAS =
@@ -110,10 +130,11 @@ public void process(Node externs, Node root) {
110130
if (wrappedDynamicImportCallback) {
111131
injectWrappingFunctionExtern();
112132
}
133+
113134
if (dynamicImportsRemoved) {
114-
// This pass removes dynamic import, but adds arrow functions.
115-
// TODO(lharker): marking the feature not allowed should be unconditional
116135
compiler.markFeatureNotAllowed(Feature.DYNAMIC_IMPORT);
136+
137+
// This pass removes dynamic import, but adds arrow functions.
117138
if (this.requiresAliasing && aliasIsValid()) {
118139
NodeTraversal.traverse(compiler, externs, new AliasInjectingTraversal());
119140
}

0 commit comments

Comments
 (0)