Skip to content

Commit 67868bd

Browse files
authored
feat: add serve command (#136)
1 parent e54c5dd commit 67868bd

File tree

8 files changed

+110
-6
lines changed

8 files changed

+110
-6
lines changed

bin/vitepress.js

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ if (!command || command === 'dev') {
3131
console.error(chalk.red(`build error:\n`), err)
3232
process.exit(1)
3333
})
34+
} else if (command === 'serve') {
35+
require('../dist/node')
36+
.serve(argv)
37+
.catch((err) => {
38+
console.error(chalk.red(`failed to start server. error:\n`), err)
39+
process.exit(1)
40+
})
3441
} else {
3542
console.log(chalk.red(`unknown command "${command}".`))
3643
process.exit(1)

docs/.vitepress/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ module.exports = {
2525
{ text: 'What is VitePress?', link: '/' },
2626
{ text: 'Getting Started', link: '/guide/getting-started' },
2727
{ text: 'Configuration', link: '/guide/configuration' },
28-
{ text: 'Customization', link: '/guide/customization' }
28+
{ text: 'Customization', link: '/guide/customization' },
29+
{ text: 'Deploying', link: '/guide/deploy' }
2930
]
3031
}
3132
],

docs/guide/deploy.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Deploying
2+
3+
The following guides are based on some shared assumptions:
4+
5+
- You are placing your docs inside the `docs` directory of your project;
6+
- You are using the default build output location (`.vitepress/dist`);
7+
- VuePress is installed as a local dependency in your project, and you have setup the following npm scripts:
8+
9+
```json
10+
{
11+
"scripts": {
12+
"docs:build": "vuepress build docs",
13+
"docs:serve": "vuepress serve docs"
14+
}
15+
}
16+
```
17+
18+
## Building The Docs
19+
20+
You may run `yarn docs:build` command to build the docs.
21+
22+
```bash
23+
$ yarn docs:build
24+
```
25+
26+
By default, the build output will be placed at `.vitepress/dist`. You may deploy this `dist` folder to any of your preferred platforms.
27+
28+
### Testing The Docs Locally
29+
30+
Once you've built the docs, you may test them locally by running `yarn docs:serve` command.
31+
32+
```bash
33+
$ yarn docs:build
34+
$ yarn docs:serve
35+
```
36+
37+
The `serve` command will boot up local static web server that serves the files from `.vitepress/dist` at http://localhost:3000. It's an easy way to check if the production build looks OK in your local environment.
38+
39+
You may configure the port of the server py passing `--port` flag as an argument.
40+
41+
```json
42+
{
43+
"scripts": {
44+
"docs:serve": "vuepress serve docs --port 8080"
45+
}
46+
}
47+
```
48+
49+
Now the `docs:serve` method will launch the server at http://localhost:8080.

docs/guide/getting-started.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ This section will help you build a basic VuePress documentation site from ground
3232
{
3333
"scripts": {
3434
"docs:dev": "vitepress dev docs",
35-
"docs:build": "vitepress build docs"
35+
"docs:build": "vitepress build docs",
36+
"docs:serve": "vitepress serve docs"
3637
}
3738
}
3839
```
@@ -44,3 +45,7 @@ This section will help you build a basic VuePress documentation site from ground
4445
```
4546

4647
VitePress will start a hot-reloading development server at http://localhost:3000.
48+
49+
By now, you should have a basic but functional VuePress documentation site.
50+
51+
When your documentation site starts to take shape, be sure to read the [deployment guide](./deploy).

