@@ -156,7 +156,7 @@ impl<'a> Context<'a> {
156
156
self . globals . push_str ( c) ;
157
157
}
158
158
let global = match self . config . mode {
159
- OutputMode :: Node { module : false } => {
159
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
160
160
if contents. starts_with ( "class" ) {
161
161
format ! ( "{}\n module.exports.{1} = {1};\n " , contents, export_name)
162
162
} else {
@@ -305,6 +305,31 @@ impl<'a> Context<'a> {
305
305
reset_indentation ( & shim)
306
306
}
307
307
308
+ fn generate_inline_wasm_loading ( & mut self ) -> String {
309
+ let mut shim = String :: new ( ) ;
310
+
311
+ let buf = self . module . emit_wasm ( ) ;
312
+
313
+ let mut serialized = "const bytes = Buffer.from([" . to_string ( ) ;
314
+ let ( last, bytes) = buf. split_last ( ) . unwrap ( ) ;
315
+ for byte in bytes {
316
+ serialized. push_str ( & format ! ( "{}," , byte) ) ;
317
+ }
318
+ serialized. push_str ( & format ! ( "{}" , last) ) ;
319
+ serialized. push_str ( "]);" ) ;
320
+ shim. push_str ( & serialized) ;
321
+ shim. push_str (
322
+ "
323
+ const wasmModule = new WebAssembly.Module(bytes);
324
+ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
325
+ wasm = wasmInstance.exports;
326
+ module.exports.__wasm = wasm;
327
+ " ,
328
+ ) ;
329
+
330
+ reset_indentation ( & shim)
331
+ }
332
+
308
333
// generates something like
309
334
// ```js
310
335
// import * as import0 from './snippets/.../inline1.js';
@@ -533,6 +558,27 @@ __wbg_set_wasm(wasm);"
533
558
footer. push_str ( "export { initSync };\n " ) ;
534
559
footer. push_str ( "export default __wbg_init;" ) ;
535
560
}
561
+
562
+ OutputMode :: InlineNodeJs => {
563
+ js. push_str ( & self . generate_node_imports ( ) ) ;
564
+
565
+ js. push_str ( "let wasm;\n " ) ;
566
+
567
+ for ( id, js) in crate :: sorted_iter ( & self . wasm_import_definitions ) {
568
+ let import = self . module . imports . get_mut ( * id) ;
569
+ footer. push_str ( "\n module.exports." ) ;
570
+ footer. push_str ( & import. name ) ;
571
+ footer. push_str ( " = " ) ;
572
+ footer. push_str ( js. trim ( ) ) ;
573
+ footer. push_str ( ";\n " ) ;
574
+ }
575
+
576
+ footer. push_str ( & self . generate_inline_wasm_loading ( ) ) ;
577
+
578
+ if needs_manual_start {
579
+ footer. push_str ( "\n wasm.__wbindgen_start();\n " ) ;
580
+ }
581
+ }
536
582
}
537
583
538
584
// Before putting the static init code declaration info, put all existing typescript into a `wasm_bindgen` namespace declaration.
@@ -601,7 +647,7 @@ __wbg_set_wasm(wasm);"
601
647
}
602
648
}
603
649
604
- OutputMode :: Node { module : false } => {
650
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
605
651
for ( module, items) in crate :: sorted_iter ( & self . js_imports ) {
606
652
imports. push_str ( "const { " ) ;
607
653
for ( i, ( item, rename) ) in items. iter ( ) . enumerate ( ) {
@@ -1076,7 +1122,7 @@ __wbg_set_wasm(wasm);"
1076
1122
*/\n toString(): string;\n ",
1077
1123
) ;
1078
1124
1079
- if self . config . mode . nodejs ( ) {
1125
+ if self . config . mode . nodejs ( ) || self . config . mode . inline_nodejs ( ) {
1080
1126
// `util.inspect` must be imported in Node.js to define [inspect.custom]
1081
1127
let module_name = self . import_name ( & JsImport {
1082
1128
name : JsImportName :: Module {
@@ -1573,7 +1619,8 @@ __wbg_set_wasm(wasm);"
1573
1619
init : Option < & str > ,
1574
1620
) -> Result < ( ) , Error > {
1575
1621
match & self . config . mode {
1576
- OutputMode :: Node { .. } => {
1622
+
1623
+ OutputMode :: InlineNodeJs | OutputMode :: Node { .. } => {
1577
1624
let name = self . import_name ( & JsImport {
1578
1625
name : JsImportName :: Module {
1579
1626
module : "util" . to_string ( ) ,
@@ -1606,6 +1653,7 @@ __wbg_set_wasm(wasm);"
1606
1653
if let Some ( init) = init {
1607
1654
match & self . config . mode {
1608
1655
OutputMode :: Node { .. }
1656
+ | OutputMode :: InlineNodeJs
1609
1657
| OutputMode :: Bundler {
1610
1658
browser_only : false ,
1611
1659
} => self . global ( init) ,
@@ -3290,7 +3338,7 @@ __wbg_set_wasm(wasm);"
3290
3338
| OutputMode :: Bundler { .. }
3291
3339
| OutputMode :: Deno
3292
3340
| OutputMode :: Node { module : true } => "import.meta.url" ,
3293
- OutputMode :: Node { module : false } => {
3341
+ OutputMode :: InlineNodeJs | OutputMode :: Node { module : false } => {
3294
3342
"require('url').pathToFileURL(__filename)"
3295
3343
}
3296
3344
OutputMode :: NoModules { .. } => {
0 commit comments