Skip to content

Commit a5444dd

Browse files
committed
fix: esm must be loaded from the internal/preload module
1 parent 7265b77 commit a5444dd

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

fixtures/esm.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import path from 'path';
2+
3+
process.exit(path.basename(import.meta.url) === 'esm.mjs' ? 0 : 1);

node-preload-singleton.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,32 @@ function loadPreloadList() {
4242
return env.split(path.delimiter);
4343
}
4444

45+
function findPreloadModule() {
46+
/* This song-and-dance is to keep esm happy. */
47+
let mod = module;
48+
const seen = new Set([mod]);
49+
while ((mod = mod.parent)) {
50+
/* Generally if we're being preloaded then
51+
* mod.parent.id should be 'internal/preload' */
52+
/* istanbul ignore next: paranoia */
53+
if (seen.has(mod)) {
54+
return module;
55+
}
56+
57+
seen.add(mod);
58+
/* istanbul ignore next: this is hit but coverage cannot be collected */
59+
if (mod.id === 'internal/preload') {
60+
return mod;
61+
}
62+
}
63+
64+
return module;
65+
}
66+
4567
const nodeOptionRequireSelf = generateRequire(require.resolve('./node-preload.js'));
4668
const preloadList = loadPreloadList();
4769
const propagate = loadPropagated();
70+
const preloadModule = findPreloadModule();
4871

4972
function processNodeOptions(value) {
5073
if (value.includes(nodeOptionRequireSelf)) {
@@ -124,7 +147,7 @@ ChildProcess.prototype.spawn = wrappedSpawnFunction(ChildProcess.prototype.spawn
124147

125148
function executePreload() {
126149
preloadList.forEach(file => {
127-
require(file);
150+
preloadModule.require(file);
128151
});
129152
}
130153

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"homepage": "https://github.com/cfware/node-preload#readme",
2727
"devDependencies": {
28+
"esm": "^3.2.25",
2829
"standard-version": "^7.0.0",
2930
"tap": "^14.6.1",
3031
"xo": "^0.24.0"

test/esm.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const {spawnSync} = require('child_process');
4+
const {test} = require('tap');
5+
6+
require('../node-preload').unload();
7+
const nodePreload = require('../node-preload');
8+
9+
function runSpawn(t, args, env) {
10+
const {status, stdout, stderr} = spawnSync(
11+
process.argv[0],
12+
args.concat(require.resolve('../fixtures/esm.mjs')),
13+
{cwd: __dirname, encoding: 'utf8', env}
14+
);
15+
t.is(stderr, '');
16+
t.is(status, 0);
17+
t.is(stdout, '');
18+
}
19+
20+
test('spawn', async t => {
21+
const esm = require.resolve('esm');
22+
nodePreload.preloadAppend(esm);
23+
24+
runSpawn(t, []);
25+
runSpawn(t, [], {});
26+
27+
/* Preload something other than esm then use `-r esm` on the command-line. */
28+
nodePreload.preloadRemove(esm);
29+
nodePreload.preloadAppend(require.resolve('../fixtures/file1.js'));
30+
31+
runSpawn(t, ['-r', 'esm']);
32+
runSpawn(t, ['-r', 'esm'], {});
33+
});

0 commit comments

Comments
 (0)