Skip to content

Commit

Permalink
fix: respect cors and proxy options in preview command
Browse files Browse the repository at this point in the history
close #2279
  • Loading branch information
yyx990803 committed Mar 15, 2021
1 parent 2cdb3f6 commit f7d85ae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BuildOptions } from './build'
import { ServerOptions } from './server'
import { createLogger, LogLevel } from './logger'
import { resolveConfig } from '.'
import { serve } from './serve'
import { preview } from './preview'

const cli = cac('vite')

Expand Down Expand Up @@ -206,7 +206,7 @@ cli
'serve',
'development'
)
await serve(config, options.port)
await preview(config, options.port)
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting preview server:\n${e.stack}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@ import { ResolvedConfig } from '.'
import { Connect } from 'types/connect'
import { resolveHttpServer } from './server/http'
import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'

export async function serve(config: ResolvedConfig, port = 5000) {
export async function preview(config: ResolvedConfig, port = 5000) {
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(config.server, app)

// cors
const { cors } = config.server
if (cors !== false) {
app.use(corsMiddleware(typeof cors === 'boolean' ? {} : cors))
}

// proxy
if (config.server.proxy) {
app.use(proxyMiddleware(httpServer, config))
}

app.use(compression())

Expand All @@ -23,15 +37,13 @@ export async function serve(config: ResolvedConfig, port = 5000) {
})
)

const server = await resolveHttpServer(config.server, app)

const options = config.server || {}
const hostname = options.host || 'localhost'
const protocol = options.https ? 'https' : 'http'
const logger = config.logger
const base = config.base

server.listen(port, () => {
httpServer.listen(port, () => {
logger.info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ export async function createServer(
// proxy
const { proxy } = serverConfig
if (proxy) {
middlewares.use(proxyMiddleware(server))
middlewares.use(proxyMiddleware(httpServer, config))
}

// base
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as http from 'http'
import { createDebugger } from '../../utils'
import httpProxy from 'http-proxy'
import { HMR_HEADER } from '../ws'
import { ViteDevServer } from '..'
import { Connect } from 'types/connect'
import { HttpProxy } from 'types/http-proxy'
import chalk from 'chalk'
import { ResolvedConfig } from '../..'

const debug = createDebugger('vite:proxy')

Expand All @@ -28,10 +28,10 @@ export interface ProxyOptions extends HttpProxy.ServerOptions {
) => void | null | undefined | false | string
}

export function proxyMiddleware({
httpServer,
config
}: ViteDevServer): Connect.NextHandleFunction {
export function proxyMiddleware(
httpServer: http.Server | null,
config: ResolvedConfig
): Connect.NextHandleFunction {
const options = config.server.proxy!

// lazy require only when proxy is used
Expand Down

0 comments on commit f7d85ae

Please sign in to comment.