mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 20:41:24 +00:00
Rewrite wasm-bindgen with ES6 modules in mind
This commit is a mostly-rewrite of the `wasm-bindgen` tool. After some recent discussions it's clear that the previous model wasn't quite going to cut it, and this iteration is one which primarily embraces ES6 modules and the idea that this is a polyfill for host bindings. The overall interface and functionality hasn't changed much but the underlying technology has now changed significantly. Previously `wasm-bindgen` would emit a JS file that acted as an ES6 module but had a bit of a wonky interface. It exposed an async function for instantiation of the wasm module, but that's the bundler's job, not ours! Instead this iteration views each input and output as a discrete ES6 module. The input wasm file is interpreted as "this *should* be an ES6 module with rich types" and the output is "well here's some ES6 modules that fulfill that contract". Notably the tool now replaces the original wasm ES6 module with a JS ES6 module that has the "rich interface". Additionally a second ES6 module is emitted (the actual wasm file) which imports and exports to the original ES6 module. This strategy is hoped to be much more amenable to bundlers and controlling how the wasm itself is instantiated. The emitted files files purely assume ES6 modules and should be able to work as-is once ES6 module integration for wasm is completed. Note that there aren't a ton of tools to pretend a wasm module is an ES6 module at the moment but those should be coming soon! In the meantime a local `wasm2es6js` hack was added to help make *something* work today. The README has also been updated with instructions for interacting with this model.
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
extern crate wasm_bindgen_cli_support as cli;
|
||||
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Write, Read};
|
||||
use std::path::{PathBuf, Path};
|
||||
use std::process::Command;
|
||||
@ -15,7 +15,6 @@ thread_local!(static IDX: usize = CNT.fetch_add(1, Ordering::SeqCst));
|
||||
pub struct Project {
|
||||
files: Vec<(String, String)>,
|
||||
debug: bool,
|
||||
uglify: bool,
|
||||
js: bool,
|
||||
}
|
||||
|
||||
@ -29,7 +28,6 @@ pub fn project() -> Project {
|
||||
.read_to_string(&mut lockfile).unwrap();
|
||||
Project {
|
||||
debug: true,
|
||||
uglify: false,
|
||||
js: false,
|
||||
files: vec![
|
||||
("Cargo.toml".to_string(), format!(r#"
|
||||
@ -54,23 +52,53 @@ pub fn project() -> Project {
|
||||
("Cargo.lock".to_string(), lockfile),
|
||||
|
||||
("run.ts".to_string(), r#"
|
||||
import * as fs from "fs";
|
||||
import * as process from "process";
|
||||
|
||||
import { instantiate } from "./out";
|
||||
import * as out from "./out_wasm";
|
||||
import * as test from "./test";
|
||||
|
||||
var wasm = fs.readFileSync("out.wasm");
|
||||
|
||||
instantiate(wasm, test.imports).then(m => {
|
||||
test.test(m);
|
||||
if ((m as any).assertHeapAndStackEmpty)
|
||||
(m as any).assertHeapAndStackEmpty();
|
||||
out.booted.then(() => {
|
||||
test.test();
|
||||
if ((out as any).assertHeapAndStackEmpty)
|
||||
(out as any).assertHeapAndStackEmpty();
|
||||
}).catch(error => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
"#.to_string()),
|
||||
|
||||
("rollup.config.js".to_string(), r#"
|
||||
import typescript from 'rollup-plugin-typescript2';
|
||||
|
||||
export default {
|
||||
input: './run.ts',
|
||||
|
||||
plugins: [
|
||||
typescript()
|
||||
],
|
||||
output: {
|
||||
file: 'bundle.js',
|
||||
format: 'cjs'
|
||||
}
|
||||
}
|
||||
"#.to_string()),
|
||||
|
||||
("tsconfig.json".to_string(), r#"
|
||||
{
|
||||
"compilerOptions": {
|
||||
"noEmitOnError": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"noUnusedParameters": true,
|
||||
"noUnusedLocals": true,
|
||||
"noImplicitReturns": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"alwaysStrict": true,
|
||||
"strict": true
|
||||
}
|
||||
}
|
||||
"#.to_string()),
|
||||
],
|
||||
}
|
||||
}
|
||||
@ -87,7 +115,7 @@ pub fn root() -> PathBuf {
|
||||
return me
|
||||
}
|
||||
|
||||
fn typescript() -> PathBuf {
|
||||
fn rollup() -> PathBuf {
|
||||
static INIT: Once = ONCE_INIT;
|
||||
|
||||
let mut me = env::current_exe().unwrap();
|
||||
@ -95,7 +123,7 @@ fn typescript() -> PathBuf {
|
||||
me.pop(); // chop off `deps`
|
||||
me.pop(); // chop off `debug` / `release`
|
||||
let install_dir = me.clone();
|
||||
me.push("node_modules/typescript/bin/tsc");
|
||||
me.push("node_modules/.bin/rollup");
|
||||
|
||||
INIT.call_once(|| {
|
||||
if !me.exists() {
|
||||
@ -108,9 +136,11 @@ fn typescript() -> PathBuf {
|
||||
};
|
||||
run(npm
|
||||
.arg("install")
|
||||
.arg("rollup")
|
||||
.arg("rollup-plugin-typescript2")
|
||||
.arg("typescript")
|
||||
.arg("@types/node")
|
||||
.arg("@types/webassembly-js-api")
|
||||
//.arg("@types/webassembly-js-api")
|
||||
.current_dir(&install_dir), "npm");
|
||||
assert!(me.exists());
|
||||
}
|
||||
@ -130,11 +160,6 @@ impl Project {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn uglify(&mut self, uglify: bool) -> &mut Project {
|
||||
self.uglify = uglify;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn js(&mut self, js: bool) -> &mut Project {
|
||||
self.js = js;
|
||||
self
|
||||
@ -170,48 +195,37 @@ impl Project {
|
||||
run(&mut cmd, "wasm-gc");
|
||||
}
|
||||
|
||||
let obj = cli::Bindgen::new()
|
||||
.input_path(&out)
|
||||
let as_a_module = root.join("out.wasm");
|
||||
fs::copy(&out, &as_a_module).unwrap();
|
||||
|
||||
cli::Bindgen::new()
|
||||
.input_path(&as_a_module)
|
||||
.nodejs(true)
|
||||
.typescript(true)
|
||||
.debug(self.debug)
|
||||
.uglify_wasm_names(self.uglify)
|
||||
.generate()
|
||||
.generate(&root)
|
||||
.expect("failed to run bindgen");
|
||||
if self.js {
|
||||
obj.write_js_to(root.join("out.js")).expect("failed to write js");
|
||||
} else {
|
||||
obj.write_ts_to(root.join("out.ts")).expect("failed to write ts");
|
||||
}
|
||||
obj.write_wasm_to(root.join("out.wasm")).expect("failed to write wasm");
|
||||
let out_dir = if self.js {
|
||||
root.join("out")
|
||||
} else {
|
||||
root.clone()
|
||||
};
|
||||
|
||||
let mut wasm = Vec::new();
|
||||
File::open(root.join("out_wasm.wasm")).unwrap()
|
||||
.read_to_end(&mut wasm).unwrap();
|
||||
let obj = cli::wasm2es6js::Config::new()
|
||||
.base64(true)
|
||||
.generate(&wasm)
|
||||
.expect("failed to convert wasm to js");
|
||||
File::create(root.join("out_wasm.d.ts")).unwrap()
|
||||
.write_all(obj.typescript().as_bytes()).unwrap();
|
||||
File::create(root.join("out_wasm.js")).unwrap()
|
||||
.write_all(obj.js().as_bytes()).unwrap();
|
||||
|
||||
let mut cmd = Command::new("node");
|
||||
cmd.arg(typescript())
|
||||
.current_dir(&target_dir)
|
||||
.arg(root.join("run.ts"))
|
||||
.arg("--noUnusedLocals")
|
||||
.arg("--noUnusedParameters")
|
||||
.arg("--noImplicitReturns")
|
||||
.arg("--lib")
|
||||
.arg("es6")
|
||||
.arg("--outDir").arg(&out_dir);
|
||||
if self.js {
|
||||
cmd.arg("--allowJs");
|
||||
} else {
|
||||
cmd.arg("--noImplicitAny")
|
||||
.arg("--strict")
|
||||
.arg("--strictNullChecks")
|
||||
.arg("--declaration")
|
||||
.arg("--strictFunctionTypes");
|
||||
}
|
||||
cmd.arg(rollup())
|
||||
.current_dir(&root)
|
||||
.arg("-c");
|
||||
run(&mut cmd, "node");
|
||||
|
||||
let mut cmd = Command::new("node");
|
||||
cmd.arg(out_dir.join("run.js"))
|
||||
cmd.arg(root.join("bundle.js"))
|
||||
.current_dir(&root);
|
||||
run(&mut cmd, "node");
|
||||
}
|
||||
|
Reference in New Issue
Block a user