Skip to content

Commit 835913c

Browse files
committed
feat: add experimental inline node target
1 parent 76776ef commit 835913c

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

crates/cli-support/src/js/mod.rs

+53-5
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a> Context<'a> {
156156
self.globals.push_str(c);
157157
}
158158
let global = match self.config.mode {
159-
OutputMode::Node { module: false } => {
159+
OutputMode::InlineNodeJs | OutputMode::Node { module: false } => {
160160
if contents.starts_with("class") {
161161
format!("{}\nmodule.exports.{1} = {1};\n", contents, export_name)
162162
} else {
@@ -305,6 +305,31 @@ impl<'a> Context<'a> {
305305
reset_indentation(&shim)
306306
}
307307

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+
308333
// generates something like
309334
// ```js
310335
// import * as import0 from './snippets/.../inline1.js';
@@ -533,6 +558,27 @@ __wbg_set_wasm(wasm);"
533558
footer.push_str("export { initSync };\n");
534559
footer.push_str("export default __wbg_init;");
535560
}
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("\nmodule.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("\nwasm.__wbindgen_start();\n");
580+
}
581+
}
536582
}
537583

538584
// 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);"
601647
}
602648
}
603649

604-
OutputMode::Node { module: false } => {
650+
OutputMode::InlineNodeJs | OutputMode::Node { module: false } => {
605651
for (module, items) in crate::sorted_iter(&self.js_imports) {
606652
imports.push_str("const { ");
607653
for (i, (item, rename)) in items.iter().enumerate() {
@@ -1076,7 +1122,7 @@ __wbg_set_wasm(wasm);"
10761122
*/\n toString(): string;\n",
10771123
);
10781124

1079-
if self.config.mode.nodejs() {
1125+
if self.config.mode.nodejs() || self.config.mode.inline_nodejs() {
10801126
// `util.inspect` must be imported in Node.js to define [inspect.custom]
10811127
let module_name = self.import_name(&JsImport {
10821128
name: JsImportName::Module {
@@ -1573,7 +1619,8 @@ __wbg_set_wasm(wasm);"
15731619
init: Option<&str>,
15741620
) -> Result<(), Error> {
15751621
match &self.config.mode {
1576-
OutputMode::Node { .. } => {
1622+
1623+
OutputMode::InlineNodeJs | OutputMode::Node { .. } => {
15771624
let name = self.import_name(&JsImport {
15781625
name: JsImportName::Module {
15791626
module: "util".to_string(),
@@ -1606,6 +1653,7 @@ __wbg_set_wasm(wasm);"
16061653
if let Some(init) = init {
16071654
match &self.config.mode {
16081655
OutputMode::Node { .. }
1656+
| OutputMode::InlineNodeJs
16091657
| OutputMode::Bundler {
16101658
browser_only: false,
16111659
} => self.global(init),
@@ -3290,7 +3338,7 @@ __wbg_set_wasm(wasm);"
32903338
| OutputMode::Bundler { .. }
32913339
| OutputMode::Deno
32923340
| OutputMode::Node { module: true } => "import.meta.url",
3293-
OutputMode::Node { module: false } => {
3341+
OutputMode::InlineNodeJs | OutputMode::Node { module: false } => {
32943342
"require('url').pathToFileURL(__filename)"
32953343
}
32963344
OutputMode::NoModules { .. } => {

crates/cli-support/src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ enum OutputMode {
6969
NoModules { global: String },
7070
Node { module: bool },
7171
Deno,
72+
InlineNodeJs,
7273
}
7374

7475
enum Input {
@@ -172,6 +173,16 @@ impl Bindgen {
172173
Ok(self)
173174
}
174175

176+
pub fn inline_nodejs(&mut self, inline_nodejs: bool) -> Result<&mut Bindgen, Error> {
177+
if inline_nodejs {
178+
self.switch_mode(
179+
OutputMode::InlineNodeJs,
180+
"--target experimental-inline-nodejs",
181+
)?;
182+
}
183+
Ok(self)
184+
}
185+
175186
pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> {
176187
if bundler {
177188
self.switch_mode(
@@ -565,6 +576,10 @@ impl OutputMode {
565576
matches!(self, OutputMode::Node { .. })
566577
}
567578

579+
fn inline_nodejs(&self) -> bool {
580+
matches!(self, OutputMode::InlineNodeJs)
581+
}
582+
568583
fn no_modules(&self) -> bool {
569584
matches!(self, OutputMode::NoModules { .. })
570585
}

crates/cli/src/bin/wasm-bindgen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
110110
"nodejs" => b.nodejs(true)?,
111111
"deno" => b.deno(true)?,
112112
"experimental-nodejs-module" => b.nodejs_module(true)?,
113+
"experimental-inline-nodejs" => b.inline_nodejs(true)?,
113114
s => bail!("invalid encode-into mode: `{}`", s),
114115
};
115116
}

0 commit comments

Comments
 (0)