Skip to content

CommonJS module #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist: trusty
jdk: oraclejdk8
scala:
- 2.11.12
- 2.12.6
- 2.12.7

before_script:
- "export DISPLAY=:99.0"
Expand All @@ -24,4 +24,4 @@ before_script:
script:
- sbt ++$TRAVIS_SCALA_VERSION test
- sbt ++$TRAVIS_SCALA_VERSION publishLocal
- cd example && sbt ++$TRAVIS_SCALA_VERSION compile fullOptJS
- cd example && sbt ++$TRAVIS_SCALA_VERSION compile jquery-global-demo/fullOptJS jquery-bundler-demo/compileStatics
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Static types for the jQuery API for [Scala.js](http://www.scala-js.org/) program
Add the following dependency to your SBT build:

```scala
libraryDependencies += "io.udash" %%% "udash-jquery" % "2.0.0"
libraryDependencies += "io.udash" %%% "udash-jquery" % "3.0.0"
```

then import the jQuery package:
Expand All @@ -16,8 +16,10 @@ then import the jQuery package:
import io.udash.wrappers.jquery._
```

Since version `2.0.0` the wrapper does not force JS dependency on jQuery. You have to
add it manually by:
Since version `3.0.0` the wrapper is published as a CommonJS module with JS dependencies managed
by [scalajs-bundler](https://github.com/scalacenter/scalajs-bundler).

If you do not want to use the bundler, you have to include jQuery sources manually by:
* explicit link in your `index.html`.
```html
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
Expand Down
58 changes: 26 additions & 32 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.remote.DesiredCapabilities
import org.scalajs.jsenv.selenium.SeleniumJSEnv

name := "udash-jquery"

inThisBuild(Seq(
version := "2.0.1",
version := "3.0.0-SNAPSHOT",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll change it before releasing, right? ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right :)

organization := "io.udash",
scalaVersion := "2.12.6",
crossScalaVersions := Seq("2.11.12", "2.12.6"),
))

val commonSettings = Seq(
scalaVersion := "2.12.7",
crossScalaVersions := Seq("2.11.12", "2.12.7"),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
Expand All @@ -20,35 +21,25 @@ inThisBuild(Seq(
"-language:experimental.macros",
"-Xfuture",
"-Xfatal-warnings",
"-Xlint:_",
"-Xlint:_"
),
scalacOptions ++= {
if (CrossVersion.partialVersion((root / scalaVersion).value).contains((2, 12))) Seq(
if (scalaBinaryVersion.value == "2.12") Seq(
"-Ywarn-unused:_,-explicits,-implicits",
"-Ybackend-parallelism", "4",
"-Ycache-plugin-class-loader:last-modified",
"-Ycache-macro-class-loader:last-modified"
) else Seq.empty
},
))

// Settings for JS tests run in browser
val browserCapabilities: DesiredCapabilities = {
// requires ChromeDriver: https://sites.google.com/a/chromium.org/chromedriver/
val capabilities = DesiredCapabilities.chrome()
capabilities.setCapability(ChromeOptions.CAPABILITY, {
val options = new ChromeOptions()
options.addArguments("--headless", "--disable-gpu")
options
})
capabilities
}
}
)

