How to resolve “Ambiguous reference to a JS library”?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 00:49:30

问题


I'm struggling resolving some Javascript library dependency clashes in the following build.sbt file:

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"
)

When running the fastOptJS task I'm getting the following error message:

[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 */

How can I resolve the ambiguous references to Javascript libraries in this case?


回答1:


Based on this Github issue, I learned that it is possible to specify a qualifying path to disambiguate references to Javascript libraries. Let's try to disambiguate the jquery.js reference by adding the path dist.

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

But running fastOptJS again returns the same error

[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

This problem is actually created by the libraryDependencies entry "org.querki" %%% "jquery-facade" % "0.10". Using the dependency graph SBT plugin we see the full dependency graph:

[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

The output shows org.querki:jquery-facade_sjs0.6_2.11:0.10 to depend on org.webjars:jquery:2.1.4. This explains the Ambiguous reference to a JS library error message above because we still have two jquery library versions available in the listed dependencies.

What we can try next is use exclude on the library dependency.

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

Running the fastOptJS task now returns

[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

So although we got rid of some errors, we are not quite there yet. I'm not 100% sure here, but it seems this error is a result in 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"

So it seems we ran into the same problem we had at the start. A unqualified jquery.js (i.e. without a preceding path) cannot not be resolved unambiguously.

To solve this problem we can use to the jsManifestFilter setting. I found this tip in the 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
      )
    }
  }
}

Here we overwrite the jsManitestFilter setting and explicitly filter out the naked jquery.js dependency. The fastOptJS task runs fine now:

[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

Note that the added println statement also outputs the resourceName for the included dependencies.



来源:https://stackoverflow.com/questions/35756676/how-to-resolve-ambiguous-reference-to-a-js-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!