Skip to content
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
1 change: 1 addition & 0 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ class AsyncLoaderHookWorker {
stdin: false,
stdout: false,
trackUnmanagedFds: false,
execArgv: process.execArgv,
workerData: {
lock,
},
Expand Down
53 changes: 53 additions & 0 deletions test/es-module/test-esm-loader-worker-conditions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { mustCall, mustNotCall } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { Worker } from 'node:worker_threads';

const loaderURL = fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs');
const targetURL = fixtures.fileURL('es-modules', 'conditional-exports.mjs');

// Test that custom conditions passed to Worker via execArgv
// are forwarded to the internal ESM loader hook worker thread when using register().
{
const worker = new Worker(
`
import assert from 'node:assert';
import { register } from 'node:module';
register(${JSON.stringify(loaderURL.href)});
const ns = await import(${JSON.stringify(targetURL.href)});
assert.strictEqual(ns.default, 'from custom condition');
`,
{
eval: true,
execArgv: ['--conditions', 'custom-condition', '--no-warnings'],
}
);

worker.on('error', mustNotCall());
worker.on('exit', mustCall((code) => {
assert.strictEqual(code, 0);
}));
}

// Test that custom conditions passed to Worker via execArgv
// are forwarded to the internal ESM loader hook worker thread when using registerHooks().
{
const worker = new Worker(
`
import assert from 'node:assert';
import { registerHooks } from 'node:module';
registerHooks(${JSON.stringify(loaderURL.href)});
const ns = await import(${JSON.stringify(targetURL.href)});
assert.strictEqual(ns.default, 'from custom condition');
`,
{
eval: true,
execArgv: ['--conditions', 'custom-condition', '--no-warnings'],
}
);

worker.on('error', mustNotCall());
worker.on('exit', mustCall((code) => {
assert.strictEqual(code, 0);
}));
}
Loading