| Title: | 'JavaScript' Utilities for 'R' |
|---|---|
| Description: | A collection of popular/useful JavaScript utilities, including the terser minifier, sass compiler, typescript transpiler, and more. |
| Authors: | Andrew R. Johnson [aut, cre] (ORCID: <https://orcid.org/0000-0001-7000-8065>) |
| Maintainer: | Andrew R. Johnson <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-05-17 10:40:32 UTC |
| Source: | https://github.com/andrjohns/jsutils |
Use the Esprima library to parse or tokenize JavaScript code. Note that the first time this function is called, it will load the esprima library into the JavaScript context, which may take a few seconds. Subsequent calls will be faster.
esprima_parse(input, options = NULL, type = "script") esprima_tokenize(input, options = list())esprima_parse(input, options = NULL, type = "script") esprima_tokenize(input, options = list())
input |
Either a path to a file or a character string containing the JavaScript code to be parsed or tokenized. |
options |
A list of configuration options to pass to the Esprima parser or tokenizer. |
type |
A character string specifying the type of code to parse: "script" (default) or "module". |
For esprima_parse, a list representing the Abstract Syntax Tree (AST) of the parsed code.
For esprima_tokenize, a list of tokens extracted from the input code.
js_code <- 'const answer = 42;' esprima_parse(js_code) esprima_tokenize(js_code)js_code <- 'const answer = 42;' esprima_parse(js_code) esprima_tokenize(js_code)
This function returns the versions of the bundled JavaScript libraries used in the package.
js_versions()js_versions()
A named list with the versions of Esprima, sass.js, Terser, and TypeScript.
js_versions()js_versions()
This function uses the sass.js library to compile SASS/SCSS code into CSS. It leverages the QuickJSR package to run JavaScript code within R. Note that the first time this function is called, it will load the sass.js library into the JavaScript context, which may take a few seconds. Subsequent calls will be faster.
sass(input, options = NULL)sass(input, options = NULL)
input |
Either a path to a file or a character string containing the SASS/SCSS code to be compiled. |
options |
A list of options to pass to the sass.js compiler. |
A list containing the compiled CSS code and any warnings or errors.
scss_code <- "h1 { font-size: 40px; code { font-face: Roboto Mono; } }" sass(scss_code, list(style = "compressed"))scss_code <- "h1 { font-size: 40px; code { font-face: Roboto Mono; } }" sass(scss_code, list(style = "compressed"))
Uses the Terser JavaScript library to minify JavaScript code. Note that the first time this function is called, it will load the terser library into the JavaScript context, which may take a few seconds. Subsequent calls will be faster.
terser(input, options = NULL)terser(input, options = NULL)
input |
Either a path to a file or a character string containing the JavaScript code to be minified. |
options |
A list of options to pass to Terser for minification. See the Terser documentation for available options. |
A list containing the minified code and any warnings or errors.
js_code <- "function add(a, b) { return a + b; }" terser(js_code, list(sourceMap = TRUE))js_code <- "function add(a, b) { return a + b; }" terser(js_code, list(sourceMap = TRUE))
This function uses the TypeScript compiler to transpile TypeScript code into JavaScript. Note that the first time this function is called, it will load the TypeScript library into the JavaScript context, which may take a few seconds. Subsequent calls will be faster.
typescript(input, options = NULL)typescript(input, options = NULL)
input |
Either a path to a file or a character string containing the TypeScript code to be transpiled. |
options |
A list of options to pass to the TypeScript transpiler. See the TypeScript documentation for available options. |
A list containing the transpiled JavaScript code and any diagnostics.
ts_code <- "const greet = (name: string): string => `Hello, ${name}!`;" typescript(ts_code, list(compilerOptions = list(target = "ES5")))ts_code <- "const greet = (name: string): string => `Hello, ${name}!`;" typescript(ts_code, list(compilerOptions = list(target = "ES5")))