@@ -142,9 +142,7 @@ impl<'a> Context<'a> {
142
142
self . globals . push_str ( c) ;
143
143
}
144
144
let global = match self . config . mode {
145
- OutputMode :: Node {
146
- experimental_modules : false ,
147
- } => {
145
+ OutputMode :: Node { module : false } => {
148
146
if contents. starts_with ( "class" ) {
149
147
format ! ( "{}\n module.exports.{1} = {1};\n " , contents, export_name)
150
148
} else {
@@ -159,9 +157,7 @@ impl<'a> Context<'a> {
159
157
}
160
158
}
161
159
OutputMode :: Bundler { .. }
162
- | OutputMode :: Node {
163
- experimental_modules : true ,
164
- }
160
+ | OutputMode :: Node { module : true }
165
161
| OutputMode :: Web
166
162
| OutputMode :: Deno => {
167
163
if let Some ( body) = contents. strip_prefix ( "function" ) {
@@ -217,26 +213,29 @@ impl<'a> Context<'a> {
217
213
218
214
let mut shim = String :: new ( ) ;
219
215
220
- shim. push_str ( "let imports = {};\n " ) ;
216
+ shim. push_str ( "\n let imports = {};\n " ) ;
221
217
222
- if self . config . mode . nodejs_experimental_modules ( ) {
218
+ if self . config . mode . uses_es_modules ( ) {
223
219
for ( i, module) in imports. iter ( ) . enumerate ( ) {
224
220
if module. as_str ( ) != PLACEHOLDER_MODULE {
225
221
shim. push_str ( & format ! ( "import * as import{} from '{}';\n " , i, module) ) ;
226
222
}
227
223
}
228
- }
229
-
230
- for ( i, module) in imports. iter ( ) . enumerate ( ) {
231
- if module. as_str ( ) == PLACEHOLDER_MODULE {
232
- shim. push_str ( & format ! (
233
- "imports['{0}'] = module.exports;\n " ,
234
- PLACEHOLDER_MODULE
235
- ) ) ;
236
- } else if self . config . mode . nodejs_experimental_modules ( ) {
237
- shim. push_str ( & format ! ( "imports['{}'] = import{};\n " , module, i) ) ;
238
- } else {
239
- shim. push_str ( & format ! ( "imports['{0}'] = require('{0}');\n " , module) ) ;
224
+ for ( i, module) in imports. iter ( ) . enumerate ( ) {
225
+ if module. as_str ( ) != PLACEHOLDER_MODULE {
226
+ shim. push_str ( & format ! ( "imports['{}'] = import{};\n " , module, i) ) ;
227
+ }
228
+ }
229
+ } else {
230
+ for module in imports. iter ( ) {
231
+ if module. as_str ( ) == PLACEHOLDER_MODULE {
232
+ shim. push_str ( & format ! (
233
+ "imports['{0}'] = module.exports;\n " ,
234
+ PLACEHOLDER_MODULE
235
+ ) ) ;
236
+ } else {
237
+ shim. push_str ( & format ! ( "imports['{0}'] = require('{0}');\n " , module) ) ;
238
+ }
240
239
}
241
240
}
242
241
@@ -246,24 +245,31 @@ impl<'a> Context<'a> {
246
245
fn generate_node_wasm_loading ( & self , path : & Path ) -> String {
247
246
let mut shim = String :: new ( ) ;
248
247
249
- if self . config . mode . nodejs_experimental_modules ( ) {
248
+ if self . config . mode . uses_es_modules ( ) {
250
249
// On windows skip the leading `/` which comes out when we parse a
251
250
// url to use `C:\...` instead of `\C:\...`
252
251
shim. push_str ( & format ! (
253
252
"
254
- import * as path from 'path';
255
- import * as fs from 'fs';
256
- import * as url from 'url';
257
- import * as process from 'process';
253
+ import * as path from 'node:path';
254
+ import * as fs from 'node:fs';
255
+ import * as process from 'node:process';
258
256
259
- let file = path.dirname(url.parse (import.meta.url).pathname);
257
+ let file = path.dirname(new URL (import.meta.url).pathname);
260
258
if (process.platform === 'win32') {{
261
259
file = file.substring(1);
262
260
}}
263
261
const bytes = fs.readFileSync(path.join(file, '{}'));
264
262
" ,
265
263
path. file_name( ) . unwrap( ) . to_str( ) . unwrap( )
266
264
) ) ;
265
+ shim. push_str (
266
+ "
267
+ const wasmModule = new WebAssembly.Module(bytes);
268
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
269
+ const wasm = wasmInstance.exports;
270
+ export const __wasm = wasm;
271
+ " ,
272
+ ) ;
267
273
} else {
268
274
shim. push_str ( & format ! (
269
275
"
@@ -272,17 +278,16 @@ impl<'a> Context<'a> {
272
278
" ,
273
279
path. file_name( ) . unwrap( ) . to_str( ) . unwrap( )
274
280
) ) ;
281
+ shim. push_str (
282
+ "
283
+ const wasmModule = new WebAssembly.Module(bytes);
284
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
285
+ wasm = wasmInstance.exports;
286
+ module.exports.__wasm = wasm;
287
+ " ,
288
+ ) ;
275
289
}
276
290
277
- shim. push_str (
278
- "
279
- const wasmModule = new WebAssembly.Module(bytes);
280
- const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
281
- wasm = wasmInstance.exports;
282
- module.exports.__wasm = wasm;
283
- " ,
284
- ) ;
285
-
286
291
reset_indentation ( & shim)
287
292
}
288
293
@@ -400,9 +405,7 @@ impl<'a> Context<'a> {
400
405
401
406
// With normal CommonJS node we need to defer requiring the wasm
402
407
// until the end so most of our own exports are hooked up
403
- OutputMode :: Node {
404
- experimental_modules : false ,
405
- } => {
408
+ OutputMode :: Node { module : false } => {
406
409
js. push_str ( & self . generate_node_imports ( ) ) ;
407
410
408
411
js. push_str ( "let wasm;\n " ) ;
@@ -442,13 +445,10 @@ impl<'a> Context<'a> {
442
445
}
443
446
}
444
447
445
- // With Bundlers and modern ES6 support in Node we can simply import
446
- // the wasm file as if it were an ES module and let the
447
- // bundler/runtime take care of it.
448
- OutputMode :: Bundler { .. }
449
- | OutputMode :: Node {
450
- experimental_modules : true ,
451
- } => {
448
+ // With Bundlers we can simply import the wasm file as if it were an ES module
449
+ // and let the bundler/runtime take care of it.
450
+ // With Node we manually read the wasm file from the filesystem and instantiate it.
451
+ OutputMode :: Bundler { .. } | OutputMode :: Node { module : true } => {
452
452
for ( id, js) in crate :: sorted_iter ( & self . wasm_import_definitions ) {
453
453
let import = self . module . imports . get_mut ( * id) ;
454
454
import. module = format ! ( "./{}_bg.js" , module_name) ;
@@ -475,8 +475,18 @@ impl<'a> Context<'a> {
475
475
" ,
476
476
) ;
477
477
478
+ if matches ! ( self . config. mode, OutputMode :: Node { module: true } ) {
479
+ let start = start. get_or_insert_with ( String :: new) ;
480
+ start. push_str ( & self . generate_node_imports ( ) ) ;
481
+ start. push_str ( & self . generate_node_wasm_loading ( Path :: new ( & format ! (
482
+ "./{}_bg.wasm" ,
483
+ module_name
484
+ ) ) ) ) ;
485
+ }
478
486
if needs_manual_start {
479
- start = Some ( "\n wasm.__wbindgen_start();\n " . to_string ( ) ) ;
487
+ start
488
+ . get_or_insert_with ( String :: new)
489
+ . push_str ( "\n wasm.__wbindgen_start();\n " ) ;
480
490
}
481
491
}
482
492
@@ -509,7 +519,9 @@ impl<'a> Context<'a> {
509
519
// Emit all the JS for importing all our functionality
510
520
assert ! (
511
521
!self . config. mode. uses_es_modules( ) || js. is_empty( ) ,
512
- "ES modules require imports to be at the start of the file"
522
+ "ES modules require imports to be at the start of the file, but we \
523
+ generated some JS before the imports: {}",
524
+ js
513
525
) ;
514
526
515
527
let mut push_with_newline = |s| {
@@ -556,9 +568,7 @@ impl<'a> Context<'a> {
556
568
}
557
569
}
558
570
559
- OutputMode :: Node {
560
- experimental_modules : false ,
561
- } => {
571
+ OutputMode :: Node { module : false } => {
562
572
for ( module, items) in crate :: sorted_iter ( & self . js_imports ) {
563
573
imports. push_str ( "const { " ) ;
564
574
for ( i, ( item, rename) ) in items. iter ( ) . enumerate ( ) {
@@ -582,9 +592,7 @@ impl<'a> Context<'a> {
582
592
}
583
593
584
594
OutputMode :: Bundler { .. }
585
- | OutputMode :: Node {
586
- experimental_modules : true ,
587
- }
595
+ | OutputMode :: Node { module : true }
588
596
| OutputMode :: Web
589
597
| OutputMode :: Deno => {
590
598
for ( module, items) in crate :: sorted_iter ( & self . js_imports ) {
@@ -3216,12 +3224,10 @@ impl<'a> Context<'a> {
3216
3224
OutputMode :: Web
3217
3225
| OutputMode :: Bundler { .. }
3218
3226
| OutputMode :: Deno
3219
- | OutputMode :: Node {
3220
- experimental_modules : true ,
3221
- } => "import.meta.url" ,
3222
- OutputMode :: Node {
3223
- experimental_modules : false ,
3224
- } => "require('url').pathToFileURL(__filename)" ,
3227
+ | OutputMode :: Node { module : true } => "import.meta.url" ,
3228
+ OutputMode :: Node { module : false } => {
3229
+ "require('url').pathToFileURL(__filename)"
3230
+ }
3225
3231
OutputMode :: NoModules { .. } => {
3226
3232
prelude. push_str (
3227
3233
"if (script_src === undefined) {
0 commit comments