Skip to content

Commit 31461ea

Browse files
authored
Merge pull request #20 from UdashFramework/commonjs-module
CommonJS module
2 parents bcdee24 + 290087d commit 31461ea

File tree

15 files changed

+174
-80
lines changed

15 files changed

+174
-80
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dist: trusty
44
jdk: oraclejdk8
55
scala:
66
- 2.11.12
7-
- 2.12.6
7+
- 2.12.7
88

99
before_script:
1010
- "export DISPLAY=:99.0"
@@ -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-global-demo/fullOptJS jquery-bundler-demo/compileStatics

README.md

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

99
```scala
10-
libraryDependencies += "io.udash" %%% "udash-jquery" % "2.0.0"
10+
libraryDependencies += "io.udash" %%% "udash-jquery" % "3.0.0"
1111
```
1212

1313
then import the jQuery package:
@@ -16,8 +16,10 @@ then import the jQuery package:
1616
import io.udash.wrappers.jquery._
1717
```
1818

19-
Since version `2.0.0` the wrapper does not force JS dependency on jQuery. You have to
20-
add it manually by:
19+
Since version `3.0.0` the wrapper is published as a CommonJS module with JS dependencies managed
20+
by [scalajs-bundler](https://github.com/scalacenter/scalajs-bundler).
21+
22+
If you do not want to use the bundler, you have to include jQuery sources manually by:
2123
* explicit link in your `index.html`.
2224
```html
2325
<script src="https://code.jquery.com/jquery-3.3.1.min.js"

build.sbt

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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+
))
9+
10+
val commonSettings = Seq(
11+
scalaVersion := "2.12.7",
12+
crossScalaVersions := Seq("2.11.12", "2.12.7"),
1213
scalacOptions ++= Seq(
1314
"-feature",
1415
"-deprecation",
@@ -20,35 +21,25 @@ inThisBuild(Seq(
2021
"-language:experimental.macros",
2122
"-Xfuture",
2223
"-Xfatal-warnings",
23-
"-Xlint:_",
24+
"-Xlint:_"
2425
),
2526
scalacOptions ++= {
26-
if (CrossVersion.partialVersion((root / scalaVersion).value).contains((2, 12))) Seq(
27+
if (scalaBinaryVersion.value == "2.12") Seq(
2728
"-Ywarn-unused:_,-explicits,-implicits",
2829
"-Ybackend-parallelism", "4",
2930
"-Ycache-plugin-class-loader:last-modified",
3031
"-Ycache-macro-class-loader:last-modified"
3132
) else Seq.empty
32-
},
33-
))
34-
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-
}
33+
}
34+
)
4635

