Skip to content

Commit b07abaa

Browse files
author
Mateusz Starzec
committed
Publish jQuery wrapper as CommonJS module with global scope fallback
1 parent bcdee24 commit b07abaa

File tree

13 files changed

+102
-52
lines changed

13 files changed

+102
-52
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ before_script:
2424
script:
2525
- sbt ++$TRAVIS_SCALA_VERSION test
2626
- sbt ++$TRAVIS_SCALA_VERSION publishLocal
27-
- cd example && sbt ++$TRAVIS_SCALA_VERSION compile fullOptJS
27+
- cd example && sbt ++$TRAVIS_SCALA_VERSION compile jquery-demo/fullOptJS jquery-bundler-demo/compileStatics

build.sbt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import org.openqa.selenium.chrome.ChromeOptions
2-
import org.openqa.selenium.remote.DesiredCapabilities
3-
import org.scalajs.jsenv.selenium.SeleniumJSEnv
42

53
name := "udash-jquery"
64

75
inThisBuild(Seq(
8-
version := "2.0.1",
6+
version := "3.0.0-SNAPSHOT",
97
organization := "io.udash",
10-
scalaVersion := "2.12.6",
11-
crossScalaVersions := Seq("2.11.12", "2.12.6"),
8+
scalaVersion := "2.12.7",
9+
crossScalaVersions := Seq("2.11.12", "2.12.7"),
1210
scalacOptions ++= Seq(
1311
"-feature",
1412
"-deprecation",
@@ -20,35 +18,25 @@ inThisBuild(Seq(
2018
"-language:experimental.macros",
2119
"-Xfuture",
2220
"-Xfatal-warnings",
23-
"-Xlint:_",
21+
"-Xlint:_"
2422
),
2523
scalacOptions ++= {
26-
if (CrossVersion.partialVersion((root / scalaVersion).value).contains((2, 12))) Seq(
24+
if (scalaBinaryVersion.value == "2.12") Seq(
2725
"-Ywarn-unused:_,-explicits,-implicits",
2826
"-Ybackend-parallelism", "4",
2927
"-Ycache-plugin-class-loader:last-modified",
3028
"-Ycache-macro-class-loader:last-modified"
3129
) else Seq.empty
32-
},
30+
}
3331
))
3432

35-
// Settings for JS tests run in browser
36-
val browserCapabilities: DesiredCapabilities = {
37-
// requires ChromeDriver: https://sites.google.com/a/chromium.org/chromedriver/
38-
val capabilities = DesiredCapabilities.chrome()
39-
capabilities.setCapability(ChromeOptions.CAPABILITY, {
40-
val options = new ChromeOptions()
41-
options.addArguments("--headless", "--disable-gpu")
42-
options
43-
})
44-
capabilities
45-
}
46-
4733
val commonJSSettings = Seq(
4834
Compile / emitSourceMaps := true,
4935
Test / parallelExecution := false,
5036
Test / scalaJSStage := FastOptStage,
51-
Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
37+
// ScalaJSBundlerPlugin does not work with scalajs-env-selenium:
38+
// https://github.com/scalacenter/scalajs-bundler/issues/89
39+
// Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
5240
scalacOptions += {
5341
val localDir = (ThisBuild / baseDirectory).value.toURI.toString
5442
val githubDir = "https://raw.githubusercontent.com/UdashFramework/scala-js-jquery"
@@ -57,15 +45,17 @@ val commonJSSettings = Seq(
5745
scalacOptions += "-P:scalajs:sjsDefinedByDefault",
5846
)
5947

60-
libraryDependencies ++= Seq(
61-
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
62-
"org.scalatest" %%% "scalatest" % "3.0.5" % Test,
63-
"com.lihaoyi" %%% "scalatags" % "0.6.7" % Test
64-
)
48+
lazy val root = project.in(file("."))
49+
.enablePlugins(ScalaJSBundlerPlugin)
50+
.settings(
51+
commonJSSettings,
6552

66-
jsDependencies +=
67-
"org.webjars" % "jquery" % "3.3.1" % Test / "3.3.1/jquery.js" minified "3.3.1/jquery.min.js"
53+
libraryDependencies ++= Seq(
54+
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
55+
"org.scalatest" %%% "scalatest" % "3.0.5" % Test,
56+
"com.lihaoyi" %%% "scalatags" % "0.6.7" % Test
57+
),
6858

69-
lazy val root = project.in(file("."))
70-
.enablePlugins(ScalaJSPlugin)
71-
.settings(commonJSSettings)
59+
Compile / npmDependencies += "jquery" -> "3.3.1",
60+
Test / requiresDOM := true
61+
)

example/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
The `build.sbt` file contains two configurations:
2+
* `jquery-demo` uses global jQuery dependency.
3+
* `jquery-bundler-demo` uses `ScalaJSBundlerPlugin` in order to manage module dependencies.
4+
15
### How to use?
26

37
Run `sbt fastOptJS` or `sbt fullOptJS` to compile this demo. You can find all generated files
48
in the `generated` directory. Open `index.html` in your browser.
9+
10+
### How to use? (ScalaJSBundlerPlugin)
11+
12+
Run `sbt compileStatics` to compile this demo. You can find all generated files
13+
in the `generated` directory. Open `index.html` in your browser.

example/build.sbt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name := "jquery-demo"
22

33
inThisBuild(Seq(
4-
version := "2.0.0",
4+
version := "3.0.0",
55
organization := "io.udash",
6-
scalaVersion := "2.12.6",
7-
crossScalaVersions := Seq("2.11.12", "2.12.6"),
6+
scalaVersion := "2.12.7",
7+
crossScalaVersions := Seq("2.11.12", "2.12.7"),
88
scalacOptions ++= Seq(
99
"-feature",
1010
"-deprecation",
@@ -18,7 +18,7 @@ inThisBuild(Seq(
1818
"-Xlint:_",
1919
),
2020
scalacOptions ++= {
21-
if (CrossVersion.partialVersion((`jquery-demo` / scalaVersion).value).contains((2, 12))) Seq(
21+
if (scalaBinaryVersion.value == "2.12") Seq(
2222
"-Ywarn-unused:_,-explicits,-implicits",
2323
"-Ybackend-parallelism", "4",
2424
"-Ycache-plugin-class-loader:last-modified",
@@ -28,6 +28,7 @@ inThisBuild(Seq(
2828
))
2929

3030
val generatedDir = file("generated")
31+
val compileStatics = taskKey[Unit]("Compiles all static files.")
3132
val copyAssets = taskKey[Unit]("Copies all assets to the target directory.")
3233
val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
3334
.settings(
@@ -47,7 +48,7 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
4748

4849
copyAssets := {
4950
IO.copyFile(
50-
sourceDirectory.value / "main/assets/index.html",
51+
sourceDirectory.value / "main/assets/index-global.html",
5152
generatedDir / "index.html"
5253
)
5354
},
@@ -60,4 +61,32 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
6061
(Compile / packageJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js",
6162
Compile / packageMinifiedJSDependencies / artifactPath :=
6263
(Compile / packageMinifiedJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js"
64+
)
65+
66+
val `jquery-bundler-demo` = project.in(file("."))
67+
.enablePlugins(ScalaJSBundlerPlugin)
68+
.settings(
69+
libraryDependencies ++= Dependencies.deps.value,
70+
71+
Compile / scalaJSUseMainModuleInitializer := true,
72+
73+
copyAssets := {
74+
IO.copyFile(
75+
sourceDirectory.value / "main/assets/index-bundler.html",
76+
generatedDir / "index.html"
77+
)
78+
},
79+
80+
compileStatics := {
81+
val sjsFileName = (Compile / fastOptJS).value.data.name.stripSuffix(".js")
82+
IO.copyFile(
83+
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js",
84+
generatedDir / "scripts/frontend.js"
85+
)
86+
IO.copyFile(
87+
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js.map",
88+
generatedDir / "scripts/frontend.js.map"
89+
)
90+
},
91+
compileStatics := compileStatics.dependsOn(Compile / fastOptJS / webpack, copyAssets).value,
6392
)

example/project/Dependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sbt._
44

55
object Dependencies {
66
val udashCoreVersion = "0.6.1"
7-
val udashJQueryVersion = "2.0.0"
7+
val udashJQueryVersion = "3.0.0-SNAPSHOT"
88

99
val deps = Def.setting(Seq[ModuleID](
1010
"io.udash" %%% "udash-core-frontend" % udashCoreVersion,

example/project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.2.0
1+
sbt.version = 1.2.6

example/project/plugins.sbt

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

3-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.24")
3+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
4+
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.1")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>jquery-demo - ScalaJSBundlerPlugin demo</title>
6+
<script src="scripts/frontend.js"></script>
7+
</head>
8+
<body>
9+
<div id="application"></div>
10+
</body>
11+
</html>

example/src/main/assets/index.html renamed to example/src/main/assets/index-global.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head lang="en">
44
<meta charset="UTF-8">
5-
<title>jquery-demo</title>
5+
<title>jquery-demo - global scope</title>
66

77
<script src="scripts/frontend-deps.js"></script>
88
<script src="scripts/frontend-impl.js"></script>

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version = 1.2.0
1+
sbt.version = 1.2.6

project/plugins.sbt

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

3-
libraryDependencies += "org.scala-js" %% "scalajs-env-selenium" % "0.2.0"
3+
libraryDependencies += "org.scala-js" %% "scalajs-env-selenium" % "0.3.0"
44

5-
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.24")
5+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
6+
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.1")

src/main/scala/io/udash/wrappers/jquery/package.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.udash.wrappers
33
import org.scalajs.dom._
44

55
import scala.scalajs.js
6+
import scala.scalajs.js.annotation.JSImport
67

78
/** All docs are quoted from <a href="http://api.jquery.com/">jQuery API docs</a>. */
89
package object jquery {
@@ -12,5 +13,7 @@ package object jquery {
1213
type EventName = String
1314
type JQueryCallback = (Element, JQueryEvent) => Any
1415

15-
def jQ: JQueryStatic = js.Dynamic.global.jQuery.asInstanceOf[JQueryStatic]
16-
}
16+
@js.native
17+
@JSImport("jquery", JSImport.Default, "$")
18+
object jQ extends JQueryStatic
19+
}

src/main/scala/io/udash/wrappers/jquery/utils.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ sealed trait TopLeftCoords {
1212
case class Position(override val top: Double, override val left: Double) extends TopLeftCoords
1313
case class Offset(override val top: Double, override val left: Double) extends TopLeftCoords
1414

15-
case class AnimationOptions(duration: Option[Int] = None, easing: Option[EasingFunction] = None, queue: Option[Boolean] = None,
16-
step: (Int, js.Dynamic) => Any = null, progress: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Int, Int) => Any = null,
17-
complete: () => Any = null, start: JQueryPromise[js.Function1[js.Any, js.Any], js.Any] => Any = null,
18-
done: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null,
19-
fail: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null,
20-
always: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null) {
15+
case class AnimationOptions(
16+
duration: Option[Int] = None,
17+
easing: Option[EasingFunction] = None,
18+
queue: Option[Boolean] = None,
19+
step: (Int, js.Dynamic) => Any = null,
20+
progress: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Int, Int) => Any = null,
21+
complete: () => Any = null,
22+
start: JQueryPromise[js.Function1[js.Any, js.Any], js.Any] => Any = null,
23+
done: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null,
24+
fail: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null,
25+
always: (JQueryPromise[js.Function1[js.Any, js.Any], js.Any], Boolean) => Any = null
26+
) {
2127
def toJSDictionary: js.Dictionary[Any] = {
2228
val r = js.Dictionary[Any]()
2329
if (duration.isDefined) r.update("duration", duration.get)

0 commit comments

Comments
 (0)