config.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /// <reference types="node" />
  2. import { DotenvParseOutput } from 'dotenv';
  3. import { Options as RollupPluginVueOptions } from 'rollup-plugin-vue';
  4. import { CompilerOptions, SFCStyleCompileOptions, SFCAsyncStyleCompileOptions, SFCTemplateCompileOptions } from '@vue/compiler-sfc';
  5. import { InputOptions as RollupInputOptions, OutputOptions as RollupOutputOptions, Plugin as RollupPlugin, OutputChunk } from 'rollup';
  6. import { BuildPlugin } from './build';
  7. import { Context, ServerPlugin } from './server';
  8. import { Resolver } from './resolver';
  9. import { Transform, CustomBlockTransform, IndexHtmlTransform } from './transform';
  10. import { DepOptimizationOptions } from './optimizer';
  11. import { ServerOptions } from 'https';
  12. import { Options as RollupTerserOptions } from 'rollup-plugin-terser';
  13. import { ProxiesOptions } from './server/serverPluginProxy';
  14. export declare type PreprocessLang = NonNullable<SFCStyleCompileOptions['preprocessLang']>;
  15. export declare type PreprocessOptions = SFCStyleCompileOptions['preprocessOptions'];
  16. export declare type CssPreprocessOptions = Partial<Record<PreprocessLang, PreprocessOptions>>;
  17. /**
  18. * https://github.com/koajs/cors#corsoptions
  19. */
  20. export interface CorsOptions {
  21. /**
  22. * `Access-Control-Allow-Origin`, default is request Origin header
  23. */
  24. origin?: string | ((ctx: Context) => string);
  25. /**
  26. * `Access-Control-Allow-Methods`, default is 'GET,HEAD,PUT,POST,DELETE,PATCH'
  27. */
  28. allowMethods?: string | string[];
  29. /**
  30. * `Access-Control-Expose-Headers`
  31. */
  32. exposeHeaders?: string | string[];
  33. /**
  34. * `Access-Control-Allow-Headers`
  35. */
  36. allowHeaders?: string | string[];
  37. /**
  38. * `Access-Control-Max-Age` in seconds
  39. */
  40. maxAge?: string | number;
  41. /**
  42. * `Access-Control-Allow-Credentials`, default is false
  43. */
  44. credentials?: boolean | ((ctx: Context) => boolean);
  45. /**
  46. * Add set headers to `err.header` if an error is thrown
  47. */
  48. keepHeadersOnError?: boolean;
  49. }
  50. export { Resolver, Transform };
  51. /**
  52. * Options shared between server and build.
  53. */
  54. export interface SharedConfig {
  55. /**
  56. * Project root directory. Can be an absolute path, or a path relative from
  57. * the location of the config file itself.
  58. * @default process.cwd()
  59. */
  60. root?: string;
  61. /**
  62. * Import alias. The entries can either be exact request -> request mappings
  63. * (exact, no wildcard syntax), or request path -> fs directory mappings.
  64. * When using directory mappings, the key **must start and end with a slash**.
  65. *
  66. * Example `vite.config.js`:
  67. * ``` js
  68. * module.exports = {
  69. * alias: {
  70. * // alias package names
  71. * 'react': '@pika/react',
  72. * 'react-dom': '@pika/react-dom'
  73. *
  74. * // alias a path to a fs directory
  75. * // the key must start and end with a slash
  76. * '/@foo/': path.resolve(__dirname, 'some-special-dir')
  77. * }
  78. * }
  79. * ```
  80. */
  81. alias?: Record<string, string>;
  82. /**
  83. * Function that tests a file path for inclusion as a static asset.
  84. */
  85. assetsInclude?: (file: string) => boolean;
  86. /**
  87. * Custom file transforms.
  88. */
  89. transforms?: Transform[];
  90. /**
  91. * Custom index.html transforms.
  92. */
  93. indexHtmlTransforms?: IndexHtmlTransform[];
  94. /**
  95. * Define global variable replacements.
  96. * Entries will be defined on `window` during dev and replaced during build.
  97. */
  98. define?: Record<string, any>;
  99. /**
  100. * Resolvers to map dev server public path requests to/from file system paths,
  101. * and optionally map module ids to public path requests.
  102. */
  103. resolvers?: Resolver[];
  104. /**
  105. * Configure dep optimization behavior.
  106. *
  107. * Example `vite.config.js`:
  108. * ``` js
  109. * module.exports = {
  110. * optimizeDeps: {
  111. * exclude: ['dep-a', 'dep-b']
  112. * }
  113. * }
  114. * ```
  115. */
  116. optimizeDeps?: DepOptimizationOptions;
  117. /**
  118. * Options to pass to `@vue/compiler-dom`
  119. *
  120. * https://github.com/vuejs/vue-next/blob/master/packages/compiler-core/src/options.ts
  121. */
  122. vueCompilerOptions?: CompilerOptions;
  123. /**
  124. * Configure what tags/attributes to trasnform into asset url imports,
  125. * or disable the transform altogether with `false`.
  126. */
  127. vueTransformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls'];
  128. /**
  129. * The options for template block preprocessor render.
  130. */
  131. vueTemplatePreprocessOptions?: Record<string, SFCTemplateCompileOptions['preprocessOptions']>;
  132. /**
  133. * Transform functions for Vue custom blocks.
  134. *
  135. * Example `vue.config.js`:
  136. * ``` js
  137. * module.exports = {
  138. * vueCustomBlockTransforms: {
  139. * i18n: src => `export default Comp => { ... }`
  140. * }
  141. * }
  142. * ```
  143. */
  144. vueCustomBlockTransforms?: Record<string, CustomBlockTransform>;
  145. /**
  146. * Configure what to use for jsx factory and fragment.
  147. * @default 'vue'
  148. */
  149. jsx?: 'vue' | 'preact' | 'react' | {
  150. factory?: string;
  151. fragment?: string;
  152. };
  153. /**
  154. * Environment mode
  155. */
  156. mode?: string;
  157. /**
  158. * CSS preprocess options
  159. */
  160. cssPreprocessOptions?: CssPreprocessOptions;
  161. /**
  162. * CSS modules options
  163. */
  164. cssModuleOptions?: SFCAsyncStyleCompileOptions['modulesOptions'];
  165. /**
  166. * Enable esbuild
  167. * @default true
  168. */
  169. enableEsbuild?: boolean;
  170. /**
  171. * Environment variables parsed from .env files
  172. * only ones starting with VITE_ are exposed on `import.meta.env`
  173. * @internal
  174. */
  175. env?: DotenvParseOutput;
  176. }
  177. export interface HmrConfig {
  178. protocol?: string;
  179. hostname?: string;
  180. port?: number;
  181. path?: string;
  182. }
  183. export interface ServerConfig extends SharedConfig {
  184. /**
  185. * Configure hmr websocket connection.
  186. */
  187. hmr?: HmrConfig | boolean;
  188. /**
  189. * Configure dev server hostname.
  190. */
  191. hostname?: string;
  192. port?: number;
  193. open?: boolean;
  194. /**
  195. * Configure https.
  196. */
  197. https?: boolean;
  198. httpsOptions?: ServerOptions;
  199. /**
  200. * Configure custom proxy rules for the dev server. Uses
  201. * [`koa-proxies`](https://github.com/vagusX/koa-proxies) which in turn uses
  202. * [`http-proxy`](https://github.com/http-party/node-http-proxy). Each key can
  203. * be a path Full options
  204. * [here](https://github.com/http-party/node-http-proxy#options).
  205. *
  206. * Example `vite.config.js`:
  207. * ``` js
  208. * module.exports = {
  209. * proxy: {
  210. * // string shorthand
  211. * '/foo': 'http://localhost:4567/foo',
  212. * // with options
  213. * '/api': {
  214. * target: 'http://jsonplaceholder.typicode.com',
  215. * changeOrigin: true,
  216. * rewrite: path => path.replace(/^\/api/, '')
  217. * }
  218. * }
  219. * }
  220. * ```
  221. */
  222. proxy?: Record<string, string | ProxiesOptions>;
  223. /**
  224. * Configure CORS for the dev server.
  225. * Uses [@koa/cors](https://github.com/koajs/cors).
  226. * Set to `true` to allow all methods from any origin, or configure separately
  227. * using an object.
  228. */
  229. cors?: CorsOptions | boolean;
  230. /**
  231. * A plugin function that configures the dev server. Receives a server plugin
  232. * context object just like the internal server plugins. Can also be an array
  233. * of multiple server plugin functions.
  234. */
  235. configureServer?: ServerPlugin | ServerPlugin[];
  236. }
  237. export interface BuildConfig extends Required<SharedConfig> {
  238. /**
  239. * Entry. Use this to specify a js entry file in use cases where an
  240. * `index.html` does not exist (e.g. serving vite assets from a different host)
  241. * @default 'index.html'
  242. */
  243. entry: string;
  244. /**
  245. * Base public path when served in production.
  246. * @default '/'
  247. */
  248. base: string;
  249. /**
  250. * Directory relative from `root` where build output will be placed. If the
  251. * directory exists, it will be removed before the build.
  252. * @default 'dist'
  253. */
  254. outDir: string;
  255. /**
  256. * Directory relative from `outDir` where the built js/css/image assets will
  257. * be placed.
  258. * @default '_assets'
  259. */
  260. assetsDir: string;
  261. /**
  262. * Static asset files smaller than this number (in bytes) will be inlined as
  263. * base64 strings. Default limit is `4096` (4kb). Set to `0` to disable.
  264. * @default 4096
  265. */
  266. assetsInlineLimit: number;
  267. /**
  268. * Whether to code-split CSS. When enabled, CSS in async chunks will be
  269. * inlined as strings in the chunk and inserted via dynamically created
  270. * style tags when the chunk is loaded.
  271. * @default true
  272. */
  273. cssCodeSplit: boolean;
  274. /**
  275. * Whether to generate sourcemap
  276. * @default false
  277. */
  278. sourcemap: boolean | 'inline';
  279. /**
  280. * Set to `false` to disable minification, or specify the minifier to use.
  281. * Available options are 'terser' or 'esbuild'.
  282. * @default 'terser'
  283. */
  284. minify: boolean | 'terser' | 'esbuild';
  285. /**
  286. * The option for `terser`
  287. */
  288. terserOptions: RollupTerserOptions;
  289. /**
  290. * Transpile target for esbuild.
  291. * @default 'es2020'
  292. */
  293. esbuildTarget: string;
  294. /**
  295. * Build for server-side rendering, only as a CLI flag
  296. * for programmatic usage, use `ssrBuild` directly.
  297. * @internal
  298. */
  299. ssr?: boolean;
  300. /**
  301. * Will be passed to rollup.rollup()
  302. *
  303. * https://rollupjs.org/guide/en/#big-list-of-options
  304. */
  305. rollupInputOptions: ViteRollupInputOptions;
  306. /**
  307. * Will be passed to bundle.generate()
  308. *
  309. * https://rollupjs.org/guide/en/#big-list-of-options
  310. */
  311. rollupOutputOptions: RollupOutputOptions;
  312. /**
  313. * Will be passed to rollup-plugin-vue
  314. *
  315. * https://github.com/vuejs/rollup-plugin-vue/blob/next/src/index.ts
  316. */
  317. rollupPluginVueOptions: Partial<RollupPluginVueOptions>;
  318. /**
  319. * Will be passed to @rollup/plugin-node-resolve
  320. * https://github.com/rollup/plugins/tree/master/packages/node-resolve#dedupe
  321. */
  322. rollupDedupe: string[];
  323. /**
  324. * Whether to log asset info to console
  325. * @default false
  326. */
  327. silent: boolean;
  328. /**
  329. * Whether to write bundle to disk
  330. * @default true
  331. */
  332. write: boolean;
  333. /**
  334. * Whether to emit index.html
  335. * @default true
  336. */
  337. emitIndex: boolean;
  338. /**
  339. * Whether to emit assets other than JavaScript
  340. * @default true
  341. */
  342. emitAssets: boolean;
  343. /**
  344. * Whether to emit a manifest.json under assets dir to map hash-less filenames
  345. * to their hashed versions. Useful when you want to generate your own HTML
  346. * instead of using the one generated by Vite.
  347. *
  348. * Example:
  349. *
  350. * ```json
  351. * {
  352. * "main.js": "main.68fe3fad.js",
  353. * "style.css": "style.e6b63442.css"
  354. * }
  355. * ```
  356. * @default false
  357. */
  358. emitManifest?: boolean;
  359. /**
  360. * Predicate function that determines whether a link rel=modulepreload shall be
  361. * added to the index.html for the chunk passed in
  362. */
  363. shouldPreload: ((chunk: OutputChunk) => boolean) | null;
  364. /**
  365. * Enable 'rollup-plugin-vue'
  366. * @default true
  367. */
  368. enableRollupPluginVue?: boolean;
  369. /**
  370. * Plugin functions that mutate the Vite build config. The `builds` array can
  371. * be added to if the plugin wants to add another Rollup build that Vite writes
  372. * to disk. Return a function to gain access to each build's output.
  373. * @internal
  374. */
  375. configureBuild?: BuildPlugin | BuildPlugin[];
  376. }
  377. export interface ViteRollupInputOptions extends RollupInputOptions {
  378. /**
  379. * @deprecated use `pluginsPreBuild` or `pluginsPostBuild` instead
  380. */
  381. plugins?: RollupPlugin[];
  382. /**
  383. * Rollup plugins that passed before Vite's transform plugins
  384. */
  385. pluginsPreBuild?: RollupPlugin[];
  386. /**
  387. * Rollup plugins that passed after Vite's transform plugins
  388. */
  389. pluginsPostBuild?: RollupPlugin[];
  390. /**
  391. * Rollup plugins for optimizer
  392. */
  393. pluginsOptimizer?: RollupPlugin[];
  394. }
  395. export interface UserConfig extends Partial<BuildConfig>, ServerConfig {
  396. plugins?: Plugin[];
  397. }
  398. export interface Plugin extends Pick<UserConfig, 'alias' | 'transforms' | 'indexHtmlTransforms' | 'define' | 'resolvers' | 'configureBuild' | 'configureServer' | 'vueCompilerOptions' | 'vueTransformAssetUrls' | 'vueTemplatePreprocessOptions' | 'vueCustomBlockTransforms' | 'rollupInputOptions' | 'rollupOutputOptions' | 'enableRollupPluginVue'> {
  399. }
  400. export declare type ResolvedConfig = UserConfig & {
  401. /**
  402. * Path of config file.
  403. */
  404. __path?: string;
  405. };
  406. export declare function resolveConfig(mode: string, configPath?: string): Promise<ResolvedConfig | undefined>;
  407. export declare const defaultDefines: {
  408. __VUE_OPTIONS_API__: boolean;
  409. __VUE_PROD_DEVTOOLS__: boolean;
  410. };