Skip to content

Commit f32a74a

Browse files
committed
make isomorphic
1 parent dc080d0 commit f32a74a

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/main.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ export default function vitePluginArraybuffer(): PluginOption {
2121
const buffer = await promises.readFile(file)
2222
const b64 = buffer.toString("base64")
2323

24-
return `export default new Uint8Array(
25-
Buffer.from("${b64}", "base64")
26-
).buffer`
24+
return `
25+
function toUint8(b64) {
26+
let bin = atob(b64);
27+
let len = bin.length;
28+
let bytes = new Uint8Array(len);
29+
for (let i = 0; i < len; i++) {
30+
bytes[i] = bin.charCodeAt(i);
31+
}
32+
return bytes;
33+
}
34+
35+
export default toUint8("${b64}").buffer
36+
`
2737
}
2838
if (id.endsWith("?uint8array")) {
2939
const file = id.slice(0, -11)
@@ -32,9 +42,19 @@ export default function vitePluginArraybuffer(): PluginOption {
3242
const buffer = await promises.readFile(file)
3343
const b64 = buffer.toString("base64")
3444

35-
return `export default new Uint8Array(
36-
Buffer.from("${b64}", "base64")
37-
)`
45+
return `
46+
function toUint8(b64) {
47+
let bin = atob(b64);
48+
let len = bin.length;
49+
let bytes = new Uint8Array(len);
50+
for (let i = 0; i < len; i++) {
51+
bytes[i] = bin.charCodeAt(i);
52+
}
53+
return bytes;
54+
}
55+
56+
export default toUint8("${b64}")
57+
`
3858
}
3959

4060
return;

0 commit comments

Comments
 (0)