val commonJSSettings = Seq(
Compile / emitSourceMaps := true,
Test / parallelExecution := false,
Test / scalaJSStage := FastOptStage,
Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
// ScalaJSBundlerPlugin does not work with scalajs-env-selenium:
// https://github.com/scalacenter/scalajs-bundler/issues/89
// Test / jsEnv := new SeleniumJSEnv(browserCapabilities),
scalacOptions += {
val localDir = (ThisBuild / baseDirectory).value.toURI.toString
val githubDir = "https://raw.githubusercontent.com/UdashFramework/scala-js-jquery"
Expand All @@ -57,15 +48,18 @@ val commonJSSettings = Seq(
scalacOptions += "-P:scalajs:sjsDefinedByDefault",
)

libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
"org.scalatest" %%% "scalatest" % "3.0.5" % Test,
"com.lihaoyi" %%% "scalatags" % "0.6.7" % Test
)
lazy val root = project.in(file("."))
.enablePlugins(ScalaJSBundlerPlugin)
.settings(
commonSettings,
commonJSSettings,

jsDependencies +=
"org.webjars" % "jquery" % "3.3.1" % Test / "3.3.1/jquery.js" minified "3.3.1/jquery.min.js"
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.9.6",
"org.scalatest" %%% "scalatest" % "3.0.5" % Test,
"com.lihaoyi" %%% "scalatags" % "0.6.7" % Test
),

lazy val root = project.in(file("."))
.enablePlugins(ScalaJSPlugin)
.settings(commonJSSettings)
Compile / npmDependencies += "jquery" -> "3.3.1",
Test / requireJsDomEnv := true
)
13 changes: 11 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
The `build.sbt` file contains two configurations:
* `jquery-global-demo` uses global jQuery dependency.
* `jquery-bundler-demo` uses `ScalaJSBundlerPlugin` in order to manage module dependencies.

### How to use?

Run `sbt fastOptJS` or `sbt fullOptJS` to compile this demo. You can find all generated files
in the `generated` directory. Open `index.html` in your browser.
Run `sbt jquery-global-demo/fastOptJS` or `sbt jquery-global-demo/fullOptJS` to compile this demo. You can find all generated files
in the `generated/global` directory. Open `index.html` in your browser.

### How to use? (ScalaJSBundlerPlugin)

Run `sbt jquery-bundler-demo/compileStatics` to compile this demo. You can find all generated files
in the `generated/bundler` directory. Open `index.html` in your browser.
94 changes: 80 additions & 14 deletions example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
name := "jquery-demo"

inThisBuild(Seq(
version := "2.0.0",
version := "3.0.0",
organization := "io.udash",
scalaVersion := "2.12.6",
crossScalaVersions := Seq("2.11.12", "2.12.6"),
))

val commonSettings = Seq(
scalaVersion := "2.12.7",
crossScalaVersions := Seq("2.11.12", "2.12.7"),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
Expand All @@ -18,27 +21,37 @@ inThisBuild(Seq(
"-Xlint:_",
),
scalacOptions ++= {
if (CrossVersion.partialVersion((`jquery-demo` / scalaVersion).value).contains((2, 12))) Seq(
if (scalaBinaryVersion.value == "2.12") Seq(
"-Ywarn-unused:_,-explicits,-implicits",
"-Ybackend-parallelism", "4",
"-Ycache-plugin-class-loader:last-modified",
"-Ycache-macro-class-loader:last-modified"
) else Seq.empty
},
))

val generatedDir = file("generated")
libraryDependencies ++= Dependencies.deps.value
)

val generatedGlobalDir = file("generated/global")
val copyAssets = taskKey[Unit]("Copies all assets to the target directory.")
val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
val root = project.in(file("."))
.enablePlugins(ScalaJSPlugin)
.settings(commonSettings)

