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 5c62bdac2e
commit feda4e61c1
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 #!/usr/bin/env node
require("./prebuilds") require("./prebuilds")
require("./environment")
const { getCommands } = require("./options") const { getCommands } = require("./options")
const { Command } = require("commander") const { Command } = require("commander")
const { getHelpDescription } = require("./utils") const { getHelpDescription } = require("./utils")

View File

@ -1,6 +1,7 @@
const os = require("os") const os = require("os")
const { join } = require("path") const { join } = require("path")
const fs = require("fs") const fs = require("fs")
const { error } = require("./utils")
const PREBUILDS = "prebuilds" const PREBUILDS = "prebuilds"
const ARCH = `${os.platform()}-${os.arch()}` const ARCH = `${os.platform()}-${os.arch()}`
const PREBUILD_DIR = join(process.execPath, "..", PREBUILDS, 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)) { if (fs.existsSync(PREBUILD_DIR)) {
fs.rmSync(PREBUILD_DIR, { recursive: true }) fs.rmSync(PREBUILD_DIR, { recursive: true })
} }

View File

@ -36,6 +36,9 @@ const getContextValue = (path, context) => {
// Evaluates JS code against a certain context // Evaluates JS code against a certain context
module.exports.processJS = (handlebars, context) => { module.exports.processJS = (handlebars, context) => {
if (process && process.env.NO_JS) {
throw new Error("JS disabled in environment.")
}
try { try {
// Wrap JS in a function and immediately invoke it. // Wrap JS in a function and immediately invoke it.
// This is required to allow the final `return` statement to be valid. // 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 templates = require("./index.js")
const { setJSRunner } = require("./helpers/javascript")
/** /**
* CJS entrypoint for rollup * CJS entrypoint for rollup
@ -21,13 +19,17 @@ module.exports.disableEscaping = templates.disableEscaping
module.exports.findHBSBlocks = templates.findHBSBlocks module.exports.findHBSBlocks = templates.findHBSBlocks
module.exports.convertToJS = templates.convertToJS module.exports.convertToJS = templates.convertToJS
/** if (!process.env.NO_JS) {
const { VM } = require("vm2")
const { setJSRunner } = require("./helpers/javascript")
/**
* Use vm2 to run JS scripts in a node env * Use vm2 to run JS scripts in a node env
*/ */
setJSRunner((js, context) => { setJSRunner((js, context) => {
const vm = new VM({ const vm = new VM({
sandbox: context, sandbox: context,
timeout: 1000 timeout: 1000
}) })
return vm.run(js) return vm.run(js)
}) })
}

View File

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