mirror of
https://github.com/vitejs/vite.git
synced 2024-11-22 07:09:05 +00:00
refactor: make createServer expose raw server
This commit is contained in:
parent
1c95964771
commit
df93fdac27
24
bin/vite.js
24
bin/vite.js
@ -1,9 +1,23 @@
|
||||
#!/usr/bin/env node
|
||||
const { createServer } = require('../dist/server')
|
||||
const argv = require('minimist')(process.argv.slice(2))
|
||||
const server = require('../dist/server').createServer(argv)
|
||||
|
||||
if (argv._.length) {
|
||||
argv.cwd = require('path').resolve(process.cwd(), argv._[0])
|
||||
}
|
||||
let port = argv.port || 3000
|
||||
|
||||
createServer(argv)
|
||||
server.on('error', (e) => {
|
||||
if (e.code === 'EADDRINUSE') {
|
||||
console.log(`port ${port} is in use, trying another one...`)
|
||||
setTimeout(() => {
|
||||
server.close()
|
||||
server.listen(++port)
|
||||
}, 100)
|
||||
} else {
|
||||
console.error(e)
|
||||
}
|
||||
})
|
||||
|
||||
server.on('listening', () => {
|
||||
console.log(`Running at http://localhost:${port}`)
|
||||
})
|
||||
|
||||
server.listen(port)
|
||||
|
@ -16,7 +16,7 @@
|
||||
"dev": "run-p dev-client dev-server",
|
||||
"dev-client": "tsc -w --p src/client",
|
||||
"dev-server": "tsc -w --p src/server",
|
||||
"build": "tsc -p src/client && tsc -p src/server",
|
||||
"build": "rm -rf dist && tsc -p src/client && tsc -p src/server",
|
||||
"lint": "prettier --write --parser typescript \"src/**/*.ts\"",
|
||||
"test": "yarn build && jest",
|
||||
"prepublishOnly": "yarn build"
|
||||
|
@ -1,4 +1,5 @@
|
||||
import http, { Server } from 'http'
|
||||
import https from 'https'
|
||||
import Koa from 'koa'
|
||||
import { hmrMiddleware } from './middlewares/hmr'
|
||||
import { moduleResolverMiddleware } from './middlewares/modules'
|
||||
@ -15,8 +16,8 @@ export interface MiddlewareCtx {
|
||||
}
|
||||
|
||||
export interface ServerConfig {
|
||||
port?: number
|
||||
cwd?: string
|
||||
https?: boolean
|
||||
middlewares?: Middleware[]
|
||||
}
|
||||
|
||||
@ -28,13 +29,15 @@ const middlewares: Middleware[] = [
|
||||
serveMiddleware
|
||||
]
|
||||
|
||||
export async function createServer({
|
||||
port = 3000,
|
||||
export function createServer({
|
||||
cwd = process.cwd(),
|
||||
middlewares: userMiddlewares = []
|
||||
}: ServerConfig = {}): Promise<Server> {
|
||||
middlewares: userMiddlewares = [],
|
||||
https: useHttps = false
|
||||
}: ServerConfig = {}): Server {
|
||||
const app = new Koa()
|
||||
const server = http.createServer(app.callback())
|
||||
const server = useHttps
|
||||
? https.createServer(app.callback())
|
||||
: http.createServer(app.callback())
|
||||
|
||||
;[...userMiddlewares, ...middlewares].forEach((m) =>
|
||||
m({
|
||||
@ -44,25 +47,5 @@ export async function createServer({
|
||||
})
|
||||
)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
server.on('error', (e: Error & { code: string }) => {
|
||||
if (e.code === 'EADDRINUSE') {
|
||||
console.log(`port ${port} is in use, trying another one...`)
|
||||
setTimeout(() => {
|
||||
server.close()
|
||||
server.listen(++port)
|
||||
}, 100)
|
||||
} else {
|
||||
console.error(e)
|
||||
reject(e)
|
||||
}
|
||||
})
|
||||
|
||||
server.on('listening', () => {
|
||||
console.log(`Running at http://localhost:${port}`)
|
||||
resolve(server)
|
||||
})
|
||||
|
||||
server.listen(port)
|
||||
})
|
||||
return server
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Middleware } from '../index'
|
||||
|
||||
export const historyFallbackMiddleware: Middleware = ({ cwd, app }) => {
|
||||
export const historyFallbackMiddleware: Middleware = ({ app }) => {
|
||||
app.use((ctx, next) => {
|
||||
const cleanUrl = ctx.url.split('?')[0].split('#')[0]
|
||||
if (ctx.method !== 'GET' || cleanUrl.includes('.')) {
|
||||
|
@ -10,10 +10,6 @@
|
||||
"noUnusedLocals": true,
|
||||
"strictNullChecks": true,
|
||||
"noImplicitAny": true,
|
||||
"removeComments": false,
|
||||
"lib": [
|
||||
"esnext",
|
||||
"DOM"
|
||||
]
|
||||
"removeComments": false
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user