scalacenter / scalajs-bundler

https://scalacenter.github.io/scalajs-bundler
Other
234 stars 101 forks source link

some npm js lib is not bundled into js-opt-bundle.js #360

Closed doofin closed 4 years ago

doofin commented 4 years ago

It seems that some kind of js lib is not bundled ,like https://www.npmjs.com/package/marked .

sbt Setup :

npmDependencies in Compile ++= Seq( "marked" -> "0.8.2")
import scala.scalajs.js.Dynamic.{global => g}
g.marked("string") 

browser complains about marked is not defined.However,include this script directly via script tag works().

sjrd commented 4 years ago

Please elaborate, at least with some kind of reproduction. Currently this report is not actionable.

doofin commented 4 years ago

Sorry for that,I have add more info.

raquo commented 4 years ago

Just because you listed this as a dependency, doesn't mean it will be added to the bundle, only that it will be added to the list of dependencies in package.json. You also need to define a @JSImport in your code (see scala.js docs for the exact syntax) referencing this dependency, and then also make sure to reference the JSImport-ed object from a val so that it doesn't get dead-code-eliminated (objects are lazy so if not accessed scala.js will eliminate them from the bundle).

doofin commented 4 years ago

Thank you! I will have a look.

doofin commented 4 years ago
object f2 {
  @js.native
  @JSGlobal("marked")
  def marked(string: String): Node =
    js.native

}

Still not working,maybe due to the irregular layout of the lib

sjrd commented 4 years ago

You need to somehow use @JSImport, not @JSGlobal. (Or not using scalajs-bundler at all...)

fdietze commented 4 years ago

@doofin Here is a simple facade for marked:

https://github.com/woost/wust2/blob/master/webUtil/src/main/scala/wust/facades/marked/Marked.scala

doofin commented 4 years ago

Thanks! @fdietze