perf: reduce desktop frontend bundle from 10 to 2MB

Use shiki fine-grained bundle, avoid bundling all languages and themes
This commit is contained in:
Huakun Shen 2024-11-04 16:08:23 -05:00
parent 11cc79627d
commit be07e16b36
No known key found for this signature in database
GPG Key ID: 967DBC3ECBD63A70
3 changed files with 50 additions and 32 deletions

View File

@ -1,32 +1,35 @@
import { defineConfig } from "vite"; import { sveltekit } from "@sveltejs/kit/vite"
import { sveltekit } from "@sveltejs/kit/vite"; import { defineConfig } from "vite"
// @ts-expect-error process is a nodejs global // @ts-expect-error process is a nodejs global
const host = process.env.TAURI_DEV_HOST; const host = process.env.TAURI_DEV_HOST
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig(async () => ({ export default defineConfig(async () => ({
plugins: [sveltekit()], plugins: [sveltekit()],
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build` // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
// //
// 1. prevent vite from obscuring rust errors // 1. prevent vite from obscuring rust errors
clearScreen: false, clearScreen: false,
// 2. tauri expects a fixed port, fail if that port is not available // 2. tauri expects a fixed port, fail if that port is not available
server: { server: {
port: 1420, port: 1420,
strictPort: true, strictPort: true,
host: host || false, host: host || false,
hmr: host hmr: host
? { ? {
protocol: "ws", protocol: "ws",
host, host,
port: 1421, port: 1421
} }
: undefined, : undefined,
watch: { watch: {
// 3. tell vite to ignore watching `src-tauri` // 3. tell vite to ignore watching `src-tauri`
ignored: ["**/src-tauri/**"], ignored: ["**/src-tauri/**"]
}, }
}, },
})); build: {
minify: true
}
}))

View File

@ -1,6 +1,10 @@
<!-- Don't Load All languages and themes, use fine-grained bundle-->
<!-- https://shiki.style/guide/bundles#fine-grained-bundle -->
<script lang="ts"> <script lang="ts">
import { cn } from "@kksh/ui/utils" import { cn } from "@kksh/ui/utils"
import { codeToHtml } from "shiki" import { mode } from "mode-watcher"
import { createHighlighterCore } from "shiki/core"
import { createOnigurumaEngine } from "shiki/engine/oniguruma"
import { onMount } from "svelte" import { onMount } from "svelte"
const { const {
@ -8,12 +12,23 @@
lang, lang,
theme, theme,
class: className class: className
}: { code: string; lang: string; theme?: string; class?: string } = $props() }: {
code: string
lang: "json" | "typescript"
theme?: "vitesse-dark" | "vitesse-light"
class?: string
} = $props()
let html = $state("") let html = $state("")
onMount(async () => { onMount(async () => {
html = await codeToHtml(code, { const highlighter = await createHighlighterCore({
lang: lang, themes: [import("shiki/themes/vitesse-dark.mjs"), import("shiki/themes/vitesse-light.mjs")],
theme: theme ?? "vitesse-dark" langs: [import("shiki/langs/json.mjs"), import("shiki/langs/typescript.mjs")],
engine: createOnigurumaEngine(import("shiki/wasm"))
})
html = highlighter.codeToHtml(code, {
lang,
theme: theme ?? ($mode === "dark" ? "vitesse-dark" : "vitesse-light")
}) })
}) })
</script> </script>

View File

@ -14,7 +14,7 @@
</script> </script>
<CommandPrimitive.Group <CommandPrimitive.Group
class={cn("text-foreground overflow-hidden p-1 select-none", className)} class={cn("text-foreground select-none overflow-hidden p-1", className)}
bind:ref bind:ref
{...restProps} {...restProps}
> >