Skip to content

Commit

Permalink
Fixes #16: add optional htmlAttributes parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmunier committed Jan 26, 2017
1 parent 32e58b5 commit 56eebd6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ Usage for the Play Framework with [fingerprinting for public assets](https://www
name => getClass.getResource(s"/public/$name") != null)
```

Additional HTML attributes can be added to the generated `<script>` tags:
```
@scalajs.html.scripts(
projectName = "client",
name => s"/assets/$name",
name => getClass.getResource(s"/public/$name") != null,
Html("""charset="UTF-8" defer""")
)
```

Note:
The Twirl templates require a `resourceExists` function because the application classpath may not be accessible from the library.
Play for instance, puts libraries in a parent classpath of the application, meaning they can't see the classes provided by the application.
Expand Down
4 changes: 2 additions & 2 deletions src/main/twirl/scalajs/jsScript.scala.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@(src: String)
@(src: String, htmlAttributes: Html = Html(""))

<script src="@src" type="text/javascript"></script>
<script src="@src" type="text/javascript" @htmlAttributes></script>
5 changes: 3 additions & 2 deletions src/main/twirl/scalajs/jsdeps.scala.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@(projectName: String,
assets: String => String,
resourceExists: String => Boolean
resourceExists: String => Boolean,
htmlAttributes: Html = Html("")
)

@defining(s"${projectName.toLowerCase}-jsdeps") { jsdeps =>
@Seq(s"${jsdeps}.min.js", s"${jsdeps}.js").find(resourceExists).map(name => jsScript(assets(name)))
@Seq(s"${jsdeps}.min.js", s"${jsdeps}.js").find(resourceExists).map(name => jsScript(assets(name), htmlAttributes))
}
5 changes: 3 additions & 2 deletions src/main/twirl/scalajs/launcher.scala.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@(projectName: String,
assets: String => String,
resourceExists: String => Boolean
resourceExists: String => Boolean,
htmlAttributes: Html = Html("")
)

@defining(s"${projectName.toLowerCase}-launcher.js") { launcher =>
@if(resourceExists(launcher)) {
@jsScript(assets(launcher))
@jsScript(assets(launcher), htmlAttributes)
}
}
9 changes: 5 additions & 4 deletions src/main/twirl/scalajs/scripts.scala.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@(projectName: String,
assets: String => String,
resourceExists: String => Boolean
resourceExists: String => Boolean,
htmlAttributes: Html = Html("")
)

@jsdeps(projectName, assets, resourceExists)
@selectScript(projectName, assets, resourceExists)
@launcher(projectName, assets, resourceExists)
@jsdeps(projectName, assets, resourceExists, htmlAttributes)
@selectScript(projectName, assets, resourceExists, htmlAttributes)
@launcher(projectName, assets, resourceExists, htmlAttributes)
5 changes: 3 additions & 2 deletions src/main/twirl/scalajs/selectScript.scala.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@(projectName: String,
assets: String => String,
resourceExists: String => Boolean
resourceExists: String => Boolean,
htmlAttributes: Html = Html("")
)

@defining(s"${projectName.toLowerCase}") { name =>
@Seq(s"$name-opt.js", s"$name-fastopt.js").find(resourceExists).map(name => jsScript(assets(name)))
@Seq(s"$name-opt.js", s"$name-fastopt.js").find(resourceExists).map(name => jsScript(assets(name), htmlAttributes))
}

0 comments on commit 56eebd6

Please sign in to comment.