Skip to content

Commit 6031a4b

Browse files
authored
worker: handle --input-type more consistently
PR-URL: #54979 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent c42d846 commit 6031a4b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/internal/main/worker_thread.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ let debug = require('internal/util/debuglog').debuglog('worker', (fn) => {
5555
});
5656

5757
const assert = require('internal/assert');
58+
const { getOptionValue } = require('internal/options');
5859
const { exitCodes: { kGenericUserError } } = internalBinding('errors');
5960

6061
prepareWorkerThreadExecution();
@@ -152,7 +153,7 @@ port.on('message', (message) => {
152153
break;
153154
}
154155

155-
case 'classic': {
156+
case 'classic': if (getOptionValue('--input-type') !== 'module') {
156157
const { evalScript } = require('internal/process/execution');
157158
const name = '[worker eval]';
158159
// This is necessary for CJS module compilation.
@@ -168,6 +169,7 @@ port.on('message', (message) => {
168169
break;
169170
}
170171

172+
// eslint-disable-next-line no-fallthrough
171173
case 'module': {
172174
const { evalModuleEntryPoint } = require('internal/process/execution');
173175
PromisePrototypeThen(evalModuleEntryPoint(filename), undefined, (e) => {

test/parallel/test-worker-cli-options.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Flags: --expose-internals --expose-gc
22
'use strict';
3-
require('../common');
3+
const common = require('../common');
44
const { Worker } = require('worker_threads');
55
const assert = require('assert');
66

@@ -29,3 +29,11 @@ new Worker(CODE, { eval: true, env: process.env, execArgv: ['--expose-internals'
2929
assert.throws(() => {
3030
new Worker(CODE, { eval: true, execArgv: ['--expose-gc'] });
3131
}, /ERR_WORKER_INVALID_EXEC_ARGV/);
32+
33+
// Test ESM eval
34+
new Worker('export {}', { eval: true, execArgv: ['--input-type=module'] });
35+
new Worker('export {}', { eval: true, execArgv: ['--input-type=commonjs'] })
36+
.once('error', common.expectsError({ name: 'SyntaxError' }));
37+
new Worker('export {}', { eval: true, execArgv: ['--experimental-detect-module'] });
38+
new Worker('export {}', { eval: true, execArgv: ['--no-experimental-detect-module'] })
39+
.once('error', common.expectsError({ name: 'SyntaxError' }));

0 commit comments

Comments
 (0)