|
|
@ -3,6 +3,18 @@ import compression from 'compression'
|
|
|
|
import { resolveConfig } from '../config'
|
|
|
|
import { resolveConfig } from '../config'
|
|
|
|
import polka from 'polka'
|
|
|
|
import polka from 'polka'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function trimChar(str: string, char: string) {
|
|
|
|
|
|
|
|
while (str.charAt(0) === char) {
|
|
|
|
|
|
|
|
str = str.substring(1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (str.charAt(str.length - 1) === char) {
|
|
|
|
|
|
|
|
str = str.substring(0, str.length - 1)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return str
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface ServeOptions {
|
|
|
|
export interface ServeOptions {
|
|
|
|
root?: string
|
|
|
|
root?: string
|
|
|
|
port?: number
|
|
|
|
port?: number
|
|
|
@ -11,6 +23,7 @@ export interface ServeOptions {
|
|
|
|
export async function serve(options: ServeOptions = {}) {
|
|
|
|
export async function serve(options: ServeOptions = {}) {
|
|
|
|
const port = options.port !== undefined ? options.port : 5000
|
|
|
|
const port = options.port !== undefined ? options.port : 5000
|
|
|
|
const site = await resolveConfig(options.root, 'serve', 'production')
|
|
|
|
const site = await resolveConfig(options.root, 'serve', 'production')
|
|
|
|
|
|
|
|
const base = trimChar(site?.site?.base ?? "", "/")
|
|
|
|
|
|
|
|
|
|
|
|
const compress = compression()
|
|
|
|
const compress = compression()
|
|
|
|
const serve = sirv(site.outDir, {
|
|
|
|
const serve = sirv(site.outDir, {
|
|
|
@ -27,10 +40,19 @@ export async function serve(options: ServeOptions = {}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (base) {
|
|
|
|
|
|
|
|
polka()
|
|
|
|
|
|
|
|
.use(base, compress, serve)
|
|
|
|
|
|
|
|
.listen(port, (err: any) => {
|
|
|
|
|
|
|
|
if (err) throw err
|
|
|
|
|
|
|
|
console.log(`Built site served at http://localhost:${port}/${base}/\n`)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
polka()
|
|
|
|
polka()
|
|
|
|
.use(compress, serve)
|
|
|
|
.use(compress, serve)
|
|
|
|
.listen(port, (err: any) => {
|
|
|
|
.listen(port, (err: any) => {
|
|
|
|
if (err) throw err
|
|
|
|
if (err) throw err
|
|
|
|
console.log(`Built site served at http://localhost:${port}/\n`)
|
|
|
|
console.log(`Built site served at http://localhost:${port}/\n`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|