Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion transform.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down