package.json

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
"dev-watch": "node scripts/watchAndCopy",
2323
"build": "rimraf -rf dist && node scripts/copyShared && tsc -p src/client && tsc -p src/node && node scripts/copyClient",
2424
"lint": "yarn lint:js && yarn lint:ts",
25-
"lint:js": "prettier --check --write \"{bin,scripts,src}/**/*.js\"",
26-
"lint:ts": "prettier --check --write --parser typescript \"{src,types}/**/*.ts\"",
25+
"lint:js": "prettier --check --write \"{bin,docs,scripts,src}/**/*.js\"",
26+
"lint:ts": "prettier --check --write --parser typescript \"{src,docs,types}/**/*.ts\"",
2727
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
2828
"release": "bash scripts/release.sh",
2929
"docs": "run-p dev docs-dev",
3030
"docs-dev": "node ./bin/vitepress dev docs",
31-
"docs-build": "yarn build && node ./bin/vitepress build docs"
31+
"docs-build": "yarn build && node ./bin/vitepress build docs",
32+
"docs-serve": "yarn docs-build && node ./bin/vitepress serve --root docs"
3233
},
3334
"engines": {
3435
"node": ">=10.0.0"
@@ -85,12 +86,16 @@
8586
},
8687
"devDependencies": {
8788
"@types/fs-extra": "^9.0.1",
89+
"@types/koa": "^2.11.6",
90+
"@types/koa-static": "^4.0.1",
8891
"@types/lru-cache": "^5.1.0",
8992
"@types/markdown-it": "^10.0.2",
9093
"@types/node": "^13.13.4",
9194
"@types/postcss-load-config": "^2.0.1",
9295
"chokidar": "^3.4.2",
9396
"conventional-changelog-cli": "^2.1.0",
97+
"koa": "^2.13.0",
98+
"koa-static": "^5.0.0",
9499
"lint-staged": "^10.3.0",
95100
"npm-run-all": "^4.1.5",
96101
"prettier": "^2.0.5",

src/node/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './server'
22
export * from './build/build'
3+
export * from './serve/serve'
34
export * from './config'

src/node/serve/serve.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Koa from 'koa'
2+
import koaServe from 'koa-static'
3+
import { resolveConfig } from '../config'
4+
5+
export interface ServeOptions {
6+
root?: string
7+
port?: number
8+
}
9+
10+
export async function serve(options: ServeOptions = {}) {
11+
const port = options.port !== undefined ? options.port : 3000
12+
const site = await resolveConfig(options.root)
13+
14+
const app = new Koa()
15+
16+
app.use(koaServe(site.outDir))
17+
18+
app.listen(port)
19+
20+
console.log(`listening at http://localhost:${port}`)
21+
}

yarn.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,22 @@
209209
dependencies:
210210
"@types/koa" "*"
211211

212-
"@types/koa@*", "@types/koa@^2.11.4":
212+
"@types/koa-send@*":
213+
version "4.1.2"
214+
resolved "https://registry.yarnpkg.com/@types/koa-send/-/koa-send-4.1.2.tgz#978f8267ad116d12ac6a18fecd8f34c5657e09ad"
215+
integrity sha512-rfqKIv9bFds39Jxvsp8o3YJLnEQVPVriYA14AuO2OY65IHh/4UX4U/iMs5L0wATpcRmm1bbe0BNk23TRwx3VQQ==
216+
dependencies:
217+
"@types/koa" "*"
218+
219+
"@types/koa-static@^4.0.1":
220+
version "4.0.1"
221+
resolved "https://registry.yarnpkg.com/@types/koa-static/-/koa-static-4.0.1.tgz#b740d80a549b0a0a7a3b38918daecde88a7a50ec"
222+
integrity sha512-SSpct5fEcAeRkBHa3RiwCIRfDHcD1cZRhwRF///ZfvRt8KhoqRrhK6wpDlYPk/vWHVFE9hPGqh68bhzsHkir4w==
223+
dependencies:
224+
"@types/koa" "*"
225+
"@types/koa-send" "*"
226+
227+
"@types/koa@*", "@types/koa@^2.11.4", "@types/koa@^2.11.6":
213228
version "2.11.6"
214229
resolved "https://registry.yarnpkg.com/@types/koa/-/koa-2.11.6.tgz#b7030caa6b44af801c2aea13ba77d74aff7484d5"
215230
integrity sha512-BhyrMj06eQkk04C97fovEDQMpLpd2IxCB4ecitaXwOKGq78Wi2tooaDOWOFGajPk8IkQOAtMppApgSVkYe1F/A==

0 commit comments

Comments
 (0)