From 0fd1eaa07fc04b370debce7e7eec9c03aba11e83 Mon Sep 17 00:00:00 2001 From: ZaneHannanAU Date: Fri, 10 May 2019 14:24:24 +1000 Subject: [PATCH 1/2] Attempt setup of synchronous use (no workers) --- index.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index fd741ab..beb6f0a 100644 --- a/index.js +++ b/index.js @@ -12,10 +12,21 @@ function terser(userOptions = {}) { renderChunk(code, chunk, outputOptions) { if (!this.worker) { - this.worker = new Worker(require.resolve("./transform.js"), { - numWorkers: userOptions.numWorkers - }); - this.numOfBundles = 0; + if (userOptions.nameCache || userOptions.numWorkers === 0) { + // Transform syncronously + this.worker = { + transform: function() { + const transformer = require("./transform.js"); + return Promise.resolve(transformer.transform.apply(null, arguments)); + }, + end: function() {} // noop + }; + } else { + this.worker = new Worker(require.resolve("./transform.js"), { + numWorkers: userOptions.numWorkers + }); + this.numOfBundles = 0; + } } this.numOfBundles++; @@ -32,7 +43,9 @@ function terser(userOptions = {}) { } } - const serializedOptions = serialize(normalizedOptions); + const serializedOptions = (userOptions.nameCache || userOptions.numWorkers === 0) + ? normalizedOptions + : serialize(normalizedOptions); const result = this.worker .transform(code, serializedOptions) From 8a16c28aa7fe1228518bf2d558ddabea0e7a3c8d Mon Sep 17 00:00:00 2001 From: ZaneHannanAU Date: Fri, 10 May 2019 14:25:43 +1000 Subject: [PATCH 2/2] Complete --- transform.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transform.js b/transform.js index 64b62bf..3cfe4dc 100644 --- a/transform.js +++ b/transform.js @@ -1,7 +1,7 @@ const { minify } = require("terser"); const transform = (code, optionsString) => { - const options = eval(`(${optionsString})`); + const options = typeof optionsString === 'string' ? eval(`(${optionsString})`) : optionsString; const result = minify(code, options); if (result.error) { throw result.error;