如何解决"对JS库的不明确引用"?

时间:2016-03-02 19:45:07

标签: scala.js

我正在努力解决以下build.sbt文件中的一些Javascript库依赖性冲突:

lazy val root = project.in(file(".")).enablePlugins(ScalaJSPlugin)

scalaVersion := "2.11.7"

libraryDependencies ++= Seq(
  "org.scala-js" %%% "scalajs-java-time" % "0.1.0",
  "org.querki" %%% "jquery-facade" % "0.10"
)

jsDependencies ++= Seq(
  "org.webjars.bower" % "jquery" % "3.0.0-beta1" / "jquery.js"
)

运行fastOptJS任务时,我收到以下错误消息:

[trace] Stack trace suppressed: run last compile:resolvedJSDependencies for the full output.
[error] (compile:resolvedJSDependencies) org.scalajs.core.tools.jsdep.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.min.js
[error]   Possible paths found on the classpath:
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.min.js
[error]   - META-INF/resources/webjars/jquery/2.1.4/jquery.min.js
[error]   originating from: root:compile
[error] - Ambiguous reference to a JS library: jquery.js
[error]   Possible paths found on the classpath:
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error]   - META-INF/resources/webjars/jquery/2.1.4/jquery.js
[error]   originating from: root:compile, root:compile
[error] Total time: 1 s, completed Mar 2, 2016 12:36:43 PM */

在这种情况下,如何解决对Javascript库的模糊引用?

1 个答案:

答案 0 :(得分:3)

基于this Github issue,我了解到可以指定一个限定路径来消除对Javascript库的引用的歧义。 让我们尝试通过添加路径jquery.js消除dist引用的歧义。

jsDependencies ++= Seq(
  "org.webjars.bower" % "jquery" % "3.0.0-beta1" / "dist/jquery.js"
)

但再次运行fastOptJS会返回相同的错误

[error] - Ambiguous reference to a JS library: jquery.min.js
[error]   Possible paths found on the classpath:
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.min.js
[error]   - META-INF/resources/webjars/jquery/2.1.4/jquery.min.js
[error]   originating from: root:compile
[error] - Ambiguous reference to a JS library: jquery.js
[error]   Possible paths found on the classpath:
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error]   - META-INF/resources/webjars/jquery/2.1.4/jquery.js
[error]   originating from: root:compile
[error] Total time: 2 s, completed Mar 2, 2016 7:29:08 PM

此问题实际上是由libraryDependencies条目"org.querki" %%% "jquery-facade" % "0.10"创建的。使用dependency graph SBT plugin,我们可以看到完整的依赖关系图:

[info] root:root_sjs0.6_2.11:0.1-SNAPSHOT [S]
[info]   +-org.querki:jquery-facade_sjs0.6_2.11:0.10 [S]
[info]   | +-org.querki:querki-jsext_sjs0.6_2.11:0.6 [S]
[info]   | | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info]   | | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info]   | | 
[info]   | +-org.scala-js:scalajs-dom_sjs0.6_2.11:0.8.0 [S]
[info]   | | +-org.scala-js:scalajs-library_2.11:0.6.0 (evicted by: 0.6.7)
[info]   | | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info]   | | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info]   | | 
[info]   | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info]   | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info]   | +-org.webjars:jquery:2.1.4
[info]   | 
[info]   +-org.scala-js:scalajs-java-time_sjs0.6_2.11:0.1.0 [S]
[info]   | +-org.scala-js:scalajs-library_2.11:0.6.6 (evicted by: 0.6.7)
[info]   | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info]   | 
[info]   +-org.scala-js:scalajs-library_2.11:0.6.6 (evicted by: 0.6.7)
[info]   +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info]   +-org.webjars.bower:jquery:3.0.0-beta1

输出显示org.querki:jquery-facade_sjs0.6_2.11:0.10取决于org.webjars:jquery:2.1.4。这解释了上面的Ambiguous reference to a JS library错误消息,因为我们仍然在列出的依赖项中提供了两个jquery库版本。

我们接下来要尝试的是对库依赖项使用exclude

libraryDependencies ++= Seq(
  "org.scala-js" %%% "scalajs-java-time" % "0.1.0",
  "org.querki" %%% "jquery-facade" % "0.10" exclude("org.webjars","jquery")
)

现在运行fastOptJS任务返回

[error] (compile:resolvedJSDependencies) org.scalajs.core.tools.jsdep.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.js
[error]   Possible paths found on the classpath:
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error]   - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error]   originating from: root:compile
[error] Total time: 2 s, completed Mar 2, 2016 7:47:14 PM

因此,虽然我们摆脱了一些错误,但我们还没有完全解决。我在这里不是100%肯定,但似乎这个错误是how the build.sbt of the jQuery Facade includes jQuery using jsDependencies itself的结果:

jsDependencies += "org.webjars" % "jquery" % "2.1.4" / "jquery.js" minified "jquery.min.js"

所以我们似乎遇到了我们在开始时遇到的同样问题。不合格的jquery.js(即没有前面的路径)无法明确解决。

要解决此问题,我们可以使用jsManifestFilter设置。我在Scala.js Gitter room中找到了这个提示。

jsManifestFilter := {
  import org.scalajs.core.tools.jsdep.{JSDependencyManifest, JSDependency}

  (seq: Traversable[JSDependencyManifest]) => {
    seq map { manifest =>

      def isOkToInclude(jsDep: JSDependency): Boolean = {
        println(s"jsDep=>$jsDep")
        jsDep.resourceName != "jquery.js"
      }

      new JSDependencyManifest(
        origin = manifest.origin,
        libDeps = manifest.libDeps filter isOkToInclude,
        requiresDOM = manifest.requiresDOM,
        compliantSemantics = manifest.compliantSemantics
      )
    }
  }
}

在这里,我们覆盖jsManitestFilter设置,并明确过滤掉裸jquery.js依赖项。 fastOptJS任务现在正常运行:

[info] Resolving org.eclipse.jetty#jetty-continuation;8.1.16.v20140903 ...
[info] Done updating.
jsDep=>JSDependency(resourceName=dist/jquery.js)
jsDep=>JSDependency(resourceName=jquery.js, minifiedResourceName=Some(jquery.min.js))
[info] Fast optimizing jsdeps/target/scala-2.11/root-fastopt.js
[success] Total time: 3 s, completed Mar 2, 2016 8:27:40 PM

请注意,添加的println语句还会为包含的依赖项输出resourceName

相关问题