4736
val commonJSSettings = Seq(
4837
Compile / emitSourceMaps := true,
4938
Test / parallelExecution := false,
5039
Test / scalaJSStage := FastOptStage,
51-
Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
40+
// ScalaJSBundlerPlugin does not work with scalajs-env-selenium:
41+
// https://github.com/scalacenter/scalajs-bundler/issues/89
42+
// Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
5243
scalacOptions += {
5344
val localDir = (ThisBuild / baseDirectory).value.toURI.toString
5445
val githubDir = "https://raw.githubusercontent.com/UdashFramework/scala-js-jquery"
@@ -57,15 +48,18 @@ val commonJSSettings = Seq(
5748
scalacOptions += "-P:scalajs:sjsDefinedByDefault",
5849
)
5950

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-
)
51+
lazy val root = project.in(file("."))
52+
.enablePlugins(ScalaJSBundlerPlugin)
53+
.settings(
54+
commonSettings,
55+
commonJSSettings,
6556

66-
jsDependencies +=
67-
"org.webjars" % "jquery" % "3.3.1" % Test / "3.3.1/jquery.js" minified "3.3.1/jquery.min.js"
57+
libraryDependencies ++= Seq(
58+
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
59+
"org.scalatest" %%% "scalatest" % "3.0.5" % Test,
60+
"com.lihaoyi" %%% "scalatags" % "0.6.7" % Test
61+
),
6862

69-
lazy val root = project.in(file("."))
70-
.enablePlugins(ScalaJSPlugin)
71-
.settings(commonJSSettings)
63+
Compile / npmDependencies += "jquery" -> "3.3.1",
64+
Test / requireJsDomEnv := true
65+
)

example/README.md

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

3-
Run `sbt fastOptJS` or `sbt fullOptJS` to compile this demo. You can find all generated files
4-
in the `generated` directory. Open `index.html` in your browser.
7+
Run `sbt jquery-global-demo/fastOptJS` or `sbt jquery-global-demo/fullOptJS` to compile this demo. You can find all generated files
8+
in the `generated/global` directory. Open `index.html` in your browser.
9+
10+
### How to use? (ScalaJSBundlerPlugin)
11+
12+
Run `sbt jquery-bundler-demo/compileStatics` to compile this demo. You can find all generated files
13+
in the `generated/bundler` directory. Open `index.html` in your browser.

example/build.sbt

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
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+
))
7+
8+
val commonSettings = Seq(
9+
scalaVersion := "2.12.7",
10+
crossScalaVersions := Seq("2.11.12", "2.12.7"),
811
scalacOptions ++= Seq(
912
"-feature",
1013
"-deprecation",
@@ -18,27 +21,37 @@ inThisBuild(Seq(
1821
"-Xlint:_",
1922
),
2023
scalacOptions ++= {
21-
if (CrossVersion.partialVersion((`jquery-demo` / scalaVersion).value).contains((2, 12))) Seq(
24+
if (scalaBinaryVersion.value == "2.12") Seq(
2225
"-Ywarn-unused:_,-explicits,-implicits",
2326
"-Ybackend-parallelism", "4",
2427
"-Ycache-plugin-class-loader:last-modified",
2528
"-Ycache-macro-class-loader:last-modified"
2629
) else Seq.empty
2730
},
28-
))
2931

30-
val generatedDir = file("generated")
32+
libraryDependencies ++= Dependencies.deps.value
33+
)
34+
35+
val generatedGlobalDir = file("generated/global")
3136
val copyAssets = taskKey[Unit]("Copies all assets to the target directory.")
32-
val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
37+
val root = project.in(file("."))
38+
.enablePlugins(ScalaJSPlugin)
39+
.settings(commonSettings)
40+
41+
val `jquery-global-demo` = project.in(file("global-demo"))
42+
.enablePlugins(ScalaJSPlugin)
3343
.settings(
34-
libraryDependencies ++= Dependencies.deps.value,
44+
commonSettings,
45+
3546
jsDependencies ++= Dependencies.jsDeps.value,
3647

48+
sourceDirsSettings(_.getParentFile),
49+
3750
/* move these files out of target/. */
38-
Compile / fullOptJS / crossTarget := generatedDir,
39-
Compile / fastOptJS / crossTarget := generatedDir,
40-
Compile / packageJSDependencies / crossTarget := generatedDir,
41-
Compile / packageMinifiedJSDependencies / crossTarget := generatedDir,
51+
Compile / fullOptJS / crossTarget := generatedGlobalDir,
52+
Compile / fastOptJS / crossTarget := generatedGlobalDir,
53+
Compile / packageJSDependencies / crossTarget := generatedGlobalDir,
54+
Compile / packageMinifiedJSDependencies / crossTarget := generatedGlobalDir,
4255

4356
Compile / fastOptJS := (Compile / fastOptJS).dependsOn(copyAssets).value,
4457
Compile / fullOptJS := (Compile / fullOptJS).dependsOn(copyAssets).value,
@@ -48,7 +61,7 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
4861
copyAssets := {
4962
IO.copyFile(
5063
sourceDirectory.value / "main/assets/index.html",
51-
generatedDir / "index.html"
64+
generatedGlobalDir / "index.html"
5265
)
5366
},
5467

@@ -60,4 +73,57 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
6073
(Compile / packageJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js",
6174
Compile / packageMinifiedJSDependencies / artifactPath :=
6275
(Compile / packageMinifiedJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js"
63-
)
76+
)
77+
78+
val generatedBundlerDir = file("generated/bundler")
79+
val compileStatics = taskKey[Unit]("Compiles all static files.")
80+
val `jquery-bundler-demo` = project.in(file("bundler-demo"))
81+
.enablePlugins(ScalaJSBundlerPlugin)
82+
.settings(
83+
commonSettings,
84+
85+
sourceDirsSettings(_.getParentFile),
86+
87+
Compile / scalaJSUseMainModuleInitializer := true,
88+
89+
copyAssets := {
90+
IO.copyFile(
91+
sourceDirectory.value / "main/assets/index.html",
92+
generatedBundlerDir / "index.html"
93+
)
94+
},
95+
96+
compileStatics := {
97+
val sjsFileName = (Compile / fastOptJS).value.data.name.stripSuffix(".js")
98+
IO.copyFile(
99+
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js",
100+
generatedBundlerDir / "scripts/frontend.js"
101+
)
102+
IO.copyFile(
103+
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js.map",
104+
generatedBundlerDir / "scripts/frontend.js.map"
105+
)
106+
},
107+
compileStatics := compileStatics.dependsOn(Compile / fastOptJS / webpack, copyAssets).value,
108+
)
109+
110+
def mkSourceDirs(base: File, scalaBinary: String, conf: String): Seq[File] = Seq(
111+
base / "src" / conf / "scala",
112+
base / "src" / conf / s"scala-$scalaBinary",
113+
base / "src" / conf / "java"
114+
)
115+
116+
def mkResourceDirs(base: File, conf: String): Seq[File] = Seq(
117+
base / "src" / conf / "resources"
118+
)
119+
120+
def sourceDirsSettings(baseMapper: File => File) = Seq(
121+
Compile / unmanagedSourceDirectories ++=
122+
mkSourceDirs(baseMapper(baseDirectory.value), scalaBinaryVersion.value, "main"),
123+
Compile / unmanagedResourceDirectories ++=
124+
mkResourceDirs(baseMapper(baseDirectory.value), "main"),
125+
Test / unmanagedSourceDirectories ++=
126+
mkSourceDirs(baseMapper(baseDirectory.value), scalaBinaryVersion.value, "test"),
127+
Test / unmanagedResourceDirectories ++=
128+
mkResourceDirs(baseMapper(baseDirectory.value), "test"),
129+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>jquery-demo - ScalaJSBundlerPlugin demo</title>
6+
</head>
7+
<body>
8+
<div id="application"></div>
9+
10+
<script src="scripts/frontend.js"></script>
11+
</body>
12+
</html>
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: 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.14.0")

example/src/main/assets/index.html

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

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.14.0")

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+
}

0 commit comments

Comments
 (0)