sjrd on 0.6.x
Remove dead code: specific coll… Adapt the signature of `js.Arra… Merge pull request #3554 from s… (compare)
sjrd on master
Fix analyzer cycle detection to… Add toString methods to analyze… Do not provide linked ClassInfo… and 1 more (compare)
sjrd on master
Remove Logger.success It is un… Make level helpers final Clean-up ScalaConsoleLogger cod… and 1 more (compare)
++
)
Hi,
I am building for a while my scala-js application at runtime because my application is pluggable. I have to be abble to add plugins at runtime or restart the application including new plugins. It use the Linker methods to achieve this (see sources here: https://github.com/openmole/openmole/blob/13-dev/openmole/gui/server/org.openmole.gui.server.jscompile/src/main/scala/org/openmole/gui/server/jscompile/JSPack.scala).
Until now, I dealt with my js lib dependencies by hand, merging js deps in a big js file. But I had often js loading or depencies resolution issues. So I try to use webpack, which is pretty straight forward when everything is compiled and linked from the sbt file. But not really in my case.
My idea was to
1) fastOptJS / webpack an empty project, which depends on the same dependencies as my application (and just calling a couple of fake methods to trigger webpack), so that I get a js file resolving all the js dependencies I need for my main project,
2) build my application with fastOptJS (but with no wepback resolution)
3) merge both js in a big one.
4) compress this big js file (if it is possible afterwards)
The problem is that the main method of the main application is not recognized any more, the html body tag remains empty and nothing happens when the html is run.
Any idea on how to solve my problem ? Is the way I do seems to be the good one ?
Thanks
scalaJSUseMainModuleInitializer := true
) and I fastOptJS / webpack my empty project on an other hand. But it should lead to the same result, right ?
lazy val emptyProject = project.in(file("packJS")) enablePlugins (ScalaJSBundlerPlugin) dependsOn(ace, bootstrapnative, tools, highlightjs)
lazy val runBootstrapDemo = taskKey[Unit]("runBootsrapDemo")
lazy val bootstrapDemo = project.in(file("bootstrapDemo")) enablePlugins (ScalaJSBundlerPlugin) settings(
sourceCode,
scalaJSUseMainModuleInitializer := true,
runBootstrapDemo := {
val demoResource = (resourceDirectory in Compile).value
val deps = (fullOptJS / webpack in emptyProject in Compile).value.head.data
val jsBuild = (fullOptJS in Compile).value.data
IO.copyFile(merge(jsBuild, deps), target.value / "js/demobootstrapnative.js")
IO.copyDirectory(demoResource, target.value)
}
) dependsOn(ace, bootstrapnative, tools, highlightjs)
def merge(f1: File, in: File) = {
val from = java.nio.file.Files.lines(in.toPath)
val bw = new BufferedWriter(new FileWriter(f1))
from.forEach { p => bw.append(p + "\n") }
bw.close
f1
}
emptyProject
, having no main method, will dead-code-eliminate everything, including its imports from other modules. That in turn will cause Webpack to think that you're not referencing any extra library.
require
in the JS code produced by Scala.js. With your setup, that won't happen.
Uncaught ReferenceError: require is not defined
new FileWriter(f1, true)
.
def merge(first: File, second: File) = {
val f1 = java.nio.file.Files.lines(first.toPath)
val f2 = java.nio.file.Files.lines(second.toPath)
val in = java.io.File.createTempFile("merge", "JS")
val bw = new BufferedWriter(new FileWriter(in, true))
f1.forEach { p => bw.append(p + "\n") }
f2.forEach { p => bw.append(p + "\n") }
bw.close
in
}
Also Webpack is supposed to rewrite the calls to require
in the JS code produced by Scala.js. With your setup, that won't happen.
ScalaJSBundler
's LibraryAndApplication
mode could save you.
lazy val emptyProject = project.in(file("packJS")) enablePlugins (ScalaJSBundlerPlugin) settings(
scalaJSUseMainModuleInitializer := true,
webpackBundlingMode := LibraryAndApplication()
)dependsOn(ace, bootstrapnative, tools, highlightjs)
object PackJS {
def main(argvs: Array[String]): Unit = {
def pack = {
val editorDiv = div(idAttr := "editor")
ace.edit(editorDiv.ref)
Tabs.tabs()
HighlightJS.initHighlightingOnLoad()
}
}
}