【问题标题】:grunt-closure-compiler modules throwing IllegalArgumentExceptiongrunt-closure-compiler 模块抛出 IllegalArgumentException
【发布时间】:2016-01-26 11:08:08
【问题描述】:

我正在使用this grunt-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$97.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$000(Compiler.java:89)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:632)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:629)
at com.google.javascript.jscomp.CompilerExecutor$2.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 个基本模块?您可以尝试为test_mod__c 和test_mod__d 创建一个公共基础模块。这样的基本模块允许有零个文件来支持这种情况。
  • 抱歉,基本模块是什么意思?据我了解,a 取决于b 和d,而b 取决于c 和d。代码的入口点是a。
  • 评论太长了 - 我会添加答案。

标签: javascript google-closure-compiler


【解决方案1】:

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

您的标志列出了 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 并且是插件作者

【讨论】:

  • 谢谢!任何地方都有这方面的文件吗?找不到任何解释 Closure 模块系统的东西,我有一个从 ImpactJS 模块系统转换到它的繁重任务。看起来它们的工作方向相反,所以我的繁重任务需要进行相当大的重写。
  • 输出模块和JS模块是有区别的。见github.com/google/closure-compiler/wiki/JS-Modules。如果你能给我指出一些这种转变的例子,我可以给你更多的见解。我是模块互操作的主要编译器开发人员。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 2014-09-18
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多