From 05b17dfcdf0c6d792a7ec5d9cf9a11e17ac79b6c Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Sun, 10 Jul 2022 00:29:53 +0800 Subject: [PATCH] docs: update api-javascript (#8999) --- docs/guide/api-javascript.md | 92 ++++++++++++++++++++++- packages/vite/src/node/optimizer/index.ts | 3 +- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/docs/guide/api-javascript.md b/docs/guide/api-javascript.md index 6167bd7dfe7793..205d32a6070cda 100644 --- a/docs/guide/api-javascript.md +++ b/docs/guide/api-javascript.md @@ -44,6 +44,13 @@ The `InlineConfig` interface extends `UserConfig` with additional properties: - `configFile`: specify config file to use. If not set, Vite will try to automatically resolve one from project root. Set to `false` to disable auto resolving. - `envFile`: Set to `false` to disable `.env` files. +## `ResolvedConfig` + +The `ResolvedConfig` interface has all the same properties of a `UserConfig`, except most properties are resolved and non-undefined. It also contains utilities like: + +- `config.assetsInclude`: A function to check if an `id` is considered an asset. +- `config.logger`: Vite's internal logger object. + ## `ViteDevServer` ```ts @@ -189,12 +196,74 @@ import { preview } from 'vite' async function resolveConfig( inlineConfig: InlineConfig, command: 'build' | 'serve', - defaultMode?: string + defaultMode = 'development' ): Promise ``` The `command` value is `serve` in dev (in the cli `vite`, `vite dev`, and `vite serve` are aliases). +## `mergeConfig` + +**Type Signature:** + +```ts +function mergeConfig( + defaults: Record, + overrides: Record, + isRoot = true +): Record +``` + +Deeply merge two Vite configs. `isRoot` represents the level within the Vite config which is being merged. For example, set `false` if you're merging two `build` options. + +## `searchForWorkspaceRoot` + +**Type Signature:** + +```ts +function searchForWorkspaceRoot( + current: string, + root = searchForPackageRoot(current) +): string +``` + +**Related:** [server.fs.allow](/config/server-options.md#server-fs-allow) + +Search for the root of the potential workspace if it meets the following conditions, otherwise it would fallback to `root`: + +- contains `workspaces` field in `package.json` +- contains one of the following file + - `lerna.json` + - `pnpm-workspace.yaml` + +## `loadEnv` + +**Type Signature:** + +```ts +function loadEnv( + mode: string, + envDir: string, + prefixes: string | string[] = 'VITE_' +): Record +``` + +**Related:** [`.env` Files](./env-and-mode.md#env-files) + +Load `.env` files within the `envDir`. By default only env variables prefixed with `VITE_` are loaded, unless `prefixes` is changed. + +## `normalizePath` + +**Type Signature:** + +```ts +function normalizePath(id: string): string +``` + +**Related:** [Path Normalization](./api-plugin.md#path-normalization) + +Normalizes a path to interoperate between Vite plugins. + ## `transformWithEsbuild` **Type Signature:** @@ -207,3 +276,24 @@ async function transformWithEsbuild( inMap?: object ): Promise ``` + +Transform JavaScript or TypeScript with esbuild. Useful for plugins that prefers matching Vite's internal esbuild transform. + +## `loadConfigFromFile` + +**Type Signature:** + +```ts +async function loadConfigFromFile( + configEnv: ConfigEnv, + configFile?: string, + configRoot: string = process.cwd(), + logLevel?: LogLevel +): Promise<{ + path: string + config: UserConfig + dependencies: string[] +} | null> +``` + +Load a Vite config file manually with esbuild. diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 267a59e2f9b39a..4cbd13f8d965a7 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -214,7 +214,8 @@ export interface DepOptimizationMetadata { } /** - * Used by Vite CLI when running `vite optimize` + * Scan and optimize dependencies within a project. + * Used by Vite CLI when running `vite optimize`. */ export async function optimizeDeps( config: ResolvedConfig,