Skip to content

Support both bundler and JS deps #27

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 12 commits into from
Apr 1, 2020
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- stage: test
script:
- sbt +test +publishLocal
- cd example && sbt compileStatics
- cd example && sbt +compileStatics
- stage: release
if: tag =~ ^v
script:
Expand Down
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,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" % "3.0.2"
libraryDependencies += "io.udash" %%% "udash-jquery" % "3.0.4"
```

then import the jQuery package:
Expand All @@ -19,21 +19,9 @@ then import the jQuery package:
import io.udash.wrappers.jquery._
```

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"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
```
* or a [Scala.js dependency](http://www.scala-js.org/doc/project/dependencies.html).
```scala
jsDependencies +=
"org.webjars" % "jquery" % "3.3.1" / "3.3.1/jquery.js" minified "3.3.1/jquery.min.js"
```
Since version `3.0.4` the wrapper targets SJS 1.x series and supports JS dependencies managed by
by [scalajs-bundler](https://github.com/scalacenter/scalajs-bundler) or [sbt-jsdependencies
](https://github.com/scala-js/jsdependencies)


## Examples
Expand Down
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


name := "udash-jquery"

inThisBuild(Seq(
Expand Down Expand Up @@ -75,7 +73,7 @@ val commonJSSettings = Seq(
)

lazy val root = project.in(file("."))
.enablePlugins(ScalaJSBundlerPlugin)
.enablePlugins(ScalaJSBundlerPlugin, JSDependenciesPlugin)
.settings(
commonSettings,
commonJSSettings,
Expand All @@ -87,5 +85,6 @@ lazy val root = project.in(file("."))
),

Compile / npmDependencies += "jquery" -> "3.3.1",
jsDependencies += "org.webjars" % "jquery" % "3.3.1" / "3.3.1/jquery.js",
Test / requireJsDomEnv := true
)
55 changes: 48 additions & 7 deletions example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name := "jquery-demo"

inThisBuild(Seq(
version := "3.0.2",
version := "3.0.0-SNAPSHOT",
organization := "io.udash",
))

val commonSettings = Seq(
scalaVersion := "2.12.10",
crossScalaVersions := Seq("2.12.10"), //todo 2.13 & SJS 1.0 with Udash 0.9
scalaVersion := "2.12.11",
crossScalaVersions := Seq("2.13.1"),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
Expand All @@ -28,15 +28,54 @@ val commonSettings = Seq(
)

val generatedGlobalDir = file("generated/global")
val compileStatics = taskKey[Unit]("Compiles all static files.")
val copyAssets = taskKey[Unit]("Copies all assets to the target directory.")
val root = project.in(file("."))

lazy val root = project.in(file("."))
.enablePlugins(ScalaJSPlugin)
.settings(commonSettings)
.aggregate(`jquery-bundler-demo`, `jquery-global-demo`)

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

sourceDirsSettings(_.getParentFile),

cleanFiles += generatedGlobalDir,

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,

scalaJSUseMainModuleInitializer := true,

val example = project.in(file("."))
copyAssets := {
IO.copyFile(
sourceDirectory.value / "main/assets/index.html",
generatedGlobalDir / "index.html"
)
},

compileStatics := (Compile / fastOptJS).value,

Compile / fastOptJS / artifactPath :=
(Compile / fastOptJS / crossTarget).value / "scripts" / "frontend-impl.js",
Compile / fullOptJS / artifactPath :=
(Compile / fullOptJS / crossTarget).value / "scripts" / "frontend-impl.js",
Compile / packageJSDependencies / artifactPath :=
(Compile / packageJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js",
Compile / packageMinifiedJSDependencies / artifactPath :=
(Compile / packageMinifiedJSDependencies / crossTarget).value / "scripts" / "frontend-deps.js"
)

val generatedBundlerDir = file("generated/bundler")
lazy val `jquery-bundler-demo` = project.in(file("bundler-demo"))
.enablePlugins(ScalaJSBundlerPlugin)
.settings(
commonSettings,
Expand All @@ -52,6 +91,8 @@ val example = project.in(file("."))
)
},

cleanFiles += generatedBundlerDir,

compileStatics := {
val sjsFileName = (Compile / fastOptJS).value.data.name.stripSuffix(".js")
IO.copyFile(
Expand Down
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>
6 changes: 3 additions & 3 deletions example/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
import sbt._

object Dependencies {
val udashCoreVersion = "0.8.3"
val udashJQueryVersion = "3.0.2"
val scalatagsVersion = "0.8.6"
val udashJQueryVersion = "3.0.0-SNAPSHOT"

val deps = Def.setting(Seq[ModuleID](
"io.udash" %%% "udash-core" % udashCoreVersion,
"com.lihaoyi" %%% "scalatags" % scalatagsVersion,
"io.udash" %%% "udash-jquery" % udashJQueryVersion
))
}
5 changes: 3 additions & 2 deletions example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
logLevel := Level.Warn

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.32")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler-sjs06" % "0.17.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.17.0")
addSbtPlugin("org.scala-js" % "sbt-jsdependencies" % "1.0.0")
13 changes: 13 additions & 0 deletions example/src/main/scala/io/udash/demos/jquery/Init.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.udash.demos.jquery

import io.udash.demos.jquery.views.IndexView
import org.scalajs.dom

import scala.scalajs.js.annotation.JSExport

object Init {

@JSExport
def main(args: Array[String]): Unit =
dom.document.querySelector("#application").appendChild(IndexView.content.render)
}

This file was deleted.

This file was deleted.

32 changes: 0 additions & 32 deletions example/src/main/scala/io/udash/demos/jquery/init.scala

This file was deleted.

32 changes: 0 additions & 32 deletions example/src/main/scala/io/udash/demos/jquery/states.scala

This file was deleted.

13 changes: 0 additions & 13 deletions example/src/main/scala/io/udash/demos/jquery/views/ErrorView.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package io.udash.demos.jquery.views

import io.udash._
import org.scalajs.dom.{Element, Event}

abstract class FunctionView extends FinalView {
abstract class FunctionView {

import scalatags.JsDom.all._

protected val content: Element
protected val script: () => Any

override def getTemplate: Modifier =
protected def script: () => Any

final def getTemplate: Modifier =
div(
content,
button(
onclick :+= ((_: Event) => {
h3(button(
marginTop := 10.px,
onclick := ((_: Event) => {
script()
false
})
)("Run script")
)("Run script"))
)
}
Loading