Skip to content

Commit

Permalink
make isomorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlened committed Feb 27, 2024
1 parent dc080d0 commit f32a74a
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ export default function vitePluginArraybuffer(): PluginOption {
const buffer = await promises.readFile(file)
const b64 = buffer.toString("base64")

return `export default new Uint8Array(
Buffer.from("${b64}", "base64")
).buffer`
return `
function toUint8(b64) {
let bin = atob(b64);
let len = bin.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = bin.charCodeAt(i);
}
return bytes;
}
export default toUint8("${b64}").buffer
`
}
if (id.endsWith("?uint8array")) {
const file = id.slice(0, -11)
Expand All @@ -32,9 +42,19 @@ export default function vitePluginArraybuffer(): PluginOption {
const buffer = await promises.readFile(file)
const b64 = buffer.toString("base64")

return `export default new Uint8Array(
Buffer.from("${b64}", "base64")
)`
return `
function toUint8(b64) {
let bin = atob(b64);
let len = bin.length;
let bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = bin.charCodeAt(i);
}
return bytes;
}
export default toUint8("${b64}")
`
}

return;
Expand Down

0 comments on commit f32a74a

Please sign in to comment.