From a506ce05a17384537e6d9600a355cccb0caa291c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 5 Sep 2022 19:04:45 +0100 Subject: [PATCH] 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. --- packages/cli/src/environment.js | 1 + packages/cli/src/index.js | 1 + packages/cli/src/prebuilds.js | 11 +++++++- .../src/helpers/javascript.js | 3 ++ packages/string-templates/src/index.cjs | 24 ++++++++-------- packages/string-templates/src/index.mjs | 28 ++++++++++--------- 6 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 packages/cli/src/environment.js diff --git a/packages/cli/src/environment.js b/packages/cli/src/environment.js new file mode 100644 index 0000000000..a42eceb07e --- /dev/null +++ b/packages/cli/src/environment.js @@ -0,0 +1 @@ +process.env.NO_JS = "1" diff --git a/packages/cli/src/index.js b/packages/cli/src/index.js index 0a3ced4200..691fc71928 100644 --- a/packages/cli/src/index.js +++ b/packages/cli/src/index.js @@ -1,5 +1,6 @@ #!/usr/bin/env node require("./prebuilds") +require("./environment") const { getCommands } = require("./options") const { Command } = require("commander") const { getHelpDescription } = require("./utils") diff --git a/packages/cli/src/prebuilds.js b/packages/cli/src/prebuilds.js index b8f380a937..b582b090e7 100644 --- a/packages/cli/src/prebuilds.js +++ b/packages/cli/src/prebuilds.js @@ -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 }) } diff --git a/packages/string-templates/src/helpers/javascript.js b/packages/string-templates/src/helpers/javascript.js index 951a9f534a..c5996c25f0 100644 --- a/packages/string-templates/src/helpers/javascript.js +++ b/packages/string-templates/src/helpers/javascript.js @@ -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. diff --git a/packages/string-templates/src/index.cjs b/packages/string-templates/src/index.cjs index 870e14493a..12fd86b6cb 100644 --- a/packages/string-templates/src/index.cjs +++ b/packages/string-templates/src/index.cjs @@ -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) -}) +} diff --git a/packages/string-templates/src/index.mjs b/packages/string-templates/src/index.mjs index 34cb90ea34..58b0c9dde4 100644 --- a/packages/string-templates/src/index.mjs +++ b/packages/string-templates/src/index.mjs @@ -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 }) + }) +}