[ Web Proxy ]
URL:
Viewing: https://ko.vite.dev/guide/api-javascript.html [Back]  [Original]

JavaScript API | Vite
Skip to content
Menu
Return to top
Sidebar Navigation

API

On this page
    ViteConf [ViteConf ]

    ViteConf 2025

    JavaScript API

    Vite API VSCode JS .

    createServer

    :

    ts
    async function createServer(inlineConfig?: InlineConfig): Promise<ViteDevServer>

    :

    ts
    import { 
    createServer
    } from 'vite'
    const
    server
    = await
    createServer
    ({
    // `mode`, `configFile`
    configFile
    : false,
    root
    : import.meta.
    dirname
    ,
    server
    : {
    port
    : 1337,
    }, }) await
    server
    .
    listen
    ()
    server
    .
    printUrls
    ()
    server
    .
    bindCLIShortcuts
    ({
    print
    : true })

    createServer build mode process.env.NODE_ENV . Node.js , API process.env.NODE_ENV mode development . API .

    WebSocket , http middlewareMode .

    ts
    import 
    http
    from 'http'
    import {
    createServer
    } from 'vite'
    const
    parentServer
    =
    http
    .
    createServer
    () // express, koa
    const
    vite
    = await
    createServer
    ({
    server
    : {
    //
    middlewareMode
    : {
    // WebSocket http .
    server
    :
    parentServer
    ,
    },
    proxy
    : {
    '/ws': {
    target
    : 'ws://localhost:3000',
    // WebSocket
    ws
    : true,
    }, }, }, })
    parentServer
    .use(
    vite
    .
    middlewares
    )

    InlineConfig

    InlineConfig UserConfig :

    • configFile: . , Vite . false .

    ResolvedConfig

    ResolvedConfig UserConfig , undefined . :

    • config.assetsInclude: id asset .
    • config.logger: Vite (Logger) .

    ViteDevServer

    ts
    interface ViteDevServer {
      /**
       *  Vite  .
       */
      config: ResolvedConfig
      /**
       * connect  .
       * -         .
       * -  http    connect  Node.js
       *       .
       *
       * https://github.com/senchalabs/connect#use-middleware
       */
      middlewares: Connect.Server
      /**
       *  Node http  .
       *   null.
       */
      httpServer: http.Server | null
      /**
       * Chokidar watcher . `config.server.watch` `null`
       *    , `add`  `unwatch`   .
       * https://github.com/paulmillr/chokidar/tree/3.6.0#api
       */
      watcher: FSWatcher
      /**
       * `send(payload)`   WebSocket .
       */
      ws: WebSocketServer
      /**
       *         Rollup  .
       */
      pluginContainer: PluginContainer
      /**
       * import  URL- ,
       * HMR    .
       */
      moduleGraph: ModuleGraph
      /**
       * Vite CLI   URL(URL ).
       *        `null` .
       */
      resolvedUrls: ResolvedServerUrls | null
      /**
       * http       URL
       * , ,   .
       */
      transformRequest(
        url: string,
        options?: TransformOptions,
      ): Promise<TransformResult | null>
      /**
       * Vite  HTML    HTML  .
       */
      transformIndexHtml(
        url: string,
        html: string,
        originalUrl?: string,
      ): Promise<string>
      /**
       *  URL SSR   .
       */
      ssrLoadModule(
        url: string,
        options?: { fixStacktrace?: boolean },
      ): Promise<Record<string, any>>
      /**
       * SSR    .
       */
      ssrFixStacktrace(e: Error): void
      /**
       *      HMR .   
       * `server.moduleGraph` API   . `hmr` false    .
       */
      reloadModule(module: ModuleNode): Promise<void>
      /**
       *  .
       */
      listen(port?: number, isRestart?: boolean): Promise<ViteDevServer>
      /**
       *   .
       *
       * @param forceOptimize -     . --force CLI  .
       */
      restart(forceOptimize?: boolean): Promise<void>
      /**
       *  .
       */
      close(): Promise<void>
      /**
       * CLI  .
       */
      bindCLIShortcuts(options?: BindCLIShortcutsOptions<ViteDevServer>): void
      /**
       * `await server.waitForRequestsIdle(id)`    import
       *   . load  transform   
       *    id   .    
       * import        resolve.
       * @experimental
       */
      waitForRequestsIdle: (ignoredId?: string) => Promise<void>
    }

    INFO

    waitForRequestsIdle Vite ( . - ) DX . Tailwind CSS , ( Vite Tailwind - ). , HTTP1 , http . Vite , . . Vite optimizeDeps.crawlUntilStaticImports: false .

    build

    :

    ts
    async function build(
      inlineConfig?: InlineConfig,
    ): Promise<RollupOutput | RollupOutput[]>

    :

    vite.config.js
    ts
    import 
    path
    from 'node:path'
    import {
    build
    } from 'vite'
    await
    build
    ({
    root
    :
    path
    .
    resolve
    (import.meta.
    dirname
    , './project'),
    base
    : '/foo/',
    build
    : {
    rolldownOptions
    : {
    // ... }, }, })

    preview

    :

    ts
    async function preview(inlineConfig?: InlineConfig): Promise<PreviewServer>

    :

    ts
    import { 
    preview
    } from 'vite'
    const
    previewServer
    = await
    preview
    ({
    // `mode`, `configFile`
    preview
    : {
    port
    : 8080,
    open
    : true,
    }, })
    previewServer
    .
    printUrls
    ()
    previewServer
    .
    bindCLIShortcuts
    ({
    print
    : true })

    PreviewServer

    ts
    interface PreviewServer {
      /**
       *  Vite  .
       */
      config: ResolvedConfig
      /**
       * connect  .
       * -         .
       * -  http    connect  Node.js
       *       .
       *
       * https://github.com/senchalabs/connect#use-middleware
       */
      middlewares: Connect.Server
      /**
       *  Node http  .
       */
      httpServer: http.Server
      /**
       * Vite CLI   URL(URL ).
       *      `null` .
       */
      resolvedUrls: ResolvedServerUrls | null
      /**
       *  URL .
       */
      printUrls(): void
      /**
       * CLI  .
       */
      bindCLIShortcuts(options?: BindCLIShortcutsOptions<PreviewServer>): void
    }

    resolveConfig

    :

    ts
    async function resolveConfig(
      inlineConfig: InlineConfig,
      command: 'build' | 'serve',
      defaultMode = 'development',
      defaultNodeEnv = 'development',
      isPreview = false,
    ): Promise<ResolvedConfig>

    command serve, build .

    mergeConfig

    :

    ts
    function mergeConfig(
      defaults: Record<string, any>,
      overrides: Record<string, any>,
      isRoot = true,
    ): Record<string, any>

    Vite (Deep) . isRoot Vite . , build false .

    overrides null undefined . defaults mergeConfig .

    mergeConfig . , mergeConfig .

    defineConfig :

    ts
    export default 
    defineConfig
    ((
    configEnv
    ) =>
    mergeConfig
    (
    configAsCallback
    (
    configEnv
    ),
    configAsObject
    ),
    )

    searchForWorkspaceRoot

    :

    ts
    function searchForWorkspaceRoot(
      current: string,
      root = searchForPackageRoot(current),
    ): string

    : server.fs.allow

    . root :

    • package.json workspaces
      • lerna.json
      • pnpm-workspace.yaml

    loadEnv

    :

    ts
    function loadEnv(
      mode: string,
      envDir: string,
      prefixes: string | string[] = 'VITE_',
    ): Record<string, string>

    : .env Files

    envDir .env . VITE_ , prefixes .

    normalizePath

    :

    ts
    function normalizePath(id: string): string

    : Path Normalization

    Vite .

    transformWithOxc

    :

    ts
    async function transformWithOxc(
      code: string,
      filename: string,
      options?: OxcTransformOptions,
      inMap?: object,
    ): Promise<Omit<OxcTransformResult, 'errors'> & { warnings: string[] }>

    Oxc Transformer JavaScript TypeScript . Vite Oxc Transformer .

    transformWithEsbuild

    :

    ts
    async function transformWithEsbuild(
      code: string,
      filename: string,
      options?: EsbuildTransformOptions,
      inMap?: object,
    ): Promise<ESBuildTransformResult>

    Deprecated: transformWithOxc .

    esbuild JavaScript TypeScript . Vite esbuild .

    loadConfigFromFile

    :

    ts
    async function loadConfigFromFile(
      configEnv: ConfigEnv,
      configFile?: string,
      configRoot: string = process.cwd(),
      logLevel?: LogLevel,
      customLogger?: Logger,
    ): Promise<{
      path: string
      config: UserConfig
      dependencies: string[]
    } | null>

    esbuild Vite .

    preprocessCSS

    :

    ts
    async function preprocessCSS(
      code: string,
      filename: string,
      config: ResolvedConfig,
    ): Promise<PreprocessCSSResult>
    
    interface PreprocessCSSResult {
      code: string
      map?: SourceMapInput
      modules?: Record<string, string>
      deps?: Set<string>
    }

    .css, .scss, .sass, .less, .styl .stylus CSS . CSS .

    filename . filename .module.{ext} CSS , modules .

    url() image-set() URL .

    version

    : string

    Vite (: "8.0.0").

    rolldownVersion

    : string

    Vite Rolldown (: "1.0.0"). rolldown VERSION .

    esbuildVersion

    : string

    .

    rollupVersion

    : string

    .

    Pager

    Web Proxy Viewer  |  New URL  |  Original Page