|
1 | | -import path from 'path' |
2 | | -import type { Plugin, UserConfig } from 'vite' |
| 1 | +import path from "path"; |
| 2 | +import type { Plugin, UserConfig } from "vite"; |
3 | 3 |
|
4 | 4 | export interface BuildOptions { |
5 | | - entry?: string |
6 | | - outDir?: string |
7 | | - serverOutFile?: string |
8 | | - framework?: 'vercel' // Target framework for deployment |
| 5 | + entry?: string; |
| 6 | + outDir?: string; |
| 7 | + serverOutFile?: string; |
| 8 | + framework?: "vercel"; // Target framework for deployment |
9 | 9 | } |
10 | 10 |
|
11 | 11 | export const defaultBuildOptions: Partial<BuildOptions> = { |
12 | | - entry: './src/server.ts', |
13 | | - outDir: 'dist', |
14 | | - serverOutFile: 'server.js', |
15 | | -} |
| 12 | + entry: "./src/server.ts", |
| 13 | + outDir: "dist", |
| 14 | + serverOutFile: "server.js", |
| 15 | +}; |
16 | 16 |
|
17 | 17 | export function srvxBuild(options?: BuildOptions): Plugin { |
18 | | - const mergedOptions = { ...defaultBuildOptions, ...options } |
| 18 | + const mergedOptions = { ...defaultBuildOptions, ...options }; |
19 | 19 |
|
20 | | - // Auto-detect Vercel or use explicit framework option |
21 | | - const isVercel = mergedOptions.framework === 'vercel' || process.env.VERCEL === '1' |
| 20 | + // Auto-detect Vercel or use explicit framework option |
| 21 | + const isVercel = |
| 22 | + mergedOptions.framework === "vercel" || process.env.VERCEL === "1"; |
22 | 23 |
|
23 | | - return { |
24 | | - name: 'vite-plugin-srvx-server-build', |
| 24 | + return { |
| 25 | + name: "vite-plugin-srvx-server-build", |
25 | 26 |
|
26 | | - // Only apply during server builds (not client builds) |
27 | | - apply(_config, { command, mode }) { |
28 | | - return command === 'build' && mode === 'server' |
29 | | - }, |
| 27 | + // Only apply during server builds (not client builds) |
| 28 | + apply(_config, { command, mode }) { |
| 29 | + return command === "build" && mode === "server"; |
| 30 | + }, |
30 | 31 |
|
31 | | - config(): UserConfig { |
32 | | - // For Vercel, output to dist/api/index.js |
33 | | - // For standard builds, use configured options |
34 | | - const serverOutDir = isVercel |
35 | | - ? path.join(mergedOptions.outDir || 'dist', 'api') |
36 | | - : (mergedOptions.outDir || 'dist') |
37 | | - const serverOutFile = isVercel ? 'index.js' : (mergedOptions.serverOutFile || 'server.js') |
| 32 | + config(): UserConfig { |
| 33 | + // For Vercel, output to dist/api/index.js |
| 34 | + // For standard builds, use configured options |
| 35 | + const serverOutDir = isVercel |
| 36 | + ? path.join(mergedOptions.outDir || "dist") |
| 37 | + : mergedOptions.outDir || "dist"; |
| 38 | + const serverOutFile = isVercel |
| 39 | + ? "index.js" |
| 40 | + : mergedOptions.serverOutFile || "server.js"; |
38 | 41 |
|
39 | | - return { |
40 | | - build: { |
41 | | - ssr: true, |
42 | | - outDir: serverOutDir, |
43 | | - copyPublicDir: false, // Don't copy public assets during server build |
44 | | - rollupOptions: { |
45 | | - input: mergedOptions.entry, |
46 | | - output: { |
47 | | - entryFileNames: serverOutFile, |
48 | | - format: 'esm', |
49 | | - }, |
50 | | - }, |
51 | | - emptyOutDir: false, |
52 | | - }, |
53 | | - } |
54 | | - }, |
| 42 | + return { |
| 43 | + build: { |
| 44 | + ssr: true, |
| 45 | + outDir: serverOutDir, |
| 46 | + copyPublicDir: false, // Don't copy public assets during server build |
| 47 | + rollupOptions: { |
| 48 | + input: mergedOptions.entry, |
| 49 | + output: { |
| 50 | + entryFileNames: serverOutFile, |
| 51 | + format: "esm", |
| 52 | + }, |
| 53 | + }, |
| 54 | + emptyOutDir: false, |
| 55 | + }, |
| 56 | + }; |
| 57 | + }, |
55 | 58 |
|
56 | | - buildEnd() { |
57 | | - const serverOutDir = isVercel |
58 | | - ? path.join(mergedOptions.outDir || 'dist', 'api') |
59 | | - : (mergedOptions.outDir || 'dist') |
60 | | - const serverOutFile = isVercel ? 'index.js' : (mergedOptions.serverOutFile || 'server.js') |
61 | | - const serverPath = path.join(serverOutDir, serverOutFile) |
| 59 | + buildEnd() { |
| 60 | + const serverOutDir = isVercel |
| 61 | + ? path.join(mergedOptions.outDir || "dist", "api") |
| 62 | + : mergedOptions.outDir || "dist"; |
| 63 | + const serverOutFile = isVercel |
| 64 | + ? "index.js" |
| 65 | + : mergedOptions.serverOutFile || "server.js"; |
| 66 | + const serverPath = path.join(serverOutDir, serverOutFile); |
62 | 67 |
|
63 | | - console.log('\n✅ Server built successfully!') |
64 | | - console.log(`📦 Server output: ${serverPath}`) |
| 68 | + console.log("\n✅ Server built successfully!"); |
| 69 | + console.log(`📦 Server output: ${serverPath}`); |
65 | 70 |
|
66 | | - if (isVercel) { |
67 | | - console.log('\n🚀 Deploy to Vercel:') |
68 | | - console.log(' 1. Configure vercel.json with:') |
69 | | - console.log(' {') |
70 | | - console.log(' "functions": {') |
71 | | - console.log(' "dist/api/**": { "runtime": "edge" }') |
72 | | - console.log(' }') |
73 | | - console.log(' }') |
74 | | - console.log(' 2. Run: vercel deploy\n') |
75 | | - console.log('💡 Output structure:') |
76 | | - console.log(' dist/api/index.js - Edge Function') |
77 | | - console.log(' dist/public/ - Static assets') |
78 | | - } else { |
79 | | - console.log('\n🚀 Run your app with:') |
80 | | - console.log(` srvx ${serverPath}\n`) |
81 | | - console.log('💡 Tip: srvx will automatically serve static files from dist/public/') |
82 | | - } |
83 | | - }, |
84 | | - } |
| 71 | + if (isVercel) { |
| 72 | + console.log("\n🚀 Deploy to Vercel:"); |
| 73 | + console.log(" 1. Configure vercel.json with:"); |
| 74 | + console.log(" {"); |
| 75 | + console.log(' "functions": {'); |
| 76 | + console.log(' "dist/api/**": { "runtime": "edge" }'); |
| 77 | + console.log(" }"); |
| 78 | + console.log(" }"); |
| 79 | + console.log(" 2. Run: vercel deploy\n"); |
| 80 | + console.log("💡 Output structure:"); |
| 81 | + console.log(" dist/api/index.js - Edge Function"); |
| 82 | + console.log(" dist/public/ - Static assets"); |
| 83 | + } else { |
| 84 | + console.log("\n🚀 Run your app with:"); |
| 85 | + console.log(` srvx ${serverPath}\n`); |
| 86 | + console.log( |
| 87 | + "💡 Tip: srvx will automatically serve static files from dist/public/", |
| 88 | + ); |
| 89 | + } |
| 90 | + }, |
| 91 | + }; |
85 | 92 | } |
0 commit comments