Skip to content
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

Standalone usage of set_esbuild and set_mocha functions #23

Merged
merged 8 commits into from
Jan 22, 2024
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 DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 0 additions & 13 deletions R/charpente.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/jstools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 49 additions & 13 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down Expand Up @@ -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")
}


Expand All @@ -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"
)
}
}


Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions inst/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"author": "",
"license": "<<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"
}
}
5 changes: 3 additions & 2 deletions man/set_esbuild.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/set_mocha.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading