grunt-closure-compiler 模块抛出 IllegalArgumentException

grunt-closure-compiler modules throwing IllegalArgumentException

我正在使用 this g运行t-closure-compiler 任务的分支来使用闭包编译器构建多个文件。

我设置了以下编译器标志:

--module_output_path_prefix .\  --js lib\test\mod-c.js  --module test_mod__c:1  --js lib\test\mod-d.js  --module test_mod__d:1  --js lib\test\mod-b.js  --module test_mod__b:1:test_mod__c,test_mod__d  --js lib\test\mod-a.js  --module test_mod__a:1:test_mod__b,test_mod__d  --compilation_level "ADVANCED_OPTIMIZATIONS" --language_in "ECMASCRIPT5_STRICT"

每当我运行这个,我得到以下错误:

java.lang.IllegalArgumentException: expected one element but was: <test_mod__c, test_mod__d>
at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:317)
at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:289)
at com.google.javascript.jscomp.JSModuleGraph.getRootModule(JSModuleGraph.java:150)
at com.google.javascript.jscomp.AnalyzePrototypeProperties.<init>(AnalyzePrototypeProperties.java:122)
at com.google.javascript.jscomp.CrossModuleMethodMotion.<init>(CrossModuleMethodMotion.java:79)
at com.google.javascript.jscomp.DefaultPassConfig.create(DefaultPassConfig.java:2170)
at com.google.javascript.jscomp.PhaseOptimizer$NamedPass.process(PhaseOptimizer.java:285)
at com.google.javascript.jscomp.PhaseOptimizer$Loop.process(PhaseOptimizer.java:458)
at com.google.javascript.jscomp.PhaseOptimizer.process(PhaseOptimizer.java:217)
at com.google.javascript.jscomp.Compiler.optimize(Compiler.java:1901)
at com.google.javascript.jscomp.Compiler.compileInternal(Compiler.java:681)
at com.google.javascript.jscomp.Compiler.access[=11=]0(Compiler.java:89)
at com.google.javascript.jscomp.Compiler.call(Compiler.java:632)
at com.google.javascript.jscomp.Compiler.call(Compiler.java:629)
at com.google.javascript.jscomp.CompilerExecutor.call(CompilerExecutor.java:93)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

根据我能找到的一些资料,这通常是由无效的依赖树引起的,但我不明白为什么会出错。

输出模块必须描述一棵树。该要求意味着所有其他模块都来自一个基本模块。基本模块是不依赖于其他模块的模块。

您的标志列出了 2 个基本模块:

  • --module test_mod__c:1
  • --module test_mod__d:1

这些是基本模块,因为它们不依赖于其他模块。从您的评论来看,我认为您可能在倒着描述这棵树。根据您对 a 是入口点的评论,我相信您想要的树看起来像这样:

test_mod__a
└─ test_mod__b
|  ├─ test_mod__c
└─ └─ test_mod__d

以下是描述这棵树的标志:

--module_output_path_prefix .\
--js lib\test\mod-a.js --module test_mod__a:1
--js lib\test\mod-b.js --module test_mod__b:1:test_mod__a
--js lib\test\mod-c.js --module test_mod__c:1:test_mod__b
--js lib\test\mod-d.js --module test_mod__d:1:test_mod__a,test_mod__b

此外,您可能想使用 official npm version of the compiler - 它现在包含一个 grunt 插件并支持 --module 标志。

免责声明:我管理 Closure-compiler 到 npm 的发布并且是插件作者