Skip to content

Instantly share code, notes, and snippets.

@buhrmi
Created March 28, 2024 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buhrmi/6e6d2256888d8d385d66a5f6b20a203d to your computer and use it in GitHub Desktop.
Save buhrmi/6e6d2256888d8d385d66a5f6b20a203d to your computer and use it in GitHub Desktop.
bun-plugin-svelte
import { plugin } from "bun";
import { compile } from "svelte/compiler";
plugin({
name: "svelte-loader",
setup(build) {
const emittedCSS = {};
build.module('svelte-plugin', async () => {
return {
exports: {
css: Object.values(emittedCSS).join("\n")
},
loader: 'object',
};
})
// when a .svelte file is imported...
build.onLoad({ filter: /\.svelte$/ }, async ({ path }) => {
// read and compile it with the Svelte compiler
const file = await Bun.file(path).text();
const result = compile(file, {
filename: path,
generate: "ssr",
})
emittedCSS[path] = result.css?.code || "";
// and return the compiled source code as "js"
return {
contents: result.js.code,
loader: "js",
};
});
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment