progress-tracker-api-v2/image-plugin.ts

29 lines
792 B
TypeScript

import { plugin, type BunPlugin } from "bun";
const imageLoaderPlugin: BunPlugin = {
name: "image-loader",
setup(builder) {
builder.onLoad({ filter: /\.(png|jpg|jpeg|webp)/ }, async ({ path }) => {
try {
const file = Bun.file(path);
const bytes = await file.bytes();
return {
exports: { default: bytes },
loader: "object",
}
} catch (err) {
console.error(err);
if (err instanceof Error) {
throw new Error(`Failed to load file: ${err.message}`);
} else if (typeof err === 'string') {
throw new Error(`Failed to load file: ${err}`);
}
throw new Error("Failed to load file");
}
});
}
};
plugin(imageLoaderPlugin);
export default imageLoaderPlugin;