forked from huggingface/chat-ui
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
147 lines (137 loc) · 4.17 KB
/
vite.config.ts
File metadata and controls
147 lines (137 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { sveltekit } from "@sveltejs/kit/vite";
import Icons from "unplugin-icons/vite";
import { promises } from "fs";
import { defineConfig } from "vitest/config";
import { resolve } from "path";
import fs from "fs-extra";
import { spawn } from "child_process";
import type { Plugin } from "vite";
// used to load fonts server side for thumbnail generation
function loadTTFAsArrayBuffer() {
return {
name: "load-ttf-as-array-buffer",
async transform(_src, id) {
if (id.endsWith(".ttf")) {
return `export default new Uint8Array([
${new Uint8Array(await promises.readFile(id))}
]).buffer`;
}
},
};
}
const isViteNode = process.argv.some((arg) => arg.includes("vite-node")) || !!process.env.VITE_NODE;
const skipLlamaCppBuild = process.env.SKIP_LLAMA_CPP_BUILD === "true";
const shouldCopyLlama =
process.env.npm_lifecycle_event === "build" && !isViteNode && !skipLlamaCppBuild; // Copy node-llama-cpp/llama files to build output
function copyLlamaFiles() {
return {
name: "copy-llama-files",
apply: "build" as const,
closeBundle: async () => {
try {
// Run npx command first and pipe IO
console.log("Running node-llama-cpp source download...");
await new Promise((resolve, reject) => {
const npxProcess = spawn("npx", ["--no", "node-llama-cpp", "source", "download"], {
stdio: "inherit", // Pipe all IO to parent process
shell: true,
});
npxProcess.on("close", (code) => {
if (code === 0) {
console.log("✓ Successfully downloaded llama source files");
resolve(code);
} else {
reject(new Error(`npx command failed with code ${code}`));
}
});
npxProcess.on("error", (err) => {
reject(err);
});
});
const sourcePath = resolve("node_modules/node-llama-cpp/llama");
const destPath = resolve("build/server/llama");
// Ensure destination directory exists
await fs.ensureDir(destPath);
// Copy files - using a filter to prevent copying files to subdirectories of themselves
await fs.copy(sourcePath, destPath, {
filter: (src, dest) => {
// Skip if source path is inside destination path or vice versa
if (src.includes(destPath) || dest.includes(sourcePath)) {
console.log(`Skipping problematic copy: ${src} -> ${dest}`);
return false;
}
return true;
},
overwrite: true,
dereference: true,
});
console.log("✓ Successfully copied llama files to build output");
} catch (error) {
console.error("Error in llama files process:", error);
}
},
} satisfies Plugin;
}
export default defineConfig({
plugins: [
sveltekit(),
Icons({
compiler: "svelte",
}),
loadTTFAsArrayBuffer(),
...(shouldCopyLlama ? [copyLlamaFiles()] : []),
],
optimizeDeps: {
include: ["uuid", "@huggingface/transformers", "sharp", "@gradio/client", "clsx"],
},
ssr: {
// don’t externalize @gradio/client in SSR build
noExternal: ["@gradio/client"],
},
resolve: {
// try browser-first, then module, then main
mainFields: ["browser", "module", "main"],
// accept more package.json “exports” conditions
conditions: ["browser", "import", "require"],
},
test: {
workspace: [
{
// Client-side tests (Svelte components)
extends: "./vite.config.ts",
test: {
name: "client",
environment: "browser",
browser: {
enabled: true,
provider: "playwright",
instances: [{ browser: "chromium", headless: true }],
},
include: ["src/**/*.svelte.{test,spec}.{js,ts}"],
exclude: ["src/lib/server/**", "src/**/*.ssr.{test,spec}.{js,ts}"],
setupFiles: ["./scripts/setups/vitest-setup-client.ts"],
},
},
{
// SSR tests (Server-side rendering)
extends: "./vite.config.ts",
test: {
name: "ssr",
environment: "node",
include: ["src/**/*.ssr.{test,spec}.{js,ts}"],
},
},
{
// Server-side tests (Node.js utilities)
extends: "./vite.config.ts",
test: {
name: "server",
environment: "node",
include: ["src/**/*.{test,spec}.{js,ts}"],
exclude: ["src/**/*.svelte.{test,spec}.{js,ts}", "src/**/*.ssr.{test,spec}.{js,ts}"],
setupFiles: ["./scripts/setups/vitest-setup-server.ts"],
},
},
],
},
});