Skip to content

Commit f55a157

Browse files
committed
docs: add full control build
1 parent de6e6b1 commit f55a157

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,85 @@ import { useFetch } from 'vite-ssr-vue3'
8282
const counts = await useFetch('counts', () => Promise.resolve([1, 2, 3]))
8383
```
8484

85+
### Full Control Build
86+
87+
`vite-ssr build` command default equals build client and server, so you can customize your build process.
88+
89+
```diff
90+
{
91+
"scripts": {
92+
+ "build": "vite-ssr build",
93+
- "build:client": "vite build --ssrManifest --outDir dist/client",
94+
- "build:server": "vite build --ssr src/main.ts --outDir dist/server",
95+
},
96+
}
97+
```
98+
99+
vite-ssr support separate options for client and server building, Configure them for separate builds
100+
101+
```ts
102+
import { defineConfig } from 'vite'
103+
104+
export default defineConfig({
105+
ssrOptions: {
106+
// Client build options
107+
clientConfig: {},
108+
// Server build options
109+
serverConfig: {},
110+
},
111+
})
112+
```
113+
114+
Compiled with `vite-ssr build`, there are three ways to start the server:
115+
116+
1. Run `vite-ssr --mode production`
117+
2. Use vite-ssr built-in express server, then run `node ./bin/www`
118+
119+
```js
120+
// ./bin/www.js
121+
const { startExpressServer: startServer } = require('vite-ssr-vue3/server')
122+
const { createApp } = require('../dist/server/main.cjs')
123+
124+
startServer({ createApp })
125+
```
126+
127+
3. Customize server
128+
129+
// TODO
130+
131+
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`
132+
133+
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.
134+
135+
```ts
136+
// src/server.ts
137+
import { startExpressServer as startServer } from 'vite-ssr-vue3/server'
138+
import { createApp } from './main'
139+
140+
startServer({ createApp, root: __dirname, outDir: '..' })
141+
```
142+
143+
```ts
144+
// vite.config.noexternal.ts
145+
import type { UserConfig } from 'vite'
146+
import { defineConfig } from 'vite'
147+
148+
export default defineConfig({
149+
ssrOptions: {
150+
serverConfig: {
151+
build: {
152+
ssr: './src/server',
153+
},
154+
ssr: {
155+
noExternal: /./,
156+
},
157+
},
158+
},
159+
} as UserConfig)
160+
```
161+
162+
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.
163+
85164
## Thanks
86165

87166
Lots of references to the following repos.

0 commit comments

Comments
 (0)