Fixing issue with string templates importing vm2 which requires other prebuilts for JS in the CLI - no need for these to add to build size when JS is unused.

This commit is contained in:
mike12345567 2022-09-05 19:04:45 +01:00
parent 5b685b1f1b
commit a506ce05a1
6 changed files with 43 additions and 25 deletions

View File

@ -0,0 +1 @@
process.env.NO_JS = "1"

View File

@ -1,5 +1,6 @@
#!/usr/bin/env node
require("./prebuilds")
require("./environment")
const { getCommands } = require("./options")
const { Command } = require("commander")
const { getHelpDescription } = require("./utils")

View File

@ -1,6 +1,7 @@
const os = require("os")
const { join } = require("path")
const fs = require("fs")
const { error } = require("./utils")
const PREBUILDS = "prebuilds"
const ARCH = `${os.platform()}-${os.arch()}`
const PREBUILD_DIR = join(process.execPath, "..", PREBUILDS, ARCH)
@ -25,7 +26,15 @@ function checkForBinaries() {
}
}
function cleanup() {
function cleanup(evt) {
if (evt && evt.errno) {
console.error(
error(
"Failed to run CLI command - please report with the following message:"
)
)
console.error(error(evt))
}
if (fs.existsSync(PREBUILD_DIR)) {
fs.rmSync(PREBUILD_DIR, { recursive: true })
}

View File

@ -36,6 +36,9 @@ const getContextValue = (path, context) => {
// Evaluates JS code against a certain context
module.exports.processJS = (handlebars, context) => {
if (process && process.env.NO_JS) {
throw new Error("JS disabled in environment.")
}
try {
// Wrap JS in a function and immediately invoke it.
// This is required to allow the final `return` statement to be valid.

View File

@ -1,6 +1,4 @@
const { VM } = require("vm2")
const templates = require("./index.js")
const { setJSRunner } = require("./helpers/javascript")
/**
* CJS entrypoint for rollup
@ -21,13 +19,17 @@ module.exports.disableEscaping = templates.disableEscaping
module.exports.findHBSBlocks = templates.findHBSBlocks
module.exports.convertToJS = templates.convertToJS
/**
* Use vm2 to run JS scripts in a node env
*/
setJSRunner((js, context) => {
const vm = new VM({
sandbox: context,
timeout: 1000
if (!process.env.NO_JS) {
const { VM } = require("vm2")
const { setJSRunner } = require("./helpers/javascript")
/**
* Use vm2 to run JS scripts in a node env
*/
setJSRunner((js, context) => {
const vm = new VM({
sandbox: context,
timeout: 1000
})
return vm.run(js)
})
return vm.run(js)
})
}

View File

@ -21,16 +21,18 @@ export const disableEscaping = templates.disableEscaping
export const findHBSBlocks = templates.findHBSBlocks
export const convertToJS = templates.convertToJS
/**
* Use polyfilled vm to run JS scripts in a browser Env
*/
setJSRunner((js, context) => {
context = {
...context,
alert: undefined,
setInterval: undefined,
setTimeout: undefined,
}
vm.createContext(context)
return vm.runInNewContext(js, context, { timeout: 1000 })
})
if (process && !process.env.NO_JS) {
/**
* Use polyfilled vm to run JS scripts in a browser Env
*/
setJSRunner((js, context) => {
context = {
...context,
alert: undefined,
setInterval: undefined,
setTimeout: undefined,
}
vm.createContext(context)
return vm.runInNewContext(js, context, { timeout: 1000 })
})
}