Skip to content

Commit b5739c9

Browse files
use V8 for a babel transform helper
1 parent a5dfd7b commit b5739c9

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ Imports:
2424
Suggests:
2525
htmlwidgets (>= 0.6.0),
2626
rmarkdown,
27-
shiny
27+
shiny,
28+
V8
2829
RoxygenNote: 5.0.1

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(babel_transform)
34
export(html_dependency_react)
45
importFrom(htmltools,htmlDependency)

R/babel.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Transform Code with Babel
2+
#'
3+
#' Helper function to use \code{V8} with \code{Babel} so we can
4+
#' avoid a JSX transformer with using \code{reactR}.
5+
#'
6+
#' @param code \code{character}
7+
#'
8+
#' @return transformed \code{character}
9+
#' @export
10+
#'
11+
#' @examples
12+
#' library(reactR)
13+
#' babel_transform('<div>react div</div>')
14+
babel_transform <- function(code=""){
15+
stopifnot(require(V8), is.character(code))
16+
17+
ctx <- v8()
18+
ctx$source('https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.10.3/babel.min.js')
19+
ctx$assign('code', code)
20+
ctx$get('Babel.transform(code,{ presets: ["es2015","react"] }).code')
21+
}

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# reactR
22

3-
A convenience function with local dependencies for using [`React`](https://facebook.github.io/react) in `R`. This is modeled after the `html_dependency_*` functions from RStudio's [`rmarkdown`](https://github.com/rstudio/rmarkdown) package.
3+
A set of convenience function with local dependencies for using [`React`](https://facebook.github.io/react) in `R`. This is modeled after the `html_dependency_*` functions from RStudio's [`rmarkdown`](https://github.com/rstudio/rmarkdown) package.
44

55
## Installation
66

@@ -34,6 +34,22 @@ browsable(attachDependencies(
3434
))
3535
```
3636

37+
`reactR` also uses `V8` if available to transform `JSX` and `ES2015` code.
38+
39+
```R
40+
library(reactR)
41+
library(htmltools)
42+
43+
browsable(
44+
attachDependencies(
45+
tags$script(
46+
babel_transform('ReactDOM.render(<h1>Powered By React/JSX</h1>,document.body)')
47+
),
48+
html_dependency_react()
49+
)
50+
)
51+
```
52+
3753
## Contributing and Code of Conduct
3854

3955
I welcome contributors. Help make this package great. Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

man/babel_transform.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)