@@ -26,9 +26,10 @@ export async function compileApplicationToWasm(
26
26
output ,
27
27
wasmEngine ,
28
28
enableExperimentalHighResolutionTimeMethods = false ,
29
- enableExperimentalTopLevelAwait = false ,
30
29
enableAOT = false ,
31
30
aotCache = '' ,
31
+ moduleMode = false ,
32
+ doBundle = false ,
32
33
) {
33
34
try {
34
35
if ( ! ( await isFile ( input ) ) ) {
@@ -94,86 +95,132 @@ export async function compileApplicationToWasm(
94
95
process . exit ( 1 ) ;
95
96
}
96
97
97
- let contents ;
98
- try {
99
- contents = await bundle ( input , enableExperimentalTopLevelAwait ) ;
100
- } catch ( error ) {
101
- console . error ( `Error:` , error . message ) ;
102
- process . exit ( 1 ) ;
103
- }
98
+ let tmpDir ;
99
+ if ( doBundle ) {
100
+ let contents ;
101
+ try {
102
+ contents = await bundle ( input , moduleMode ) ;
103
+ } catch ( error ) {
104
+ console . error ( `Error:` , error . message ) ;
105
+ process . exit ( 1 ) ;
106
+ }
104
107
105
- let wizerInput = precompile (
106
- contents . outputFiles [ 0 ] . text ,
107
- undefined ,
108
- enableExperimentalTopLevelAwait ,
109
- ) ;
108
+ const precompiled = precompile (
109
+ contents . outputFiles [ 0 ] . text ,
110
+ undefined ,
111
+ moduleMode ,
112
+ ) ;
110
113
111
- // for StarlingMonkey, we need to write to a tmpdir pending streaming source hooks or similar
112
- const tmpDir = await getTmpDir ( ) ;
113
- const outPath = resolve ( tmpDir , 'input.js' ) ;
114
- await writeFile ( outPath , wizerInput ) ;
115
- wizerInput = outPath ;
114
+ tmpDir = await getTmpDir ( ) ;
115
+ const outPath = resolve ( tmpDir , 'input.js' ) ;
116
+ await writeFile ( outPath , precompiled ) ;
116
117
117
- try {
118
- if ( enableAOT ) {
119
- const wevalBin = await weval ( ) ;
118
+ // the bundled output is now the Wizer input
119
+ input = outPath ;
120
+ }
120
121
121
- let wevalProcess = spawnSync (
122
- `"${ wevalBin } "` ,
123
- [
124
- 'weval' ,
125
- ...( aotCache ? [ `--cache-ro ${ aotCache } ` ] : [ ] ) ,
126
- '--dir .' ,
127
- `--dir ${ maybeWindowsPath ( dirname ( wizerInput ) ) } ` ,
128
- '-w' ,
129
- `-i "${ wasmEngine } "` ,
130
- `-o "${ output } "` ,
131
- ] ,
132
- {
133
- stdio : [ null , process . stdout , process . stderr ] ,
134
- input : `${ maybeWindowsPath ( wizerInput ) } ${ enableExperimentalTopLevelAwait ? '' : ' --legacy-script' } ` ,
135
- shell : true ,
136
- encoding : 'utf-8' ,
137
- env : {
138
- ...process . env ,
139
- ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS :
140
- enableExperimentalHighResolutionTimeMethods ? '1' : '0' ,
141
- } ,
122
+ try {
123
+ if ( ! doBundle ) {
124
+ // assert(moduleMode);
125
+ const spawnOpts = {
126
+ stdio : [ null , process . stdout , process . stderr ] ,
127
+ input : maybeWindowsPath ( input ) ,
128
+ shell : true ,
129
+ encoding : 'utf-8' ,
130
+ env : {
131
+ ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS :
132
+ enableExperimentalHighResolutionTimeMethods ? '1' : '0' ,
142
133
} ,
143
- ) ;
144
- if ( wevalProcess . status !== 0 ) {
145
- throw new Error ( `Weval initialization failure` ) ;
134
+ } ;
135
+ if ( enableAOT ) {
136
+ const wevalBin = await weval ( ) ;
137
+
138
+ let wevalProcess = spawnSync (
139
+ `"${ wevalBin } "` ,
140
+ [
141
+ 'weval' ,
142
+ ...( aotCache ? [ `--cache-ro ${ aotCache } ` ] : [ ] ) ,
143
+ `--dir="${ maybeWindowsPath ( process . cwd ( ) ) } "` ,
144
+ '-w' ,
145
+ `-i "${ wasmEngine } "` ,
146
+ `-o "${ output } "` ,
147
+ ] ,
148
+ spawnOpts ,
149
+ ) ;
150
+ if ( wevalProcess . status !== 0 ) {
151
+ throw new Error ( `Weval initialization failure` ) ;
152
+ }
153
+ process . exitCode = wevalProcess . status ;
154
+ } else {
155
+ let wizerProcess = spawnSync (
156
+ `"${ wizer } "` ,
157
+ [
158
+ '--allow-wasi' ,
159
+ `--wasm-bulk-memory=true` ,
160
+ `--dir="${ maybeWindowsPath ( process . cwd ( ) ) } "` ,
161
+ '-r _start=wizer.resume' ,
162
+ `-o="${ output } "` ,
163
+ `"${ wasmEngine } "` ,
164
+ ] ,
165
+ spawnOpts ,
166
+ ) ;
167
+ if ( wizerProcess . status !== 0 ) {
168
+ throw new Error ( `Wizer initialization failure` ) ;
169
+ }
170
+ process . exitCode = wizerProcess . status ;
146
171
}
147
- process . exitCode = wevalProcess . status ;
148
172
} else {
149
- let wizerProcess = spawnSync (
150
- `"${ wizer } "` ,
151
- [
152
- '--inherit-env=true' ,
153
- '--allow-wasi' ,
154
- '--dir=.' ,
155
- `--dir=${ maybeWindowsPath ( dirname ( wizerInput ) ) } ` ,
156
- `--wasm-bulk-memory=true` ,
157
- '-r _start=wizer.resume' ,
158
- `-o="${ output } "` ,
159
- `"${ wasmEngine } "` ,
160
- ] ,
161
- {
162
- stdio : [ null , process . stdout , process . stderr ] ,
163
- input : `${ maybeWindowsPath ( wizerInput ) } ${ enableExperimentalTopLevelAwait ? '' : ' --legacy-script' } ` ,
164
- shell : true ,
165
- encoding : 'utf-8' ,
166
- env : {
167
- ...process . env ,
168
- ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS :
169
- enableExperimentalHighResolutionTimeMethods ? '1' : '0' ,
170
- } ,
173
+ const spawnOpts = {
174
+ stdio : [ null , process . stdout , process . stderr ] ,
175
+ input : `${ maybeWindowsPath ( input ) } ${ moduleMode ? '' : ' --legacy-script' } ` ,
176
+ shell : true ,
177
+ encoding : 'utf-8' ,
178
+ env : {
179
+ ...process . env ,
180
+ ENABLE_EXPERIMENTAL_HIGH_RESOLUTION_TIME_METHODS :
181
+ enableExperimentalHighResolutionTimeMethods ? '1' : '0' ,
171
182
} ,
172
- ) ;
173
- if ( wizerProcess . status !== 0 ) {
174
- throw new Error ( `Wizer initialization failure` ) ;
183
+ } ;
184
+ if ( enableAOT ) {
185
+ const wevalBin = await weval ( ) ;
186
+
187
+ let wevalProcess = spawnSync (
188
+ `"${ wevalBin } "` ,
189
+ [
190
+ 'weval' ,
191
+ ...( aotCache ? [ `--cache-ro ${ aotCache } ` ] : [ ] ) ,
192
+ '--dir .' ,
193
+ `--dir ${ maybeWindowsPath ( dirname ( input ) ) } ` ,
194
+ '-w' ,
195
+ `-i "${ wasmEngine } "` ,
196
+ `-o "${ output } "` ,
197
+ ] ,
198
+ spawnOpts ,
199
+ ) ;
200
+ if ( wevalProcess . status !== 0 ) {
201
+ throw new Error ( `Weval initialization failure` ) ;
202
+ }
203
+ process . exitCode = wevalProcess . status ;
204
+ } else {
205
+ let wizerProcess = spawnSync (
206
+ `"${ wizer } "` ,
207
+ [
208
+ '--inherit-env=true' ,
209
+ '--allow-wasi' ,
210
+ '--dir=.' ,
211
+ `--dir=${ maybeWindowsPath ( dirname ( input ) ) } ` ,
212
+ '-r _start=wizer.resume' ,
213
+ `--wasm-bulk-memory=true` ,
214
+ `-o="${ output } "` ,
215
+ `"${ wasmEngine } "` ,
216
+ ] ,
217
+ spawnOpts ,
218
+ ) ;
219
+ if ( wizerProcess . status !== 0 ) {
220
+ throw new Error ( `Wizer initialization failure` ) ;
221
+ }
222
+ process . exitCode = wizerProcess . status ;
175
223
}
176
- process . exitCode = wizerProcess . status ;
177
224
}
178
225
} catch ( error ) {
179
226
console . error (
@@ -182,6 +229,8 @@ export async function compileApplicationToWasm(
182
229
) ;
183
230
process . exit ( 1 ) ;
184
231
} finally {
185
- rmSync ( tmpDir , { recursive : true } ) ;
232
+ if ( doBundle ) {
233
+ rmSync ( tmpDir , { recursive : true } ) ;
234
+ }
186
235
}
187
236
}
0 commit comments