File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import path from 'path' ;
2+
3+ process . exit ( path . basename ( import . meta. url ) === 'esm.mjs' ? 0 : 1 ) ;
Original file line number Diff line number Diff 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+
4567const nodeOptionRequireSelf = generateRequire ( require . resolve ( './node-preload.js' ) ) ;
4668const preloadList = loadPreloadList ( ) ;
4769const propagate = loadPropagated ( ) ;
70+ const preloadModule = findPreloadModule ( ) ;
4871
4972function processNodeOptions ( value ) {
5073 if ( value . includes ( nodeOptionRequireSelf ) ) {
@@ -124,7 +147,7 @@ ChildProcess.prototype.spawn = wrappedSpawnFunction(ChildProcess.prototype.spawn
124147
125148function executePreload ( ) {
126149 preloadList . forEach ( file => {
127- require ( file ) ;
150+ preloadModule . require ( file ) ;
128151 } ) ;
129152}
130153
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments