Skip to content

Commit

Permalink
fix(terser): exit process on promise rejection of worker
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 10, 2022
1 parent 66da603 commit 9136794
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
62 changes: 33 additions & 29 deletions packages/terser/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,47 @@ export default function terser(options: Options = {}) {
defaultOptions.toplevel = true;
}

const { code: result, nameCache } = await callWorker(__filename, {
code,
options: merge({}, options || {}, defaultOptions)
});

if (options.nameCache && nameCache) {
let vars: Record<string, any> = {
props: {}
};

if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) {
vars = merge({}, options.nameCache.vars || {}, vars);
}
try {
const { code: result, nameCache } = await callWorker(__filename, {
code,
options: merge({}, options || {}, defaultOptions)
});

if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) {
vars = merge({}, nameCache.vars, vars);
}
if (options.nameCache && nameCache) {
let vars: Record<string, any> = {
props: {}
};

// eslint-disable-next-line no-param-reassign
options.nameCache.vars = vars;
if (hasOwnProperty(options.nameCache, 'vars') && isObject(options.nameCache.vars)) {
vars = merge({}, options.nameCache.vars || {}, vars);
}

let props: Record<string, any> = {};
if (hasOwnProperty(nameCache, 'vars') && isObject(nameCache.vars)) {
vars = merge({}, nameCache.vars, vars);
}

if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) {
// eslint-disable-next-line prefer-destructuring
props = options.nameCache.props;
}
// eslint-disable-next-line no-param-reassign
options.nameCache.vars = vars;

let props: Record<string, any> = {};

if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) {
props = merge({}, nameCache.props, props);
if (hasOwnProperty(options.nameCache, 'props') && isObject(options.nameCache.props)) {
// eslint-disable-next-line prefer-destructuring
props = options.nameCache.props;
}

if (hasOwnProperty(nameCache, 'props') && isObject(nameCache.props)) {
props = merge({}, nameCache.props, props);
}

// eslint-disable-next-line no-param-reassign
options.nameCache.props = props;
}

// eslint-disable-next-line no-param-reassign
options.nameCache.props = props;
return result;
} catch (e) {
return Promise.reject(e);
}

return result;
}
};
}
25 changes: 15 additions & 10 deletions packages/terser/src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'process';
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads';

import serializeJavascript from 'serialize-javascript';
Expand Down Expand Up @@ -37,7 +38,7 @@ export async function callWorker(filePath: string, context: WorkerContext) {
worker.on('error', reject);

worker.on('exit', (code) => {
if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
if (code !== 0) reject(new Error(`Minify worker stopped with exit code ${code}`));
});
});
}
Expand All @@ -47,17 +48,21 @@ export async function runWorker() {
return;
}

// eslint-disable-next-line no-eval
const eval2 = eval;
try {
// eslint-disable-next-line no-eval
const eval2 = eval;

const options = eval2(`(${workerData.options})`);
const options = eval2(`(${workerData.options})`);

const result = await minify(workerData.code, options);
const result = await minify(workerData.code, options);

const output: WorkerOutput = {
code: result.code || workerData.code,
nameCache: options.nameCache
};
const output: WorkerOutput = {
code: result.code || workerData.code,
nameCache: options.nameCache
};

parentPort.postMessage(output);
parentPort.postMessage(output);
} catch (e) {
process.exit(1);
}
}
4 changes: 2 additions & 2 deletions packages/terser/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ test.serial('throw error on terser fail', async (t) => {
await bundle.generate({ format: 'esm' });
t.falsy(true);
} catch (error) {
t.is(error.toString(), 'SyntaxError: Name expected');
t.is(error.toString(), 'Error: Minify worker stopped with exit code 1');
}
});

Expand All @@ -127,7 +127,7 @@ test.serial('throw error on terser fail with multiple outputs', async (t) => {
await Promise.all([bundle.generate({ format: 'cjs' }), bundle.generate({ format: 'esm' })]);
t.falsy(true);
} catch (error) {
t.is(error.toString(), 'SyntaxError: Name expected');
t.is(error.toString(), 'Error: Minify worker stopped with exit code 1');
}
});

Expand Down

0 comments on commit 9136794

Please sign in to comment.