From bf932477ddde711cb9d2f831fdfb51d849a52f2b Mon Sep 17 00:00:00 2001 From: H3CoF6 <1707889225@qq.com> Date: Thu, 28 May 2026 02:02:29 +0800 Subject: [PATCH] build(core): fix SSR bundle and node:sqlite resolution in Vite config --- packages/core/vite.config.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/core/vite.config.ts b/packages/core/vite.config.ts index b6f14d44..a148e838 100644 --- a/packages/core/vite.config.ts +++ b/packages/core/vite.config.ts @@ -23,9 +23,10 @@ const toPosix = (p: string) => p.replace(/\\/g, '/'); // `@snowluma/websocket` is bundled — it's an // in-tree TS workspace wrapper that routes its native `.node` // addon through `dist/native/`. Only Node builtins stay external. -const external: string[] = []; - -const nodeModules = [...builtinModules, ...builtinModules.map((m) => `node:${m}`)].flat(); +const isNodeBuiltin = (id: string) => + id.startsWith('node:') || + builtinModules.includes(id) || + builtinModules.includes(id.replace(/^node:/, '')); const runtimeSrc = toPosix(runtimeDir); const nativeSrc = toPosix(nativeDir); @@ -115,8 +116,9 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ } }, build: { + ssr: true, sourcemap: source_map, - target: 'esnext', + target: 'node22', minify: false, lib: { entry: { @@ -126,7 +128,7 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ fileName: (_, entryName) => `${entryName}.mjs` }, rollupOptions: { - external: [...nodeModules, ...external], + external: isNodeBuiltin, output: { // better-sqlite3's JS wrapper is pure CJS — its `require('fs')` / // `require('path')` / `require('bindings')` calls get inlined into @@ -142,6 +144,9 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ // the shim then transparently delegates every inlined CJS require // to the real CJS loader. Node 18+ ships `createRequire` in the // `node:module` builtin, so this is supported on every target. + entryFileNames: '[name].mjs', + chunkFileNames: '[name].mjs', + banner: [ "import { createRequire as __snowlumaCreateRequire } from 'node:module';", "const require = __snowlumaCreateRequire(import.meta.url);", @@ -153,6 +158,9 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ // Required since outDir is outside the vite project root. emptyOutDir: true }, + ssr: { + noExternal: true, + }, define: { __BUILD_WEBUI__: process.env.BUILD_WEBUI === 'true', __APP_VERSION__: JSON.stringify(rootPkg.version)