Skip to content

Commit 18351b8

Browse files
authored
Merge pull request #27 from UdashFramework/js-deps
Support both bundler and JS deps
2 parents 7c48457 + e485417 commit 18351b8

32 files changed

+240
-387
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
- stage: test
1010
script:
1111
- sbt +test +publishLocal
12-
- cd example && sbt compileStatics
12+
- cd example && sbt +compileStatics
1313
- stage: release
1414
if: tag =~ ^v
1515
script:

README.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Static types for the jQuery API for [Scala.js](http://www.scala-js.org/) program
1010
Add the following dependency to your SBT build:
1111

1212
```scala
13-
libraryDependencies += "io.udash" %%% "udash-jquery" % "3.0.2"
13+
libraryDependencies += "io.udash" %%% "udash-jquery" % "3.0.4"
1414
```
1515

1616
then import the jQuery package:
@@ -19,21 +19,9 @@ then import the jQuery package:
1919
import io.udash.wrappers.jquery._
2020
```
2121

22-
Since version `3.0.0` the wrapper is published as a CommonJS module with JS dependencies managed
23-
by [scalajs-bundler](https://github.com/scalacenter/scalajs-bundler).
24-
25-
If you do not want to use the bundler, you have to include jQuery sources manually by:
26-
* explicit link in your `index.html`.
27-
```html
28-
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
29-
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
30-
crossorigin="anonymous"></script>
31-
```
32-
* or a [Scala.js dependency](http://www.scala-js.org/doc/project/dependencies.html).
33-
```scala
34-
jsDependencies +=
35-
"org.webjars" % "jquery" % "3.3.1" / "3.3.1/jquery.js" minified "3.3.1/jquery.min.js"
36-
```
22+
Since version `3.0.4` the wrapper targets SJS 1.x series and supports JS dependencies managed by
23+
by [scalajs-bundler](https://github.com/scalacenter/scalajs-bundler) or [sbt-jsdependencies
24+
](https://github.com/scala-js/jsdependencies)
3725

3826

3927
## Examples

build.sbt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
name := "udash-jquery"
42

53
inThisBuild(Seq(
@@ -75,7 +73,7 @@ val commonJSSettings = Seq(
7573
)
7674

7775
lazy val root = project.in(file("."))
78-
.enablePlugins(ScalaJSBundlerPlugin)
76+
.enablePlugins(ScalaJSBundlerPlugin, JSDependenciesPlugin)
7977
.settings(
8078
commonSettings,
8179
commonJSSettings,
@@ -87,5 +85,6 @@ lazy val root = project.in(file("."))
8785
),
8886

8987
Compile / npmDependencies += "jquery" -> "3.3.1",
88+
jsDependencies += "org.webjars" % "jquery" % "3.3.1" / "3.3.1/jquery.js",
9089
Test / requireJsDomEnv := true
9190
)

example/build.sbt

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name := "jquery-demo"
22

33
inThisBuild(Seq(
4-
version := "3.0.2",
4+
version := "3.0.0-SNAPSHOT",
55
organization := "io.udash",
66
))
77

88
val commonSettings = Seq(
9-
scalaVersion := "2.12.10",
10-
crossScalaVersions := Seq("2.12.10"), //todo 2.13 & SJS 1.0 with Udash 0.9
9+
scalaVersion := "2.12.11",
10+
crossScalaVersions := Seq("2.13.1"),
1111
scalacOptions ++= Seq(
1212
"-feature",
1313
"-deprecation",
@@ -28,15 +28,54 @@ val commonSettings = Seq(
2828
)
2929

3030
val generatedGlobalDir = file("generated/global")
31+
val compileStatics = taskKey[Unit]("Compiles all static files.")
3132
val copyAssets = taskKey[Unit]("Copies all assets to the target directory.")
32-
val root = project.in(file("."))
33+
34+
lazy val root = project.in(file("."))
3335
.enablePlugins(ScalaJSPlugin)
3436
.settings(commonSettings)
37+
.aggregate(`jquery-bundler-demo`, `jquery-global-demo`)
3538

36-
val generatedBundlerDir = file("generated")
37-
val compileStatics = taskKey[Unit]("Compiles all static files.")
39+
lazy val `jquery-global-demo` = project.in(file("global-demo"))
40+
.enablePlugins(ScalaJSPlugin, JSDependenciesPlugin)
41+
.settings(
42+
commonSettings,
43+
44+
sourceDirsSettings(_.getParentFile),
45+
46+
cleanFiles += generatedGlobalDir,
47+
48+
Compile / fullOptJS / crossTarget := generatedGlobalDir,
49+
Compile / fastOptJS / crossTarget := generatedGlobalDir,
50+
Compile / packageJSDependencies / crossTarget := generatedGlobalDir,
51+
Compile / packageMinifiedJSDependencies / crossTarget := generatedGlobalDir,
52+
53+
Compile / fastOptJS := (Compile / fastOptJS).dependsOn(copyAssets).value,
54+
Compile / fullOptJS := (Compile / fullOptJS).dependsOn(copyAssets).value,
55+
56+
scalaJSUseMainModuleInitializer := true,
3857

39-
val example = project.in(file("."))
58+
copyAssets := {
59+
IO.copyFile(
60+
sourceDirectory.value / "main/assets/index.html",
61+
generatedGlobalDir / "index.html"
62+
)
63+
},
64+
65+
compileStatics := (Compile / fastOptJS).value,
66+
67+
Compile / fastOptJS / artifactPath :=
68+
(Compile / fastOptJS / crossTarget).value / "scripts" / "frontend-impl.js",
69+
Compile / fullOptJS / artifactPath :=
70+
(Compile / fullOptJS / crossTarget).value / "scripts" / "frontend-impl.js",
71+
Compile / packageJSDependencies / artifactPath :=
72+
(Compile / packageJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js",
73+
Compile / packageMinifiedJSDependencies / artifactPath :=
74+
(Compile / packageMinifiedJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js"
75+
)
76+
77+
val generatedBundlerDir = file("generated/bundler")
78+
lazy val `jquery-bundler-demo` = project.in(file("bundler-demo"))
4079
.enablePlugins(ScalaJSBundlerPlugin)
4180
.settings(
4281
commonSettings,
@@ -52,6 +91,8 @@ val example = project.in(file("."))
5291
)
5392
},
5493

94+
cleanFiles += generatedBundlerDir,
95+
5596
compileStatics := {
5697
val sjsFileName = (Compile / fastOptJS).value.data.name.stripSuffix(".js")
5798
IO.copyFile(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>jquery-demo - global scope</title>
6+
</head>
7+
<body>
8+
<div id="application"></div>
9+
10+
<script src="scripts/frontend-deps.js"></script>
11+
<script src="scripts/frontend-impl.js"></script>
12+
</body>
13+
</html>

example/project/Dependencies.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
22
import sbt._
33

44
object Dependencies {
5-
val udashCoreVersion = "0.8.3"
6-
val udashJQueryVersion = "3.0.2"
5+
val scalatagsVersion = "0.8.6"
6+
val udashJQueryVersion = "3.0.0-SNAPSHOT"
77

88
val deps = Def.setting(Seq[ModuleID](
9-
"io.udash" %%% "udash-core" % udashCoreVersion,
9+
"com.lihaoyi" %%% "scalatags" % scalatagsVersion,
1010
"io.udash" %%% "udash-jquery" % udashJQueryVersion
1111
))
1212
}

example/project/plugins.sbt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
logLevel := Level.Warn
22

3-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.32")
4-
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler-sjs06" % "0.17.0")
3+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.1")
4+
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.17.0")
5+
addSbtPlugin("org.scala-js" % "sbt-jsdependencies" % "1.0.0")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.udash.demos.jquery
2+
3+
import io.udash.demos.jquery.views.IndexView
4+
import org.scalajs.dom
5+
6+
import scala.scalajs.js.annotation.JSExport
7+
8+
object Init {
9+
10+
@JSExport
11+
def main(args: Array[String]): Unit =
12+
dom.document.querySelector("#application").appendChild(IndexView.content.render)
13+
}

example/src/main/scala/io/udash/demos/jquery/RoutingRegistryDef.scala

Lines changed: 0 additions & 29 deletions
This file was deleted.

example/src/main/scala/io/udash/demos/jquery/StatesToViewPresenterDef.scala

Lines changed: 0 additions & 27 deletions
This file was deleted.

example/src/main/scala/io/udash/demos/jquery/init.scala

Lines changed: 0 additions & 32 deletions
This file was deleted.

example/src/main/scala/io/udash/demos/jquery/states.scala

Lines changed: 0 additions & 32 deletions
This file was deleted.

example/src/main/scala/io/udash/demos/jquery/views/ErrorView.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package io.udash.demos.jquery.views
22

3-
import io.udash._
43
import org.scalajs.dom.{Element, Event}
54

6-
abstract class FunctionView extends FinalView {
5+
abstract class FunctionView {
6+
77
import scalatags.JsDom.all._
88

99
protected val content: Element
10-
protected val script: () => Any
1110

12-
override def getTemplate: Modifier =
11+
protected def script: () => Any
12+
13+
final def getTemplate: Modifier =
1314
div(
1415
content,
15-
button(
16-
onclick :+= ((_: Event) => {
16+
h3(button(
17+
marginTop := 10.px,
18+
onclick := ((_: Event) => {
1719
script()
1820
false
1921
})
20-
)("Run script")
22+
)("Run script"))
2123
)
2224
}

0 commit comments

Comments
 (0)