Skip to content

配置项 ​

Type definitions copied verbatim from the plugin TypeScript source:

ts
export interface BuildOptions {
  /**
   * The chunk name.
   *
   * 构建的 chunk 名称。
   */
  chunk: string;
  /**
   * Global variable name.
   *
   * 全局变量名。
   */
  name: string;
  /**
   * The output format.
   *
   * 输出格式。
   */
  format?: LibraryFormats;
  /**
   * Whether to generate sourcemaps.
   *
   * 是否生成 sourcemap。
   */
  sourcemap?: boolean | 'inline' | 'hidden';
  /**
   * The exports type.
   *
   * 导出类型。
   */
  exports?: 'default' | 'named' | 'none' | 'auto';
  /**
   * Whether to minify the output.
   *
   * 是否压缩输出。
   */
  minify?: boolean | 'terser' | 'esbuild';
  /**
   * The output directory.
   *
   * 输出目录。
   */
  outDir?: string;
  /**
   * The output file name.
   *
   * 输出文件名。
   */
  fileName?: string | ((format: LibraryFormats, entryName: string) => string);
  /**
   * The plugins to use.
   *
   * 使用的插件。
   */
  plugins?: PluginOption[];

  /**
   * The resolve options.
   *
   * 解析选项。
   */
  resolve?: UserConfig['resolve'];
}

export interface Options {
  /**
   * The build options.
   *
   * 构建选项。
   */
  build: BuildOptions | BuildOptions[];
}

核心配置接口 ​

Options(顶层配置对象) ​

参数类型必填默认值说明
buildBuildOptions[] | BuildOptions是-构建任务配置(数组或单个对象)

BuildOptions(每个构建任务的配置) ​

参数类型必填默认值说明
chunkstring是-主构建输出目录(outDir)内的文件名
namestring是-全局变量名(仅在 UMD/IIFE 格式生效)
format'es' | 'cjs' | 'umd' | 'iife'否'umd'构建格式(默认 umd)
sourcemapboolean | 'inline' | 'hidden'否false是否生成 sourcemap(支持内联或隐藏模式)
exports'default' | 'named' | 'none' | 'auto'否'auto'Rollup 输出的导出类型
minifyboolean | 'terser' | 'esbuild'否false是否启用代码压缩(支持 terser 或 esbuild)
outDirstring否主构建的 outDir自定义输出目录(相对项目根目录)
fileNamestring | ((format: LibraryFormats, entryName: string) => string)否根据格式生成默认名称自定义文件名(支持模板字符串或函数)
pluginsPluginOption[]否[]额外的 Vite 插件列表
resolveUserConfig['resolve']否-透传给二次构建的 resolve 解析配置

TypeScript 定义 ​

typescript
import type { LibraryFormats, PluginOption, UserConfig } from 'vite';

export interface BuildOptions {
  /**
   * The chunk name.
   *
   * 构建的 chunk 名称。
   */
  chunk: string;
  /**
   * Global variable name.
   *
   * 全局变量名。
   */
  name: string;
  /**
   * The output format.
   *
   * 输出格式。
   */
  format?: LibraryFormats;
  /**
   * Whether to generate sourcemaps.
   *
   * 是否生成 sourcemap。
   */
  sourcemap?: boolean | 'inline' | 'hidden';
  /**
   * The exports type.
   *
   * 导出类型。
   */
  exports?: 'default' | 'named' | 'none' | 'auto';
  /**
   * Whether to minify the output.
   *
   * 是否压缩输出。
   */
  minify?: boolean | 'terser' | 'esbuild';
  /**
   * The output directory.
   *
   * 输出目录。
   */
  outDir?: string;
  /**
   * The output file name.
   *
   * 输出文件名。
   */
  fileName?: string | ((format: LibraryFormats, entryName: string) => string);
  /**
   * The plugins to use.
   *
   * 使用的插件。
   */
  plugins?: PluginOption[];

  /**
   * The resolve options.
   *
   * 解析选项。
   */
  resolve?: UserConfig['resolve'];
}

export interface Options {
  /**
   * The build options.
   *
   * 构建选项。
   */
  build: BuildOptions | BuildOptions[];
}