Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance and bundled size #6

Merged
merged 10 commits into from
Feb 29, 2024
17 changes: 17 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import arrayBuffer from "./typescript.svg?arraybuffer";
import uint8array from "./typescript.svg?uint8array";
import arrayBufferB64 from "./typescript.svg?arraybuffer&base64";
import uint8arrayB64 from "./typescript.svg?uint8array&base64";
import crypto from "node:crypto";

import { describe, expect, test } from "vitest";

const hash = (buffer: ArrayBuffer) => crypto.createHash("md5").update(Buffer.from(buffer)).digest("hex");

describe("test plugin", () => {
test("?arraybuffer", () => {
expect(arrayBuffer[Symbol.toStringTag]).toBe("ArrayBuffer");
expect(arrayBuffer instanceof ArrayBuffer).toBe(true);
expect(hash(arrayBuffer)).toEqual("7167f7caac27a336c58b0c16cc5003d7");
});
test("?uint8array", () => {
expect(uint8array[Symbol.toStringTag]).toBe("Uint8Array");
expect(uint8array instanceof Uint8Array).toBe(true);
expect(hash(uint8array.buffer)).toEqual("7167f7caac27a336c58b0c16cc5003d7");
});
test("?arraybuffer&base64", () => {
expect(arrayBufferB64[Symbol.toStringTag]).toBe("ArrayBuffer");
expect(arrayBufferB64 instanceof ArrayBuffer).toBe(true);
expect(hash(arrayBufferB64)).toEqual("7167f7caac27a336c58b0c16cc5003d7");
});
test("?uint8array&base64", () => {
expect(uint8arrayB64[Symbol.toStringTag]).toBe("Uint8Array");
expect(uint8arrayB64 instanceof Uint8Array).toBe(true);
expect(hash(uint8arrayB64.buffer)).toEqual("7167f7caac27a336c58b0c16cc5003d7");
});
});
49 changes: 48 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export default function vitePluginArraybuffer(): PluginOption {
return {
name: "vite-plugin-arraybuffer",
resolveId(id) {
if (id.endsWith("?arraybuffer") || id.endsWith("?uint8array")) {
if (
id.endsWith("?arraybuffer") ||
id.endsWith("?uint8array") ||
id.endsWith("?arraybuffer&base64") ||
id.endsWith("?uint8array&base64")
) {
return id
}

Expand All @@ -30,6 +35,48 @@ export default function vitePluginArraybuffer(): PluginOption {
${new Uint8Array(await promises.readFile(file)).join(',')}
])`;
}
if (id.endsWith("?arraybuffer&base64")) {
const file = id.slice(0, -12)
this.addWatchFile(file)

const buffer = await promises.readFile(file)
const b64 = buffer.toString("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}").buffer
`
}
if (id.endsWith("?uint8array&base64")) {
const file = id.slice(0, -11)
this.addWatchFile(file)

const buffer = await promises.readFile(file)
const b64 = buffer.toString("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
12 changes: 12 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ declare module "*?uint8array" {

export default value;
}

declare module "*?arraybuffer&base64" {
const value: ArrayBuffer;

export default value;
}

declare module "*?uint8array&base64" {
const value: Uint8Array;

export default value;
}