Skip to content

Commit

Permalink
Adding namespace exports option to transpile cmd
Browse files Browse the repository at this point in the history
Also adding a test for the `--no-namespaced-exports` option
  • Loading branch information
landonxjames committed Nov 19, 2023
1 parent 519d261 commit c7bc0bd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
12 changes: 7 additions & 5 deletions crates/js-component-bindgen/src/esm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ impl EsmBindgen {
for (alias, export_name) in &self.export_aliases {
if first {
first = false
} else {
uwrite!(output, ", ");
}
let local_name = match &self.exports[export_name] {
Binding::Local(local_name) => local_name,
Expand All @@ -176,17 +174,18 @@ impl EsmBindgen {
let alias_maybe_quoted = maybe_quote_id(alias);
if local_name == alias_maybe_quoted {
output.push_str(local_name);
uwrite!(output, ", ");
} else if instantiation {
uwrite!(output, "{alias_maybe_quoted}: {local_name}");
} else {
uwrite!(output, ", ");
} else if !opts.no_namespaced_exports {
uwrite!(output, "{local_name} as {alias_maybe_quoted}");
uwrite!(output, ", ");
}
}
for (export_name, export) in &self.exports {
if first {
first = false
} else {
uwrite!(output, ", ");
}
let local_name = match export {
Binding::Local(local_name) => local_name,
Expand All @@ -195,10 +194,13 @@ impl EsmBindgen {
let export_name_maybe_quoted = maybe_quote_id(export_name);
if local_name == export_name_maybe_quoted {
output.push_str(local_name);
uwrite!(output, ", ");
} else if instantiation {
uwrite!(output, "{export_name_maybe_quoted}: {local_name}");
uwrite!(output, ", ");
} else if !opts.no_namespaced_exports {
uwrite!(output, "{local_name} as {export_name_maybe_quoted}");
uwrite!(output, ", ");
}
}
uwrite!(output, " }}");
Expand Down
4 changes: 3 additions & 1 deletion src/cmd/transpile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async function wasm2Js (source) {
* js?: bool,
* minify?: bool,
* optimize?: bool,
* noNamespacedExports?: bool,
* optArgs?: string[],
* }} opts
* @returns {Promise<{ files: { [filename: string]: Uint8Array }, imports: string[], exports: [string, 'function' | 'instance'][] }>}
Expand Down Expand Up @@ -112,7 +113,8 @@ export async function transpileComponent (component, opts = {}) {
noNodejsCompat: !(opts.nodejsCompat ?? true),
noTypescript: opts.noTypescript || false,
tlaCompat: opts.tlaCompat ?? false,
base64Cutoff: opts.js ? 0 : opts.base64Cutoff ?? 5000
base64Cutoff: opts.js ? 0 : opts.base64Cutoff ?? 5000,
noNamespacedExports: !opts.namespacedExports,
});

let outDir = (opts.outDir ?? '').replace(/\\/g, '/');
Expand Down
1 change: 1 addition & 0 deletions src/jco.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ program.command('transpile')
.option('--js', 'output JS instead of core WebAssembly')
.option('-I, --instantiation', 'output for custom module instantiation')
.option('-q, --quiet', 'disable logging')
.option('--no-namespaced-exports', 'disable namespaced exports for typescript compatibility')
.option('--', 'for --optimize, custom wasm-opt arguments (defaults to best size optimization)')
.action(asyncAction(transpile));

Expand Down
8 changes: 8 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export async function cliTest (fixtures) {
ok(source.includes('export const $init'));
});

test('Transpile no namespaced exports', async () => {
const name = 'flavorful';
const { stderr, stdout } = await exec(jcoPath, 'transpile', `test/fixtures/components/${name}.component.wasm`, '--no-namespaced-exports', '--no-wasi-shim', '--name', name, '-o', outDir);
strictEqual(stderr, '');
const source = await readFile(`${outDir}/${name}.js`);
ok(!source.toString().includes("test as 'test:flavorful/test'"));
});

test('Optimize', async () => {
const component = await readFile(`test/fixtures/components/flavorful.component.wasm`);
const { stderr, stdout } = await exec(jcoPath, 'opt', `test/fixtures/components/flavorful.component.wasm`, '-o', outFile);
Expand Down
8 changes: 0 additions & 8 deletions xtask/src/build/jco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ pub(crate) fn run() -> Result<()> {
)?;

let sh = Shell::new()?;
//Typescript doesn't support arbitrary module namespace identifier names so
//we need to remove it from wasm-tools.js before compiling
//See: https://github.com/microsoft/TypeScript/issues/40594
cmd!(
sh,
"sed -i'' -e 's/tools as \"local:wasm-tools\\/tools\"//g' ./obj/wasm-tools.js"
)
.read()?;
cmd!(sh, "npx tsc -p tsconfig.json").read()?;

Ok(())
Expand Down

0 comments on commit c7bc0bd

Please sign in to comment.