Created
March 28, 2024 08:20
-
-
Save buhrmi/6e6d2256888d8d385d66a5f6b20a203d to your computer and use it in GitHub Desktop.
bun-plugin-svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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