UNPKG

vite-plugin-arraybuffer

Version:

Vite plugin for import file with ArrayBuffer or Uint8Array.

129 lines (123 loc) 3.48 kB
// src/main.ts import { promises } from "fs"; var VIRTUAL_ID = "virtual:decode-64"; var decode64Raw = `function b64ToUint6(nChr) { return nChr > 64 && nChr < 91 ? nChr - 65 : nChr > 96 && nChr < 123 ? nChr - 71 : nChr > 47 && nChr < 58 ? nChr + 4 : nChr === 43 ? 62 : nChr === 47 ? 63 : 0 } function base64ToUint8(sBase64, nBlocksSize) { const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, "") const nInLen = sB64Enc.length const nOutLen = nBlocksSize ? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize : (nInLen * 3 + 1) >> 2 const taBytes = new Uint8Array(nOutLen) let nMod3 let nMod4 let nUint24 = 0 let nOutIdx = 0 for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) { nMod4 = nInIdx & 3 nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4)) if (nMod4 === 3 || nInLen - nInIdx === 1) { nMod3 = 0 while (nMod3 < 3 && nOutIdx < nOutLen) { taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255 nMod3++ nOutIdx++ } nUint24 = 0 } } return taBytes } function toUint8(b64) { if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(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 } const decode64 = typeof atob === "function" ? toUint8 : base64ToUint8 export default decode64 `; function vitePluginArraybuffer() { return { name: "vite-plugin-arraybuffer", enforce: "pre", resolveId: { filter: { id: /^virtual:decode-64$/ }, handler(id) { if (id === VIRTUAL_ID) { return id; } return; } }, load: { filter: { id: /^(virtual:decode-64$|[^?]+\?(?:arraybuffer|uint8array)(?:&base64)?$)/ }, async handler(id) { if (id === VIRTUAL_ID) { return decode64Raw; } const cleanId = id.split("?", 1)[0]; if (id.endsWith("?arraybuffer")) { this.addWatchFile(cleanId); const bytes = new Uint8Array(await promises.readFile(cleanId)); return { code: `export default new Uint8Array([${bytes.join(",")}]).buffer`, map: { mappings: "" } }; } if (id.endsWith("?uint8array")) { this.addWatchFile(cleanId); const bytes = new Uint8Array(await promises.readFile(cleanId)); return { code: `export default new Uint8Array([${bytes.join(",")}])`, map: { mappings: "" } }; } if (id.endsWith("?arraybuffer&base64")) { this.addWatchFile(cleanId); const buffer = await promises.readFile(cleanId); const b64 = buffer.toString("base64"); return { code: `import decode64 from "${VIRTUAL_ID}" export default decode64("${b64}").buffer`, map: { mappings: "" } }; } if (id.endsWith("?uint8array&base64")) { this.addWatchFile(cleanId); const buffer = await promises.readFile(cleanId); const b64 = buffer.toString("base64"); return { code: `import decode64 from "${VIRTUAL_ID}" export default decode64("${b64}")`, map: { mappings: "" } }; } return; } } }; } export { vitePluginArraybuffer as default };