diff --git a/DESCRIPTION b/DESCRIPTION index 26b576b..1abded0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: charpente Title: Seamlessly design robust 'shiny' extensions -Version: 0.5.0 +Version: 0.6.0 Authors@R: c( person( given = "David", @@ -29,7 +29,7 @@ Description: 'charpente' eases the creation of 'shiny' extensions like 'shinydas License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.1 +RoxygenNote: 7.2.3 Imports: magrittr, XML, diff --git a/NAMESPACE b/NAMESPACE index 7f777be..463abc9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,8 @@ export(get_dependency_versions) export(get_installed_dependency) export(html_2_R) export(js_handler_template) +export(set_esbuild) +export(set_mocha) export(set_pwa) export(test_js) export(update_dependency) diff --git a/NEWS.md b/NEWS.md index 1c9c283..f190326 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,12 @@ -# charpent 0.5.0 +# charpente 0.6.0 + +## New: +- Allow to initialise `esbuild` and `mocha` in a standalone way, without the need of the full `charpente` workflow. This is useful if you want to use `esbuild` and its plugins in already existing projects. See `?set_esbuild` and `?set_mocha` for more details. + +## Improvements: +- Move `use_build_ignore()` and `use_git_ignore()` calls to `set_esbuild()` to enable standalone usage. + +# charpente 0.5.0 ## Breaking change: - Include Sass handling. SCSS files are sorted in the `/styles`folder and esbuild diff --git a/R/charpente.R b/R/charpente.R index 7ae3096..a14615d 100644 --- a/R/charpente.R +++ b/R/charpente.R @@ -67,19 +67,6 @@ create_charpente <- function(path, remote = NULL, private = FALSE, license) { set_esbuild() # Add mocha for tests set_mocha() - # Ignore files/folders: srcjs, node_modules, ... - use_build_ignore( - c( - "srcjs", - "node_modules", - "package.json", - "package-lock.json", - "styles", - "esbuild.dev.json", - "esbuild.prod.json" - ) - ) - use_git_ignore("node_modules") # version control set_version_control(remote, private) diff --git a/R/jstools.R b/R/jstools.R index 69794bc..f349053 100644 --- a/R/jstools.R +++ b/R/jstools.R @@ -14,7 +14,7 @@ build_js <- function(dir = "srcjs", mode = c("prod", "dev")) { mode <- match.arg(mode) pkg_desc <- desc::description$new("./DESCRIPTION")$get(c("Package", "Version", "License")) - outputDir <- sprintf("inst/%s-%s/js", pkg_desc[1], pkg_desc[2]) + outputDir <- sprintf("inst/%s-%s/dist", pkg_desc[1], pkg_desc[2]) # run esbuild run_esbuild(mode, outputDir) diff --git a/R/utils.R b/R/utils.R index e2f25a7..946d6b5 100644 --- a/R/utils.R +++ b/R/utils.R @@ -155,8 +155,10 @@ reference_style <- function(path) { #' to workaround a breaking change in charpente where styles does #' not exist in old versions. #' -#' @return Installs esbuild in node_modules (dev scope), creates srcjs + srcjs/main.js -#' @keywords internal +#' @return Installs esbuild in node_modules (dev scope), +#' if not existing, creates srcjs + srcjs/main.js andstyles + styles/main.scss, +#' and sets relevant files and folders in .gitignore. and .Rbuildignore. +#' @export set_esbuild <- function(light = FALSE) { pkg_desc <- desc::description$ @@ -185,11 +187,39 @@ set_esbuild <- function(light = FALSE) { # If light, we don't want to recreate srcjs folder # which has always been in charpente since the first release. if (!light) { - dir.create("srcjs") - write("import \"../styles/main.scss\";", "./srcjs/main.js") + + if (!dir.exists("srcjs")) { + dir.create("srcjs") + } + + write("import \"../styles/main.scss\";", + "./srcjs/main.js", + append = file.exists("srcjs/main.js")) + + } + + if (!dir.exists("styles")) { + dir.create("styles") + } + + if (!file.exists("styles/main.scss")) { + file.create("styles/main.scss") } - dir.create("styles") - file.create("styles/main.scss") + + # Ignore files/folders: srcjs, node_modules, ... + use_build_ignore( + c( + "srcjs", + "node_modules", + "package.json", + "package-lock.json", + "styles", + "esbuild.dev.json", + "esbuild.prod.json" + ) + ) + + use_git_ignore("node_modules") } @@ -199,20 +229,26 @@ set_esbuild <- function(light = FALSE) { #' #' @return Installs mocha in node_modules (dev scope), creates srcjs/test folder, #' write basic test im test_basic.js -#' @keywords internal +#' @export set_mocha <- function() { npm::npm_install("mocha", scope = "dev") - dir.create("srcjs/test") - file.create("srcjs/test/test_basic.js") - writeLines( - "describe('Basic test', () => { + + if (!dir.exists("srcjs/test")) { + dir.create("srcjs/test") + } + + if (!file.exists("srcjs/test/test_basic.js")) { + file.create("srcjs/test/test_basic.js") + writeLines( + "describe('Basic test', () => { it('should not fail', (done) => { done(); }); }); ", - "srcjs/test/test_basic.js" - ) + "srcjs/test/test_basic.js" + ) + } } diff --git a/README.md b/README.md index 18687c6..c3ea4b2 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,18 @@ build_js() devtools::load_all() ``` +## Using esbuild and mocha + +If you want to use `esbuild` and `mocha` in an existing project, you can use the functions `set_esbuild()` and `set_mocha()`. A simple workflow looks as follows: + +```r +# Setup esbuild for JS code management +set_esbuild() + +# Add mocha for tests +set_mocha() +``` + ## Acknowledgment The author would like to warmly thank [Victor Perrier](https://twitter.com/_pvictorr?lang=fr), [John Coene](https://twitter.com/jdatap), [Colin Fay](https://twitter.com/_ColinFay), [Alan Dipert](https://twitter.com/alandipert), [Kenton Russel](https://twitter.com/timelyportfolio) for providing many building block and inspiration to this package. diff --git a/inst/utils/package.json b/inst/utils/package.json index 952bf5f..fc1765e 100644 --- a/inst/utils/package.json +++ b/inst/utils/package.json @@ -17,10 +17,10 @@ "author": "", "license": "<>", "devDependencies": { - "esbuild": "^0.8.46", - "esbuild-sass-plugin": "^2.4.0", - "postcss": "^8.4.18", - "autoprefixer": "^10.4.13", + "autoprefixer": "^10.4.17", + "esbuild": "^0.19.11", + "esbuild-sass-plugin": "^2.16.1", + "postcss": "^8.4.33", "mocha": "^8.3.0" } } diff --git a/man/set_esbuild.Rd b/man/set_esbuild.Rd index 304ade4..223136f 100644 --- a/man/set_esbuild.Rd +++ b/man/set_esbuild.Rd @@ -12,9 +12,10 @@ to workaround a breaking change in charpente where styles does not exist in old versions.} } \value{ -Installs esbuild in node_modules (dev scope), creates srcjs + srcjs/main.js +Installs esbuild in node_modules (dev scope), +if not existing, creates srcjs + srcjs/main.js andstyles + styles/main.scss, +and sets relevant files and folders in .gitignore. and .Rbuildignore. } \description{ Installs esbuild for the local project } -\keyword{internal} diff --git a/man/set_mocha.Rd b/man/set_mocha.Rd index 481107f..7d98c98 100644 --- a/man/set_mocha.Rd +++ b/man/set_mocha.Rd @@ -13,4 +13,3 @@ write basic test im test_basic.js \description{ Installs mocha for the local project } -\keyword{internal}