worker: handle --input-type more consistently

PR-URL: https://github.com/nodejs/node/pull/54979
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
This commit is contained in:
Antoine du Hamel 2024-09-19 12:08:50 +02:00 committed by GitHub
parent c42d8461b0
commit 6031a4bc7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 2 deletions

View File

@ -55,6 +55,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();
@ -152,7 +153,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.
@ -168,6 +169,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) => {

View File

@ -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');
@ -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' }));