Skip to content

Commit

Permalink
fix: use hashed name to escape paths
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 29, 2023
1 parent 633f69a commit 7446cbf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
29 changes: 13 additions & 16 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,26 @@ const unplugin = createUnplugin<UnwasmPluginOptions>((opts) => {
return;
}

const source = await fs.readFile(id);
const name = `wasm/${basename(id, ".wasm")}-${sha1(source)}.wasm`;
const parsed = parse(name, source);
const buff = await fs.readFile(id);
return buff.toString('binary');
},
transform(code, id) {
if (!id.endsWith(".wasm")) {
return;
}

const buff = Buffer.from(code, 'binary')
const name = `wasm/${basename(id, ".wasm")}-${sha1(buff)}.wasm`;
const parsed = parse(name, buff);

assets[id] = <WasmAsset>{
const asset = assets[name] = <WasmAsset>{
name,
id,
source,
source: buff,
imports: parsed.imports,
exports: parsed.exports,
};

return `export default "UNWASM DUMMY EXPORT";`;
},
transform(_code, id) {
if (!id.endsWith(".wasm")) {
return;
}
const asset = assets[id];
if (!asset) {
return;
}

return {
code: getWasmBinding(asset, opts),
map: { mappings: "" },
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getWasmBinding(asset: WasmAsset, opts: UnwasmPluginOptions) {
const envCode: string = opts.esmImport
? js`
async function _instantiate(imports) {
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.id}").then(r => r.default || r);
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
return WebAssembly.instantiate(_mod, imports)
}
`
Expand Down

0 comments on commit 7446cbf

Please sign in to comment.