@@ -142,7 +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 { module : false } => {
145
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
146
146
if contents. starts_with ( "class" ) {
147
147
format ! ( "{}\n module.exports.{1} = {1};\n " , contents, export_name)
148
148
} else {
@@ -291,6 +291,31 @@ impl<'a> Context<'a> {
291
291
reset_indentation ( & shim)
292
292
}
293
293
294
+ fn generate_isomorphic_wasm_loading ( & mut self ) -> String {
295
+ let mut shim = String :: new ( ) ;
296
+
297
+ let buf = self . module . emit_wasm ( ) ;
298
+
299
+ let mut serialized = "const bytes = Buffer.from([" . to_string ( ) ;
300
+ let ( last, bytes) = buf. split_last ( ) . unwrap ( ) ;
301
+ for byte in bytes {
302
+ serialized. push_str ( & format ! ( "{}," , byte) ) ;
303
+ }
304
+ serialized. push_str ( & format ! ( "{}" , last) ) ;
305
+ serialized. push_str ( "]);" ) ;
306
+ shim. push_str ( & serialized) ;
307
+ shim. push_str (
308
+ "
309
+ const wasmModule = new WebAssembly.Module(bytes);
310
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
311
+ wasm = wasmInstance.exports;
312
+ module.exports.__wasm = wasm;
313
+ " ,
314
+ ) ;
315
+
316
+ reset_indentation ( & shim)
317
+ }
318
+
294
319
// generates something like
295
320
// ```js
296
321
// import * as import0 from './snippets/.../inline1.js';
@@ -500,6 +525,32 @@ impl<'a> Context<'a> {
500
525
footer. push_str ( "export { initSync };\n " ) ;
501
526
footer. push_str ( "export default __wbg_init;" ) ;
502
527
}
528
+
529
+ OutputMode :: InlineNodeJs => {
530
+ js. push_str ( & self . generate_node_imports ( ) ) ;
531
+
532
+ js. push_str ( "let wasm;\n " ) ;
533
+
534
+ for ( id, js) in crate :: sorted_iter ( & self . wasm_import_definitions ) {
535
+ let import = self . module . imports . get_mut ( * id) ;
536
+ footer. push_str ( "\n module.exports." ) ;
537
+ footer. push_str ( & import. name ) ;
538
+ footer. push_str ( " = " ) ;
539
+ footer. push_str ( js. trim ( ) ) ;
540
+ footer. push_str ( ";\n " ) ;
541
+ }
542
+
543
+ footer. push_str (
544
+ & self . generate_node_wasm_loading ( Path :: new ( & format ! (
545
+ "./{}_bg.wasm" ,
546
+ module_name
547
+ ) ) ) ,
548
+ ) ;
549
+
550
+ if needs_manual_start {
551
+ footer. push_str ( "\n wasm.__wbindgen_start();\n " ) ;
552
+ }
553
+ }
503
554
}
504
555
505
556
// Before putting the static init code declaration info, put all existing typescript into a `wasm_bindgen` namespace declaration.
@@ -568,7 +619,7 @@ impl<'a> Context<'a> {
568
619
}
569
620
}
570
621
571
- OutputMode :: Node { module : false } => {
622
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
572
623
for ( module, items) in crate :: sorted_iter ( & self . js_imports ) {
573
624
imports. push_str ( "const { " ) ;
574
625
for ( i, ( item, rename) ) in items. iter ( ) . enumerate ( ) {
@@ -1043,7 +1094,7 @@ impl<'a> Context<'a> {
1043
1094
*/\n toString(): string;\n ",
1044
1095
) ;
1045
1096
1046
- if self . config . mode . nodejs ( ) {
1097
+ if self . config . mode . nodejs ( ) || self . config . mode . inline_nodejs ( ) {
1047
1098
// `util.inspect` must be imported in Node.js to define [inspect.custom]
1048
1099
let module_name = self . import_name ( & JsImport {
1049
1100
name : JsImportName :: Module {
@@ -1522,7 +1573,8 @@ impl<'a> Context<'a> {
1522
1573
init : Option < & str > ,
1523
1574
) -> Result < ( ) , Error > {
1524
1575
match & self . config . mode {
1525
- OutputMode :: Node { .. } => {
1576
+
1577
+ OutputMode :: InlineNodeJs | OutputMode :: Node { .. } => {
1526
1578
let name = self . import_name ( & JsImport {
1527
1579
name : JsImportName :: Module {
1528
1580
module : "util" . to_string ( ) ,
@@ -1555,6 +1607,7 @@ impl<'a> Context<'a> {
1555
1607
if let Some ( init) = init {
1556
1608
match & self . config . mode {
1557
1609
OutputMode :: Node { .. }
1610
+ | OutputMode :: InlineNodeJs
1558
1611
| OutputMode :: Bundler {
1559
1612
browser_only : false ,
1560
1613
} => self . global ( init) ,
@@ -3244,7 +3297,7 @@ impl<'a> Context<'a> {
3244
3297
| OutputMode :: Bundler { .. }
3245
3298
| OutputMode :: Deno
3246
3299
| OutputMode :: Node { module : true } => "import.meta.url" ,
3247
- OutputMode :: Node { module : false } => {
3300
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
3248
3301
"require('url').pathToFileURL(__filename)"
3249
3302
}
3250
3303
OutputMode :: NoModules { .. } => {
0 commit comments