@@ -10,6 +10,7 @@ import assert from 'node:assert';
10
10
import { randomUUID } from 'node:crypto' ;
11
11
import { join } from 'node:path' ;
12
12
import { pathToFileURL } from 'node:url' ;
13
+ import { MessagePort } from 'node:worker_threads' ;
13
14
import { fileURLToPath } from 'url' ;
14
15
import { JavaScriptTransformer } from '../../../tools/esbuild/javascript-transformer' ;
15
16
@@ -21,12 +22,14 @@ import { JavaScriptTransformer } from '../../../tools/esbuild/javascript-transfo
21
22
const MEMORY_URL_SCHEME = 'memory://' ;
22
23
23
24
export interface ESMInMemoryFileLoaderWorkerData {
24
- outputFiles : Record < string , string > ;
25
+ jsOutputFilesForWorker : Record < string , string > ;
25
26
workspaceRoot : string ;
26
27
}
27
28
28
- let memoryVirtualRootUrl : string ;
29
- let outputFiles : Record < string , string > ;
29
+ interface ESMInMemoryFileLoaderResolutionData {
30
+ memoryVirtualRootUrl : string ;
31
+ outputFiles : Record < string , string > ;
32
+ }
30
33
31
34
const javascriptTransformer = new JavaScriptTransformer (
32
35
// Always enable JIT linking to support applications built with and without AOT.
@@ -36,23 +39,46 @@ const javascriptTransformer = new JavaScriptTransformer(
36
39
1 ,
37
40
) ;
38
41
39
- export function initialize ( data : ESMInMemoryFileLoaderWorkerData ) {
40
- // This path does not actually exist but is used to overlay the in memory files with the
41
- // actual filesystem for resolution purposes.
42
- // A custom URL schema (such as `memory://`) cannot be used for the resolve output because
43
- // the in-memory files may use `import.meta.url` in ways that assume a file URL.
44
- // `createRequire` is one example of this usage.
45
- memoryVirtualRootUrl = pathToFileURL (
46
- join ( data . workspaceRoot , `.angular/prerender-root/${ randomUUID ( ) } /` ) ,
47
- ) . href ;
48
- outputFiles = data . outputFiles ;
42
+ let resolveData : Promise < ESMInMemoryFileLoaderResolutionData > ;
43
+
44
+ export function initialize ( data : { port : MessagePort } | ESMInMemoryFileLoaderWorkerData ) {
45
+ resolveData = new Promise < ESMInMemoryFileLoaderResolutionData > ( ( resolve ) => {
46
+ if ( ! ( 'port' in data ) ) {
47
+ /** TODO: Remove when Node.js Removes < 22.2 are no longer supported. */
48
+ resolve ( {
49
+ outputFiles : data . jsOutputFilesForWorker ,
50
+ memoryVirtualRootUrl : pathToFileURL (
51
+ join ( data . workspaceRoot , `.angular/prerender-root/${ randomUUID ( ) } /` ) ,
52
+ ) . href ,
53
+ } ) ;
54
+
55
+ return ;
56
+ }
57
+
58
+ const { port } = data ;
59
+ port . once (
60
+ 'message' ,
61
+ ( { jsOutputFilesForWorker, workspaceRoot } : ESMInMemoryFileLoaderWorkerData ) => {
62
+ resolve ( {
63
+ outputFiles : jsOutputFilesForWorker ,
64
+ memoryVirtualRootUrl : pathToFileURL (
65
+ join ( workspaceRoot , `.angular/prerender-root/${ randomUUID ( ) } /` ) ,
66
+ ) . href ,
67
+ } ) ;
68
+
69
+ port . close ( ) ;
70
+ } ,
71
+ ) ;
72
+ } ) ;
49
73
}
50
74
51
- export function resolve (
75
+ export async function resolve (
52
76
specifier : string ,
53
77
context : { parentURL : undefined | string } ,
54
78
nextResolve : Function ,
55
79
) {
80
+ const { outputFiles, memoryVirtualRootUrl } = await resolveData ;
81
+
56
82
// In-memory files loaded from external code will contain a memory scheme
57
83
if ( specifier . startsWith ( MEMORY_URL_SCHEME ) ) {
58
84
let memoryUrl ;
@@ -89,7 +115,7 @@ export function resolve(
89
115
90
116
if (
91
117
specifierUrl ?. pathname &&
92
- Object . hasOwn ( outputFiles , specifierUrl . href . slice ( memoryVirtualRootUrl . length ) )
118
+ outputFiles [ specifierUrl . href . slice ( memoryVirtualRootUrl . length ) ] !== undefined
93
119
) {
94
120
return {
95
121
format : 'module' ,
@@ -114,6 +140,7 @@ export function resolve(
114
140
}
115
141
116
142
export async function load ( url : string , context : { format ?: string | null } , nextLoad : Function ) {
143
+ const { outputFiles, memoryVirtualRootUrl } = await resolveData ;
117
144
const { format } = context ;
118
145
119
146
// Load the file from memory if the URL is based in the virtual root
0 commit comments