From 74b6fe97e51f624de6d42465263f23b26346e4fc Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Fri, 29 May 2026 20:22:06 +0800 Subject: [PATCH] build(core): externalize node:sqlite instead of SSR-bundling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses MliKiowa's review on PR #68 ("单独排除 sqlite 就行了"). That PR fixed the `DatabaseSync is not a constructor` crash by switching the core bundle to SSR mode (build.ssr + ssr.noExternal + target node22 + a catch-all isNodeBuiltin external fn). The root cause was narrower: on Node 22.x `node:sqlite` is experimental and absent from `builtinModules`, so the `nodeModules` sweep didn't externalize it and Vite swapped in a browser shell. The SSR switch also self-inflicted the @hono/node-server MODULE_NOT_FOUND it then had to paper over with noExternal — the original config already bundled all non-builtin deps. Revert vite.config.ts to the pre-PR state and just add 'node:sqlite' (+ bare 'sqlite') to the external list. Verified: `SNOWLUMA_TARGET=linux-x64 vite build` succeeds and dist/index.mjs now carries a real `import { DatabaseSync } from "node:sqlite"` rather than a shimmed shell. --- packages/core/vite.config.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/core/vite.config.ts b/packages/core/vite.config.ts index a148e838..90eb7396 100644 --- a/packages/core/vite.config.ts +++ b/packages/core/vite.config.ts @@ -23,10 +23,15 @@ 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 isNodeBuiltin = (id: string) => - id.startsWith('node:') || - builtinModules.includes(id) || - builtinModules.includes(id.replace(/^node:/, '')); +// +// `node:sqlite` is an experimental builtin that Node 22.x lines don't +// list in `builtinModules`, so the `nodeModules` sweep below misses it. +// Externalise it explicitly — otherwise Vite bundles it and swaps in a +// browser shell, crashing the hook with `DatabaseSync is not a +// constructor` (the bug PR #68 set out to fix). +const external: string[] = ['node:sqlite', 'sqlite']; + +const nodeModules = [...builtinModules, ...builtinModules.map((m) => `node:${m}`)].flat(); const runtimeSrc = toPosix(runtimeDir); const nativeSrc = toPosix(nativeDir); @@ -116,9 +121,8 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ } }, build: { - ssr: true, sourcemap: source_map, - target: 'node22', + target: 'esnext', minify: false, lib: { entry: { @@ -128,7 +132,7 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({ fileName: (_, entryName) => `${entryName}.mjs` }, rollupOptions: { - external: isNodeBuiltin, + external: [...nodeModules, ...external], output: { // better-sqlite3's JS wrapper is pure CJS — its `require('fs')` / // `require('path')` / `require('bindings')` calls get inlined into @@ -144,9 +148,6 @@ 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);", @@ -158,9 +159,6 @@ 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)