@@ -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 ( ) {
@@ -1037,7 +1088,7 @@ impl<'a> Context<'a> {
1037
1088
*/\n toString(): string;\n ",
1038
1089
) ;
1039
1090
1040
- if self . config . mode . nodejs ( ) {
1091
+ if self . config . mode . nodejs ( ) || self . config . mode . inline_nodejs ( ) {
1041
1092
// `util.inspect` must be imported in Node.js to define [inspect.custom]
1042
1093
let module_name = self . import_name ( & JsImport {
1043
1094
name : JsImportName :: Module {
@@ -1516,7 +1567,8 @@ impl<'a> Context<'a> {
1516
1567
init : Option < & str > ,
1517
1568
) -> Result < ( ) , Error > {
1518
1569
match & self . config . mode {
1519
- OutputMode :: Node { .. } => {
1570
+
1571
+ OutputMode :: InlineNodeJs | OutputMode :: Node { .. } => {
1520
1572
let name = self . import_name ( & JsImport {
1521
1573
name : JsImportName :: Module {
1522
1574
module : "util" . to_string ( ) ,
@@ -1549,6 +1601,7 @@ impl<'a> Context<'a> {
1549
1601
if let Some ( init) = init {
1550
1602
match & self . config . mode {
1551
1603
OutputMode :: Node { .. }
1604
+ | OutputMode :: InlineNodeJs
1552
1605
| OutputMode :: Bundler {
1553
1606
browser_only : false ,
1554
1607
} => self . global ( init) ,
@@ -3238,7 +3291,7 @@ impl<'a> Context<'a> {
3238
3291
| OutputMode :: Bundler { .. }
3239
3292
| OutputMode :: Deno
3240
3293
| OutputMode :: Node { module : true } => "import.meta.url" ,
3241
- OutputMode :: Node { module : false } => {
3294
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
3242
3295
"require('url').pathToFileURL(__filename)"
3243
3296
}
3244
3297
OutputMode :: NoModules { .. } => {
0 commit comments