diff --git a/README.md b/README.md index e305568..da84eda 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,85 @@ import { useFetch } from 'vite-ssr-vue3' const counts = await useFetch('counts', () => Promise.resolve([1, 2, 3])) ``` +### Full Control Build + +`vite-ssr build` command default equals build client and server, so you can customize your build process. + +```diff +{ + "scripts": { ++ "build": "vite-ssr build", +- "build:client": "vite build --ssrManifest --outDir dist/client", +- "build:server": "vite build --ssr src/main.ts --outDir dist/server", + }, +} +``` + +vite-ssr support separate options for client and server building, Configure them for separate builds + +```ts +import { defineConfig } from 'vite' + +export default defineConfig({ + ssrOptions: { + // Client build options + clientConfig: {}, + // Server build options + serverConfig: {}, + }, +}) +``` + +Compiled with `vite-ssr build`, there are three ways to start the server: + +1. Run `vite-ssr --mode production` +2. Use vite-ssr built-in express server, then run `node ./bin/www` + + ```js + // ./bin/www.js + const { startExpressServer: startServer } = require('vite-ssr-vue3/server') + const { createApp } = require('../dist/server/main.cjs') + + startServer({ createApp }) + ``` + +3. Customize server + + // TODO + +In the above three ways, we need to strongly depend on the server, although we can use `ssr.noExternal` to package dependencies, but the server cannot be packaged in, you must install server package `npm install express` + +we can modify the server building entry file to avoid this, create a new file to wrapper your server instance(*`vite-ssr` will not works*), configure `ssr.noExternal: /./` to package all dependencies. if you want to runing anywhere, this is recommended. + +```ts +// src/server.ts +import { startExpressServer as startServer } from 'vite-ssr-vue3/server' +import { createApp } from './main' + +startServer({ createApp, root: __dirname, outDir: '..' }) +``` + +```ts +// vite.config.noexternal.ts +import type { UserConfig } from 'vite' +import { defineConfig } from 'vite' + +export default defineConfig({ + ssrOptions: { + serverConfig: { + build: { + ssr: './src/server', + }, + ssr: { + noExternal: /./, + }, + }, + }, +} as UserConfig) +``` + +then run `vite-ssr build:noexternal`, wait for a while, now you can run `node dist/server/server.cjs` (Required right path) to start the server anywhere. + ## Thanks Lots of references to the following repos.