Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 76 additions & 69 deletions src/build.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,92 @@
import path from 'path'
import type { Plugin, UserConfig } from 'vite'
import path from "path";
import type { Plugin, UserConfig } from "vite";

export interface BuildOptions {
entry?: string
outDir?: string
serverOutFile?: string
framework?: 'vercel' // Target framework for deployment
entry?: string;
outDir?: string;
serverOutFile?: string;
framework?: "vercel"; // Target framework for deployment
}

export const defaultBuildOptions: Partial<BuildOptions> = {
entry: './src/server.ts',
outDir: 'dist',
serverOutFile: 'server.js',
}
entry: "./src/server.ts",
outDir: "dist",
serverOutFile: "server.js",
};

export function srvxBuild(options?: BuildOptions): Plugin {
const mergedOptions = { ...defaultBuildOptions, ...options }
const mergedOptions = { ...defaultBuildOptions, ...options };

// Auto-detect Vercel or use explicit framework option
const isVercel = mergedOptions.framework === 'vercel' || process.env.VERCEL === '1'
// Auto-detect Vercel or use explicit framework option
const isVercel =
mergedOptions.framework === "vercel" || process.env.VERCEL === "1";

return {
name: 'vite-plugin-srvx-server-build',
return {
name: "vite-plugin-srvx-server-build",

// Only apply during server builds (not client builds)
apply(_config, { command, mode }) {
return command === 'build' && mode === 'server'
},
// Only apply during server builds (not client builds)
apply(_config, { command, mode }) {
return command === "build" && mode === "server";
},

config(): UserConfig {
// For Vercel, output to dist/api/index.js
// For standard builds, use configured options
const serverOutDir = isVercel
? path.join(mergedOptions.outDir || 'dist', 'api')
: (mergedOptions.outDir || 'dist')
const serverOutFile = isVercel ? 'index.js' : (mergedOptions.serverOutFile || 'server.js')
config(): UserConfig {
// For Vercel, output to dist/api/index.js
// For standard builds, use configured options
const serverOutDir = isVercel
? path.join(mergedOptions.outDir || "dist")
: mergedOptions.outDir || "dist";
const serverOutFile = isVercel
? "server.js"
: mergedOptions.serverOutFile || "server.js";

return {
build: {
ssr: true,
outDir: serverOutDir,
copyPublicDir: false, // Don't copy public assets during server build
rollupOptions: {
input: mergedOptions.entry,
output: {
entryFileNames: serverOutFile,
format: 'esm',
},
},
emptyOutDir: false,
},
}
},
return {
build: {
ssr: true,
outDir: serverOutDir,
copyPublicDir: false, // Don't copy public assets during server build
rollupOptions: {
input: mergedOptions.entry,
output: {
entryFileNames: serverOutFile,
format: "esm",
},
},
emptyOutDir: false,
},
};
},

buildEnd() {
const serverOutDir = isVercel
? path.join(mergedOptions.outDir || 'dist', 'api')
: (mergedOptions.outDir || 'dist')
const serverOutFile = isVercel ? 'index.js' : (mergedOptions.serverOutFile || 'server.js')
const serverPath = path.join(serverOutDir, serverOutFile)
buildEnd() {
const serverOutDir = isVercel
? path.join(mergedOptions.outDir || "dist", "api")
: mergedOptions.outDir || "dist";
const serverOutFile = isVercel
? "index.js"
: mergedOptions.serverOutFile || "server.js";
const serverPath = path.join(serverOutDir, serverOutFile);

console.log('\n✅ Server built successfully!')
console.log(`📦 Server output: ${serverPath}`)
console.log("\n✅ Server built successfully!");
console.log(`📦 Server output: ${serverPath}`);

if (isVercel) {
console.log('\n🚀 Deploy to Vercel:')
console.log(' 1. Configure vercel.json with:')
console.log(' {')
console.log(' "functions": {')
console.log(' "dist/api/**": { "runtime": "edge" }')
console.log(' }')
console.log(' }')
console.log(' 2. Run: vercel deploy\n')
console.log('💡 Output structure:')
console.log(' dist/api/index.js - Edge Function')
console.log(' dist/public/ - Static assets')
} else {
console.log('\n🚀 Run your app with:')
console.log(` srvx ${serverPath}\n`)
console.log('💡 Tip: srvx will automatically serve static files from dist/public/')
}
},
}
if (isVercel) {
console.log("\n🚀 Deploy to Vercel:");
console.log(" 1. Configure vercel.json with:");
console.log(" {");
console.log(' "functions": {');
console.log(' "dist/api/**": { "runtime": "edge" }');
console.log(" }");
console.log(" }");
console.log(" 2. Run: vercel deploy\n");
console.log("💡 Output structure:");
console.log(" dist/api/index.js - Edge Function");
console.log(" dist/public/ - Static assets");
} else {
console.log("\n🚀 Run your app with:");
console.log(` srvx ${serverPath}\n`);
console.log(
"💡 Tip: srvx will automatically serve static files from dist/public/",
);
}
},
};
}
Loading