Closed
Description
TL;DR: when packaging scala-cli and scalajs plugin have different defaults:
scala-cli defaults to CommonJS module type:
Whereas Scala.js when used as SBT plugin defaults to NoModule: https://github.com/scala-js/scala-js/blob/9f7012281eda7185f64e9c3824cf316f2b761081/linker-interface/shared/src/main/scala/org/scalajs/linker/interface/StandardConfig.scala#L73
❯ cat chris.scala
// using scala 2.13.6
// using scala-js
import $dep.`org.scala-js::scalajs-dom::2.0.0`
import org.scalajs.dom
import org.scalajs.dom.document
import scala.scalajs.js.annotation.JSExportTopLevel
object Main extends App {
def appendPar(targetNode: dom.Node, text: String): Unit = {
val parNode = document.createElement("p")
parNode.textContent = text
targetNode.appendChild(parNode)
}
@JSExportTopLevel("addClickedMessage")
def addClickedMessage(): Unit = {
appendPar(document.body, "You clicked the button!")
}
}
❯ scala-cli package chris.scala -f && (cat chris.js | grep addClickedMessage)
Compiling project (Scala 2.13.6, Scala.JS)
Compiled project (Scala 2.13.6, Scala.JS)
Wrote chris.js, run it with
node ./chris.js
$c_LMain$.prototype.addClickedMessage__V = (function() {
exports.addClickedMessage = (function() {
$m_LMain$().addClickedMessage__V()
❯ scala-cli package chris.scala --js-module-kind none -f && (cat chris.js | grep addClickedMessage)
Wrote chris.js, run it with
node ./chris.js
let addClickedMessage;
$c_LMain$.prototype.addClickedMessage__V = (function() {
addClickedMessage = (function() {
$m_LMain$().addClickedMessage__V()
Current default means you can't run the packaged script in the browser without altering configuration.
Stems from a Discord discusion