Skip to content

Commit

Permalink
worker: handle --input-type more consistently
Browse files Browse the repository at this point in the history
PR-URL: #54979
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
  • Loading branch information
aduh95 authored and marco-ippolito committed Nov 16, 2024
1 parent 1320c38 commit f9744c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
});

const assert = require('internal/assert');
const { getOptionValue } = require('internal/options');
const { exitCodes: { kGenericUserError } } = internalBinding('errors');

prepareWorkerThreadExecution();
Expand Down Expand Up @@ -153,7 +154,7 @@ port.on('message', (message) => {
break;
}

case 'classic': {
case 'classic': if (getOptionValue('--input-type') !== 'module') {
const { evalScript } = require('internal/process/execution');
const name = '[worker eval]';
// This is necessary for CJS module compilation.
Expand All @@ -169,6 +170,7 @@ port.on('message', (message) => {
break;
}

// eslint-disable-next-line no-fallthrough
case 'module': {
const { evalModuleEntryPoint } = require('internal/process/execution');
PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => {
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-worker-cli-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Flags: --expose-internals --expose-gc
'use strict';
require('../common');
const common = require('../common');
const { Worker } = require('worker_threads');
const assert = require('assert');

Expand Down Expand Up @@ -29,3 +29,11 @@ new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'
assert.throws(() => {
new Worker(CODE, { eval: true, execArgv: ['--expose-gc'] });
}, /ERR_WORKER_INVALID_EXEC_ARGV/);

// Test ESM eval
new Worker('export {}', { eval: true, execArgv: ['--input-type=module'] });
new Worker('export {}', { eval: true, execArgv: ['--input-type=commonjs'] })
.once('error', common.expectsError({ name: 'SyntaxError' }));
new Worker('export {}', { eval: true, execArgv: ['--experimental-detect-module'] });
new Worker('export {}', { eval: true, execArgv: ['--no-experimental-detect-module'] })
.once('error', common.expectsError({ name: 'SyntaxError' }));

0 comments on commit f9744c5

Please sign in to comment.