@@ -44,13 +44,15 @@ let serverResolver: ResolverBase;
44
44
let packagingBundles = new Map < NamedBundle , Async < { | contents : Blob | } >> ( ) ;
45
45
let moduleCache = new Map < string , ParcelModule > ( ) ;
46
46
let loadedBundles = new Map < NamedBundle , any > ( ) ;
47
+ let context : any = vm . createContext ( ) ;
47
48
48
49
export default ( new Packager ( {
49
50
async loadConfig ( { options, config} ) {
50
51
config . invalidateOnBuild ( ) ;
51
52
packagingBundles . clear ( ) ;
52
53
moduleCache . clear ( ) ;
53
54
loadedBundles . clear ( ) ;
55
+ context = createContext ( options ) ;
54
56
clientResolver = new ResolverBase ( options . projectRoot , {
55
57
mode : 2 ,
56
58
packageExports : true ,
@@ -76,28 +78,17 @@ export default (new Packager({
76
78
parcelRequireName : 'parcelRequire' + hashString ( name ) . slice ( - 4 ) ,
77
79
} ;
78
80
} ,
79
- async package ( {
80
- bundle,
81
- bundleGraph,
82
- getInlineBundleContents,
83
- config,
84
- options,
85
- } ) {
81
+ async package ( { bundle, bundleGraph, getInlineBundleContents, config} ) {
86
82
if ( bundle . env . shouldScopeHoist ) {
87
83
throw new Error ( 'Scope hoisting is not supported with SSG' ) ;
88
84
}
89
85
90
- // $FlowFixMe
91
- globalThis . AsyncLocalStorage ??= AsyncLocalStorage ;
92
-
93
86
let { load, loadModule} = await loadBundle (
94
87
bundle ,
95
88
bundleGraph ,
96
89
getInlineBundleContents ,
97
90
) ;
98
91
99
- let env = process . env . NODE_ENV ;
100
- process . env . NODE_ENV = options . env . NODE_ENV ;
101
92
let Component = load ( nullthrows ( bundle . getMainEntry ( ) ) . id ) . default ;
102
93
let { renderToReadableStream} = loadModule (
103
94
'react-server-dom-parcel/server.edge' ,
@@ -115,7 +106,6 @@ export default (new Packager({
115
106
__filename ,
116
107
'react-client' ,
117
108
) ;
118
- process . env . NODE_ENV = env ;
119
109
let { injectRSCPayload} = await import ( 'rsc-html-stream/server' ) ;
120
110
121
111
let pages : Page [ ] = [ ] ;
@@ -474,6 +464,38 @@ async function loadBundleUncached(
474
464
return { load : loadAsset , loadModule, assets} ;
475
465
}
476
466
467
+ function createContext ( options ) {
468
+ // Create a fresh global context to execute code in on each build to avoid memory leaks.
469
+ // $FlowFixMe
470
+ let context : any = vm . createContext ( vm . constants . DONT_CONTEXTIFY ) ;
471
+ context . global = context ;
472
+ context . AsyncLocalStorage = AsyncLocalStorage ;
473
+ context . process = new Proxy ( process , {
474
+ get ( target , prop , receiver ) {
475
+ // Expose the provided environment variables from Parcel instead of the global ones.
476
+ if ( prop === 'env' ) {
477
+ return options . env ;
478
+ }
479
+
480
+ return Reflect . get ( target , prop , receiver ) ;
481
+ } ,
482
+ } ) ;
483
+
484
+ // $FlowFixMe
485
+ for ( let key of Object . getOwnPropertyNames ( globalThis ) ) {
486
+ if ( ! context . hasOwnProperty ( key ) ) {
487
+ Object . defineProperty (
488
+ context ,
489
+ key ,
490
+ // $FlowFixMe
491
+ Object . getOwnPropertyDescriptor ( globalThis , key ) ,
492
+ ) ;
493
+ }
494
+ }
495
+
496
+ return context ;
497
+ }
498
+
477
499
function runModule (
478
500
code : string ,
479
501
filename : string ,
@@ -493,6 +515,7 @@ function runModule(
493
515
] ,
494
516
{
495
517
filename,
518
+ parsingContext : context ,
496
519
} ,
497
520
) ;
498
521
0 commit comments