|
45 | 45 | /**
|
46 | 46 | * Rewrite dynamic import expressions to account for bundling and module rewriting. Since dynamic
|
47 | 47 | * 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. |
49 | 49 | *
|
50 | 50 | * <p>If the import specifier is a string literal and the module resolver recognizes the target, the
|
51 | 51 | * pass retargets the specifier to the correct output chunk.
|
|
61 | 61 | * <pre>
|
62 | 62 | * imprt_('./output-chunk0.js').then(function() { return module$output$chunk0; });
|
63 | 63 | * </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. |
64 | 84 | */
|
65 | 85 | public class RewriteDynamicImports extends NodeTraversal.AbstractPostOrderCallback
|
66 | 86 | implements CompilerPass {
|
67 | 87 |
|
68 | 88 | static final DiagnosticType DYNAMIC_IMPORT_ALIASING_REQUIRED =
|
69 | 89 | DiagnosticType.warning(
|
70 | 90 | "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. " |
72 | 92 | + "Use the --dynamic_import_alias flag.");
|
73 | 93 |
|
74 | 94 | static final DiagnosticType DYNAMIC_IMPORT_INVALID_ALIAS =
|
@@ -110,10 +130,11 @@ public void process(Node externs, Node root) {
|
110 | 130 | if (wrappedDynamicImportCallback) {
|
111 | 131 | injectWrappingFunctionExtern();
|
112 | 132 | }
|
| 133 | + |
113 | 134 | if (dynamicImportsRemoved) {
|
114 |
| - // This pass removes dynamic import, but adds arrow functions. |
115 |
| - // TODO(lharker): marking the feature not allowed should be unconditional |
116 | 135 | compiler.markFeatureNotAllowed(Feature.DYNAMIC_IMPORT);
|
| 136 | + |
| 137 | + // This pass removes dynamic import, but adds arrow functions. |
117 | 138 | if (this.requiresAliasing && aliasIsValid()) {
|
118 | 139 | NodeTraversal.traverse(compiler, externs, new AliasInjectingTraversal());
|
119 | 140 | }
|
|
0 commit comments