vite-plugin-builder

License Build Status

vite-plugin-builder is a Vite plugin designed to simplify the setup of dual compilation for Server-Side Rendering (SSR) and Client-Side Rendering (CSR) in Vite projects. It allows you to build both server and client entry points in a single Vite project, streamlining the development of modern web applications that require both SSR and CSR functionality.

Features

Installation

To install vite-plugin-builder in your Vite project, you can use npm or yarn:

npm install vite-plugin-builder --save-dev

or

yarn add vite-plugin-builder --dev

Usage

Basic Setup

  1. Install the plugin as a dev dependency (as shown above).
  2. Import and configure the plugin in your vite.config.ts file.
import { defineConfig } from "vite";
import path from "path";
import react from "@vitejs/plugin-react-swc";
import builder from "vite-plugin-builder";
// https://vite.dev/config/
export default defineConfig({
  base: "/",
  plugins: [
    react(),
    //Simple Configuration
    builder({
      serverEntry: "server/main.js",
      clientEntry: {
        main: "index.html",
      },
    }),
  ],
});

This configuration genrate this structures

# Server Out
/dist/app.js
/dist/bin/[name]-[hash].[ext]
/dist/assets/[name]-[hash].[ext]
# Client Out
/dist/public/index.html
/dist/public/assets/[name]-[hash].[ext]
/dist/public/chunks/[name]-[hash].[ext]

Plugin Server Options

The plugin accepts the following options for serverEntry:

import { defineConfig } from "vite";
import path from "path";
import react from "@vitejs/plugin-react-swc";
import builder from "vite-plugin-builder";
// https://vite.dev/config/
export default defineConfig({
  base: "/",
  plugins: [
    react(),
    builder({
      // Path to the server entry file
      serverEntry: "server/main.js",
      // (optional) Configuration for the server build
      serverConfig: {
        define: {
          "BUILD.BASE": '"/"',
          "BUILD.BASE_API": '"/api"',
          "BUILD.STATIC_DIR": '"public"',
          "BUILD.SERVER_IP": '"0.0.0.0"',
          "BUILD.SERVER_PORT": "3001",
        },
      },
      // (optional) Function to modify the server build config
      serverBuild: (viteConfig) => {
        return viteConfig;
      },
    }),
  ],
});

Server Configuration Options

serverEntry: string

serverConfig

This is an optional configuration object for the server build. It contains detailed options for how the server should be built, including external dependencies, output options, minification, and more.

serverConfig.external
serverConfig.noExternal
serverConfig.output
serverConfig.minify
serverConfig.target
serverConfig.outDir
serverConfig.emptyOutDir: boolean
serverConfig.privateDir
serverConfig.define

serverBuild

Plugin Client Options

The plugin accepts the following options for clientEntry:

import { defineConfig } from "vite";
import path from "path";
import react from "@vitejs/plugin-react-swc";
import builder from "vite-plugin-builder";
// https://vite.dev/config/
export default defineConfig({
  base: "/",
  plugins: [
    react(),
    builder({
      serverEntry: "server/main.js",
      // (optional) Path to the client entry file
      clientEntry: {
        main: "index.html",
      },
      // (optional) Configuration for the client build
      clientConfig: {
        outDir: "dist/public",
      },
      // (optional) Function to modify the client build config
      clientBuild: (viteConfig) => {
        return viteConfig;
      },
    }),
  ],
});

Client Configuration Options

clientEntry

clientConfig

This is an optional configuration object for the client-side build. It contains detailed options for how the client should be built, including external dependencies, output options, and more.

clientConfig.externa
clientConfig.output
clientConfig.minify
clientConfig.target
clientConfig.outDir
clientConfig.emptyOutDir

clientBuild

Mode Options

The plugin supports several modes that determine how the server and client builds are executed. These modes are configured via the mode option and include:

Example:

mode: "parallel";