From f7d85ae4c25e7f9481261d691696554113c2d58b Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 15 Mar 2021 09:36:08 -0400 Subject: [PATCH] fix: respect cors and proxy options in preview command close #2279 --- packages/vite/src/node/cli.ts | 4 ++-- .../vite/src/node/{serve.ts => preview.ts} | 20 +++++++++++++++---- packages/vite/src/node/server/index.ts | 2 +- .../vite/src/node/server/middlewares/proxy.ts | 10 +++++----- 4 files changed, 24 insertions(+), 12 deletions(-) rename packages/vite/src/node/{serve.ts => preview.ts} (77%) diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index 3ca5815f551995..81b1e5d4db8bdc 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -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') @@ -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}`) diff --git a/packages/vite/src/node/serve.ts b/packages/vite/src/node/preview.ts similarity index 77% rename from packages/vite/src/node/serve.ts rename to packages/vite/src/node/preview.ts index 76b6fb1788c575..c76a68337cdfdf 100644 --- a/packages/vite/src/node/serve.ts +++ b/packages/vite/src/node/preview.ts @@ -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()) @@ -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`) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 538994c2903941..c732da062a064b 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -403,7 +403,7 @@ export async function createServer( // proxy const { proxy } = serverConfig if (proxy) { - middlewares.use(proxyMiddleware(server)) + middlewares.use(proxyMiddleware(httpServer, config)) } // base diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index 16b352e78f1cca..aadcdc06d3adb5 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -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') @@ -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