val `jquery-global-demo` = project.in(file("global-demo"))
.enablePlugins(ScalaJSPlugin)
.settings(
libraryDependencies ++= Dependencies.deps.value,
commonSettings,

jsDependencies ++= Dependencies.jsDeps.value,

sourceDirsSettings(_.getParentFile),

/* move these files out of target/. */
Compile / fullOptJS / crossTarget := generatedDir,
Compile / fastOptJS / crossTarget := generatedDir,
Compile / packageJSDependencies / crossTarget := generatedDir,
Compile / packageMinifiedJSDependencies / crossTarget := generatedDir,
Compile / fullOptJS / crossTarget := generatedGlobalDir,
Compile / fastOptJS / crossTarget := generatedGlobalDir,
Compile / packageJSDependencies / crossTarget := generatedGlobalDir,
Compile / packageMinifiedJSDependencies / crossTarget := generatedGlobalDir,

Compile / fastOptJS := (Compile / fastOptJS).dependsOn(copyAssets).value,
Compile / fullOptJS := (Compile / fullOptJS).dependsOn(copyAssets).value,
Expand All @@ -48,7 +61,7 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
copyAssets := {
IO.copyFile(
sourceDirectory.value / "main/assets/index.html",
generatedDir / "index.html"
generatedGlobalDir / "index.html"
)
},

Expand All @@ -60,4 +73,57 @@ val `jquery-demo` = project.in(file(".")).enablePlugins(ScalaJSPlugin)
(Compile / packageJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js",
Compile / packageMinifiedJSDependencies / artifactPath :=
(Compile / packageMinifiedJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js"
)
)

val generatedBundlerDir = file("generated/bundler")
val compileStatics = taskKey[Unit]("Compiles all static files.")
val `jquery-bundler-demo` = project.in(file("bundler-demo"))
.enablePlugins(ScalaJSBundlerPlugin)
.settings(
commonSettings,

sourceDirsSettings(_.getParentFile),

Compile / scalaJSUseMainModuleInitializer := true,

copyAssets := {
IO.copyFile(
sourceDirectory.value / "main/assets/index.html",
generatedBundlerDir / "index.html"
)
},

compileStatics := {
val sjsFileName = (Compile / fastOptJS).value.data.name.stripSuffix(".js")
IO.copyFile(
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js",
generatedBundlerDir / "scripts/frontend.js"
)
IO.copyFile(
(Compile / npmUpdate / crossTarget).value / s"$sjsFileName-bundle.js.map",
generatedBundlerDir / "scripts/frontend.js.map"
)
},
compileStatics := compileStatics.dependsOn(Compile / fastOptJS / webpack, copyAssets).value,
)

def mkSourceDirs(base: File, scalaBinary: String, conf: String): Seq[File] = Seq(
base / "src" / conf / "scala",
base / "src" / conf / s"scala-$scalaBinary",
base / "src" / conf / "java"
)

def mkResourceDirs(base: File, conf: String): Seq[File] = Seq(
base / "src" / conf / "resources"
)

def sourceDirsSettings(baseMapper: File => File) = Seq(
Compile / unmanagedSourceDirectories ++=
mkSourceDirs(baseMapper(baseDirectory.value), scalaBinaryVersion.value, "main"),
Compile / unmanagedResourceDirectories ++=
mkResourceDirs(baseMapper(baseDirectory.value), "main"),
Test / unmanagedSourceDirectories ++=
mkSourceDirs(baseMapper(baseDirectory.value), scalaBinaryVersion.value, "test"),
Test / unmanagedResourceDirectories ++=
mkResourceDirs(baseMapper(baseDirectory.value), "test"),
)
12 changes: 12 additions & 0 deletions example/bundler-demo/src/main/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>jquery-demo - ScalaJSBundlerPlugin demo</title>
</head>
<body>
<div id="application"></div>

<script src="scripts/frontend.js"></script>
</body>
</html>
13 changes: 13 additions & 0 deletions example/global-demo/src/main/assets/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>jquery-demo - global scope</title>
</head>
<body>
<div id="application"></div>

<script src="scripts/frontend-deps.js"></script>
<script src="scripts/frontend-impl.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion example/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sbt._

object Dependencies {
val udashCoreVersion = "0.6.1"
val udashJQueryVersion = "2.0.0"
val udashJQueryVersion = "3.0.0-SNAPSHOT"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


val deps = Def.setting(Seq[ModuleID](
"io.udash" %%% "udash-core-frontend" % udashCoreVersion,
Expand Down
2 changes: 1 addition & 1 deletion example/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.2.0
sbt.version = 1.2.6
3 changes: 2 additions & 1 deletion example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
logLevel := Level.Warn

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.24")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.14.0")
13 changes: 0 additions & 13 deletions example/src/main/assets/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.2.0
sbt.version = 1.2.6
5 changes: 3 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
logLevel := Level.Warn

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

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.24")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.14.0")
7 changes: 5 additions & 2 deletions src/main/scala/io/udash/wrappers/jquery/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.udash.wrappers
import org.scalajs.dom._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

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

def jQ: JQueryStatic = js.Dynamic.global.jQuery.asInstanceOf[JQueryStatic]
}
@js.native
@JSImport("jquery", JSImport.Default, "$")
object jQ extends JQueryStatic
}
Loading