30 lines
827 B
TypeScript
30 lines
827 B
TypeScript
import { plugin, type BunPlugin } from "bun";
|
|
import { readFileSync } from 'fs';
|
|
|
|
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;
|