1
0
Fork 0
tsgames/build/audioPlugin.ts

32 lines
1.1 KiB
TypeScript

import { plugin, type BunPlugin } from "bun";
const audioPlugin: BunPlugin = {
name: "Audio loader",
async setup(build) {
build.onLoad({ filter: /\.(wav|mp3|ogg)$/ }, async (args) => {
const arrayBuffer = await Bun.file(args.path).arrayBuffer();
const ext = args.path.split('.').pop();
const buffer = Buffer.from(arrayBuffer);
const src = `data:audio/${ext};base64,${buffer.toString('base64')}`;
return {
contents: `
const audio = document.createElement('audio');
const promise = new Promise((resolve, reject) => {
audio.oncanplay = resolve;
audio.onerror = () => reject(audio.error);
});
audio.src = (${JSON.stringify(src)});
await promise;
export default audio;
`,
loader: 'js',
};
});
}
};
plugin(audioPlugin);
export default audioPlugin;