Skip to content

Commit

Permalink
docs: update api-javascript (#8999)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jul 9, 2022
1 parent 2a9bc6d commit 05b17df
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
92 changes: 91 additions & 1 deletion docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,12 +196,74 @@ import { preview } from 'vite'
async function resolveConfig(
inlineConfig: InlineConfig,
command: 'build' | 'serve',
defaultMode?: string
defaultMode = 'development'
): Promise<ResolvedConfig>
```

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<string, any>,
overrides: Record<string, any>,
isRoot = true
): Record<string, any>
```

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<string, string>
```

**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:**
Expand All @@ -207,3 +276,24 @@ async function transformWithEsbuild(
inMap?: object
): Promise<ESBuildTransformResult>
```

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.
3 changes: 2 additions & 1 deletion packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 05b17df

Please sign in to comment.