build(core): externalize node:sqlite instead of SSR-bundling
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.
This commit is contained in:
1 file changed
+11
-13
@@ -23,10 +23,15 @@ const toPosix = (p: string) => p.replace(/\\/g, '/');
|
|||||||
// `@snowluma/websocket` is bundled — it's an
|
// `@snowluma/websocket` is bundled — it's an
|
||||||
// in-tree TS workspace wrapper that routes its native `.node`
|
// in-tree TS workspace wrapper that routes its native `.node`
|
||||||
// addon through `dist/native/`. Only Node builtins stay external.
|
// addon through `dist/native/`. Only Node builtins stay external.
|
||||||
const isNodeBuiltin = (id: string) =>
|
//
|
||||||
id.startsWith('node:') ||
|
// `node:sqlite` is an experimental builtin that Node 22.x lines don't
|
||||||
builtinModules.includes(id) ||
|
// list in `builtinModules`, so the `nodeModules` sweep below misses it.
|
||||||
builtinModules.includes(id.replace(/^node:/, ''));
|
// 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 runtimeSrc = toPosix(runtimeDir);
|
||||||
const nativeSrc = toPosix(nativeDir);
|
const nativeSrc = toPosix(nativeDir);
|
||||||
@@ -116,9 +121,8 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
build: {
|
build: {
|
||||||
ssr: true,
|
|
||||||
sourcemap: source_map,
|
sourcemap: source_map,
|
||||||
target: 'node22',
|
target: 'esnext',
|
||||||
minify: false,
|
minify: false,
|
||||||
lib: {
|
lib: {
|
||||||
entry: {
|
entry: {
|
||||||
@@ -128,7 +132,7 @@ const BaseConfig = (source_map: boolean = false) => defineConfig({
|
|||||||
fileName: (_, entryName) => `${entryName}.mjs`
|
fileName: (_, entryName) => `${entryName}.mjs`
|
||||||
},
|
},
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
external: isNodeBuiltin,
|
external: [...nodeModules, ...external],
|
||||||
output: {
|
output: {
|
||||||
// better-sqlite3's JS wrapper is pure CJS — its `require('fs')` /
|
// better-sqlite3's JS wrapper is pure CJS — its `require('fs')` /
|
||||||
// `require('path')` / `require('bindings')` calls get inlined into
|
// `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
|
// the shim then transparently delegates every inlined CJS require
|
||||||
// to the real CJS loader. Node 18+ ships `createRequire` in the
|
// to the real CJS loader. Node 18+ ships `createRequire` in the
|
||||||
// `node:module` builtin, so this is supported on every target.
|
// `node:module` builtin, so this is supported on every target.
|
||||||
entryFileNames: '[name].mjs',
|
|
||||||
chunkFileNames: '[name].mjs',
|
|
||||||
|
|
||||||
banner: [
|
banner: [
|
||||||
"import { createRequire as __snowlumaCreateRequire } from 'node:module';",
|
"import { createRequire as __snowlumaCreateRequire } from 'node:module';",
|
||||||
"const require = __snowlumaCreateRequire(import.meta.url);",
|
"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.
|
// Required since outDir is outside the vite project root.
|
||||||
emptyOutDir: true
|
emptyOutDir: true
|
||||||
},
|
},
|
||||||
ssr: {
|
|
||||||
noExternal: true,
|
|
||||||
},
|
|
||||||
define: {
|
define: {
|
||||||
__BUILD_WEBUI__: process.env.BUILD_WEBUI === 'true',
|
__BUILD_WEBUI__: process.env.BUILD_WEBUI === 'true',
|
||||||
__APP_VERSION__: JSON.stringify(rootPkg.version)
|
__APP_VERSION__: JSON.stringify(rootPkg.version)
|
||||||
|
|||||||
Reference in new issue
Block a user