Skip to content

Commit 7446cbf

Browse files
committed
fix: use hashed name to escape paths
1 parent 633f69a commit 7446cbf

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/plugin/index.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,26 @@ const unplugin = createUnplugin<UnwasmPluginOptions>((opts) => {
8888
return;
8989
}
9090

91-
const source = await fs.readFile(id);
92-
const name = `wasm/${basename(id, ".wasm")}-${sha1(source)}.wasm`;
93-
const parsed = parse(name, source);
91+
const buff = await fs.readFile(id);
92+
return buff.toString('binary');
93+
},
94+
transform(code, id) {
95+
if (!id.endsWith(".wasm")) {
96+
return;
97+
}
98+
99+
const buff = Buffer.from(code, 'binary')
100+
const name = `wasm/${basename(id, ".wasm")}-${sha1(buff)}.wasm`;
101+
const parsed = parse(name, buff);
94102

95-
assets[id] = <WasmAsset>{
103+
const asset = assets[name] = <WasmAsset>{
96104
name,
97105
id,
98-
source,
106+
source: buff,
99107
imports: parsed.imports,
100108
exports: parsed.exports,
101109
};
102110

103-
return `export default "UNWASM DUMMY EXPORT";`;
104-
},
105-
transform(_code, id) {
106-
if (!id.endsWith(".wasm")) {
107-
return;
108-
}
109-
const asset = assets[id];
110-
if (!asset) {
111-
return;
112-
}
113-
114111
return {
115112
code: getWasmBinding(asset, opts),
116113
map: { mappings: "" },

src/plugin/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function getWasmBinding(asset: WasmAsset, opts: UnwasmPluginOptions) {
1313
const envCode: string = opts.esmImport
1414
? js`
1515
async function _instantiate(imports) {
16-
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.id}").then(r => r.default || r);
16+
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
1717
return WebAssembly.instantiate(_mod, imports)
1818
}
1919
`

0 commit comments

Comments
 (0)