closure-compiler
closure-compiler copied to clipboard
Compiler crashes with dymamic imports (`IllegalStateException`)
% echo 'const x = async () => { const buffer = await import(/** @type {string} */("buffer")); alert("1", buffer);}; x();' | google-closure-compiler -W QUIET --module_resolution WEBPACK -O ADVANCED
The compiler is waiting for input via stdin.
java.lang.IllegalStateException: AST should not contain Dynamic module import. Reference node:
DYNAMIC_IMPORT 1:45 [length: 39] [source_file: stdin]
STRINGLIT buffer 1:74 [length: 8] [is_parenthesized: 1] [source_file: stdin]
Parent node:
AWAIT 1:39 [length: 45] [source_file: stdin]
DYNAMIC_IMPORT 1:45 [length: 39] [source_file: stdin]
STRINGLIT buffer 1:74 [length: 8] [is_parenthesized: 1] [source_file: stdin]
at com.google.javascript.jscomp.AstValidator$1.handleViolation(AstValidator.java:87)
at com.google.javascript.jscomp.AstValidator.violation(AstValidator.java:2135)
at com.google.javascript.jscomp.AstValidator.validateFeature(AstValidator.java:2214)
at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:456)
at com.google.javascript.jscomp.AstValidator.validateAwait(AstValidator.java:635)
at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:452)
at com.google.javascript.jscomp.AstValidator.validateNameDeclarationChild(AstValidator.java:1352)
at com.google.javascript.jscomp.AstValidator.validateNameDeclarationHelper(AstValidator.java:1316)
at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:210)
at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:157)
at com.google.javascript.jscomp.AstValidator.validateBlock(AstValidator.java:985)
at com.google.javascript.jscomp.AstValidator.validateFunctionExpressionHelper(AstValidator.java:1103)
at com.google.javascript.jscomp.AstValidator.validateFunctionExpression(AstValidator.java:1084)
at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:432)
at com.google.javascript.jscomp.AstValidator.validateCall(AstValidator.java:1172)
at com.google.javascript.jscomp.AstValidator.validateExpression(AstValidator.java:420)
at com.google.javascript.jscomp.AstValidator.validateExprStmt(AstValidator.java:1561)
at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:213)
at com.google.javascript.jscomp.AstValidator.validateStatement(AstValidator.java:157)
at com.google.javascript.jscomp.AstValidator.validateStatements(AstValidator.java:151)
at com.google.javascript.jscomp.AstValidator.validateScript(AstValidator.java:140)
at com.google.javascript.jscomp.AstValidator.validateCodeRoot(AstValidator.java:126)
at com.google.javascript.jscomp.AstValidator.process(AstValidator.java:111)
at com.google.javascript.jscomp.PhaseOptimizer$NamedPass.process(PhaseOptimizer.java:240)
at com.google.javascript.jscomp.PhaseOptimizer.process(PhaseOptimizer.java:179)
at com.google.javascript.jscomp.Compiler.performFinalizations(Compiler.java:2981)
at com.google.javascript.jscomp.Compiler.lambda$stage3Passes$8(Compiler.java:1054)
at com.google.javascript.jscomp.CompilerExecutor.lambda$runInCompilerThread$0(CompilerExecutor.java:100)
at [email protected]/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at [email protected]/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at [email protected]/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at [email protected]/java.lang.Thread.run(Thread.java:833)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)
Dynamic imports are only supported if the output chunk type is ES_MODULES. This works:
$ echo 'const x = async () => { const buffer = await import(/** @type {string} */("buffer")); alert("1", buffer);}; x();' | google-closure-compiler -W QUIET --module_resolution WEBPACK -O ADVANCED --chunk_output_type ES_MODULES
The compiler is waiting for input via stdin.
(async()=>{const a=await import("buffer");alert("1",a)})();export{};
The crash is not great, though. Would be nice if there was a good error message that mentioned the chunk type requirement.
Thanks, I'll see if that works in my application.
Dynamic imports are only supported if the output chunk type is ES_MODULES.
Hm, that may be a design decision you've made and I'm fine leaving it at that. However, dynamic imports are, in principle, allowed everywhere (providing support for import
, of course). E.g., <script>import('foo').finally(()=>alert("Yay!"))</script>
is perfectly valid, no need for <script type="module">
there.
It works in v20230502
and before, but it fails from v20230802
. Hope this issue can be resolved soon.