@@ -4,6 +4,43 @@ import fs from 'fs';
44import { URL } from 'url' ;
55
66const lookup = { } ;
7+ let envVars = { } ;
8+ const port = { } ; // dummy variable, not used
9+
10+ /* serialized and passed into main process */
11+ function createListener ( ) {
12+ /* communicate process.env to loader process */
13+ process . env = new Proxy ( process . env , {
14+ set ( target , key , value ) {
15+ // eslint-disable-next-line no-param-reassign
16+ target [ key ] = value ;
17+ port . postMessage ( target ) ;
18+ return target [ key ] ;
19+ } ,
20+ deleteProperty ( target , key ) {
21+ if ( ! ( key in target ) ) {
22+ return false ;
23+ }
24+ // eslint-disable-next-line no-param-reassign
25+ delete target [ key ] ;
26+ port . postMessage ( target ) ;
27+ return true ;
28+ }
29+ } ) ;
30+ }
31+
32+ export function globalPreload ( { port : p } ) {
33+ if ( process . versions . node . split ( '.' ) [ 0 ] < 20 ) {
34+ /* Skip listener, since process shared before node 20 */
35+ envVars = process . env ;
36+ return '(() => {})()' ;
37+ }
38+ // eslint-disable-next-line no-param-reassign
39+ p . onmessage = ( { data } ) => {
40+ envVars = data ;
41+ } ;
42+ return `(${ createListener } )()` ;
43+ }
744
845export const resolve = async ( specifier , context , defaultResolve ) => {
946 const result = await defaultResolve ( specifier , context , defaultResolve ) ;
@@ -59,7 +96,7 @@ export const resolve = async (specifier, context, defaultResolve) => {
5996 }
6097 }
6198
62- if ( ! ( 'TEST_SEED' in process . env ) ) {
99+ if ( ! ( 'TEST_SEED' in envVars ) ) {
63100 return result ;
64101 }
65102
@@ -69,7 +106,7 @@ export const resolve = async (specifier, context, defaultResolve) => {
69106
70107 if ( Array . isArray ( lookup [ childPath ] . reload ) ) {
71108 const hash = lookup [ childPath ] . reload . reduce (
72- ( p , c ) => p . update ( c ) . update ( process . env [ c ] || '<undefined>' ) ,
109+ ( p , c ) => p . update ( c ) . update ( envVars [ c ] || '<undefined>' ) ,
73110 crypto . createHash ( 'md5' )
74111 ) . digest ( 'hex' ) ;
75112 return {
@@ -78,6 +115,6 @@ export const resolve = async (specifier, context, defaultResolve) => {
78115 }
79116
80117 return {
81- url : `${ child . href } ?id=${ process . env . TEST_SEED } `
118+ url : `${ child . href } ?id=${ envVars . TEST_SEED } `
82119 } ;
83120} ;
0 commit comments