├── .gitignore ├── README.md ├── build.sbt ├── bundle ├── browserify.js ├── build.sbt ├── lib.js ├── package.json ├── start.js └── test-bundle.html ├── project ├── build.properties └── plugins.sbt ├── src └── main │ ├── resources │ └── bundle.js │ └── scala │ └── app │ └── Main.scala └── test.html /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .idea/ 3 | node_modules/ 4 | src/main/resources/bundle.js 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A sample to create a bundle with Browserify and use it from ScalaJS. 2 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | name := "hellosjs" 2 | 3 | scalaVersion := "2.11.7" 4 | 5 | enablePlugins(ScalaJSPlugin) 6 | 7 | val bundle = project.in(file("bundle")) 8 | 9 | jsDependencies += ProvidedJS / "bundle.js" 10 | 11 | persistLauncher in Compile := true 12 | -------------------------------------------------------------------------------- /bundle/browserify.js: -------------------------------------------------------------------------------- 1 | var inf = './start.js' 2 | var outf = './bundle.js' 3 | if(process.argv.length>2) 4 | inf = process.argv[2] 5 | if(process.argv.length>3) 6 | outf = process.argv[3] 7 | console.log(outf) 8 | var fs = require("fs") 9 | var browserify = require('browserify'); 10 | var b = browserify(inf, 11 | { standalone: 'Bundle'}); 12 | var out = fs.createWriteStream(outf) 13 | b.bundle().pipe(out) 14 | -------------------------------------------------------------------------------- /bundle/build.sbt: -------------------------------------------------------------------------------- 1 | name := "bundle" 2 | 3 | scalaVersion := "2.11.7" 4 | 5 | lazy val bundle = taskKey[Unit]("bundle") 6 | 7 | enablePlugins(SbtWeb) 8 | 9 | enablePlugins(SbtJsEngine) 10 | 11 | import com.typesafe.sbt.jse.JsEngineImport.JsEngineKeys._ 12 | import com.typesafe.sbt.jse.SbtJsTask._ 13 | import com.typesafe.sbt.jse.SbtJsEngine.autoImport.JsEngineKeys._ 14 | import scala.concurrent.duration._ 15 | 16 | bundle := { 17 | ( npmNodeModules in Assets ).value 18 | val inf = (baseDirectory.value / "start.js").getAbsolutePath 19 | val res = baseDirectory.value.getParentFile / "src" / "main" / "resources" 20 | res.mkdirs 21 | val outf = (res / "bundle.js").getAbsolutePath 22 | val modules = (baseDirectory.value / "node_modules").getAbsolutePath 23 | println(s"Bundling: ${inf}\n -> ${outf}") 24 | executeJs(state.value, 25 | engineType.value, 26 | None, 27 | Seq(modules), 28 | baseDirectory.value / "browserify.js", 29 | Seq(inf, outf), 30 | 30.seconds) 31 | () 32 | } 33 | -------------------------------------------------------------------------------- /bundle/lib.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "jquery": require("jquery-browserify"), 3 | "lodash": require("lodash") 4 | } 5 | 6 | -------------------------------------------------------------------------------- /bundle/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "static", 3 | "version": "0.1.0", 4 | "repository": { 5 | "type": "git", 6 | "url": "git@github.com:sciabarra/jgh.git" 7 | }, 8 | "dependencies": { 9 | "browserify": "13.0.0", 10 | "jquery-browserify": "1.8.1", 11 | "lodash": "4.3.0" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /bundle/start.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib.js") 2 | -------------------------------------------------------------------------------- /bundle/test-bundle.html: -------------------------------------------------------------------------------- 1 |

2 | 3 | 11 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.9 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.6") 2 | 3 | addSbtPlugin("com.typesafe.sbt" % "sbt-web" % "1.1.1") 4 | 5 | addSbtPlugin("com.typesafe.sbt" % "sbt-js-engine" % "1.1.3") 6 | -------------------------------------------------------------------------------- /src/main/scala/app/Main.scala: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import scala.scalajs.js, js.JSApp 4 | 5 | @js.native 6 | object Bundle extends js.Object { 7 | def jquery : js.Function1[js.Any, Jquery] = js.native 8 | def lodash: Lodash = js.native 9 | } 10 | 11 | @js.native 12 | trait Jquery extends js.Object { 13 | def text(arg: js.Any): Jquery = js.native 14 | } 15 | 16 | @js.native 17 | trait Lodash extends js.Object { 18 | def camelCase(arg: js.Any): String = js.native 19 | } 20 | 21 | 22 | object Main extends JSApp { 23 | def main(): Unit = { 24 | println("hello from scalajs") 25 | import Bundle._ 26 | jquery("#title").text(lodash.camelCase("This is a test")) 27 | //println(js.Dynamic.global.Bundle) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Title

4 | 5 | 6 | 7 | 8 | 9 | --------------------------------------------------------------------------------