Skip to content

Commit

Permalink
docs: add full control build
Browse files Browse the repository at this point in the history
  • Loading branch information
axiaoan committed Apr 11, 2022
1 parent de6e6b1 commit f55a157
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit f55a157

Please sign in to comment.