Skip to content

Commit 2a8520a

Browse files
janis-mehi-ogawa
andauthored
feat: support --configLoader CLI option (#7574)
Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent 1ef31a7 commit 2a8520a

File tree

18 files changed

+105
-4
lines changed

18 files changed

+105
-4
lines changed

docs/.vitepress/scripts/cli-generator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const skipConfig = new Set([
3030
'coverage.thresholds.lines',
3131
'standalone',
3232
'clearScreen',
33+
'configLoader',
3334
'color',
3435
'run',
3536
'hideSkippedTests',

docs/advanced/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The ["Running Tests"](/advanced/guide/tests#startvitest) guide has a usage examp
6161
```ts
6262
function createVitest(
6363
mode: VitestRunMode,
64-
options: UserConfig,
64+
options: CliOptions,
6565
viteOverrides: ViteUserConfig = {},
6666
vitestOptions: VitestOptions = {},
6767
): Promise<Vitest>

docs/guide/cli-generated.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,12 @@ Removes colors from the console output
919919

920920
Clear terminal screen when re-running tests during watch mode (default: `true`)
921921

922+
### configLoader
923+
924+
- **CLI:** `--configLoader <loader>`
925+
926+
Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly. This is only available in vite version 6.1.0 and above. (default: `bundle`)
927+
922928
### standalone
923929

924930
- **CLI:** `--standalone`

packages/browser/src/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function createBrowserServer(
5858
},
5959
mode: project.config.mode,
6060
configFile: configPath,
61+
configLoader: project.vite.config.inlineConfig.configLoader,
6162
// watch is handled by Vitest
6263
server: {
6364
hmr: false,

packages/vitest/src/node/cli/cli-api.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UserConfig as ViteUserConfig } from 'vite'
1+
import type { InlineConfig as ViteInlineConfig, UserConfig as ViteUserConfig } from 'vite'
22
import type { environments } from '../../integrations/env'
33
import type { Vitest, VitestOptions } from '../core'
44
import type { TestModule, TestSuite } from '../reporters/reported-tasks'
@@ -28,6 +28,14 @@ export interface CliOptions extends UserConfig {
2828
* Output collected test files only
2929
*/
3030
filesOnly?: boolean
31+
32+
/**
33+
* Override vite config's configLoader from cli.
34+
* Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly (default: `bundle`).
35+
* This is only available with **vite version 6.1.0** and above.
36+
* @experimental
37+
*/
38+
configLoader?: ViteInlineConfig extends { configLoader?: infer T } ? T : never
3139
}
3240

3341
/**

packages/vitest/src/node/cli/cli-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ export const cliOptionsConfig: VitestCLIOptions = {
806806
description:
807807
'Clear terminal screen when re-running tests during watch mode (default: `true`)',
808808
},
809+
configLoader: {
810+
description:
811+
'Use `bundle` to bundle the config with esbuild or `runner` (experimental) to process it on the fly. This is only available in vite version 6.1.0 and above. (default: `bundle`)',
812+
argument: '<loader>',
813+
},
809814
standalone: {
810815
description:
811816
'Start Vitest without running tests. File filters will be ignored, tests will be running only on change (default: `false`)',

packages/vitest/src/node/create.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import type {
22
InlineConfig as ViteInlineConfig,
33
UserConfig as ViteUserConfig,
44
} from 'vite'
5+
import type { CliOptions } from './cli/cli-api'
56
import type { VitestOptions } from './core'
6-
import type { UserConfig, VitestRunMode } from './types/config'
7+
import type { VitestRunMode } from './types/config'
78
import { resolve } from 'node:path'
89
import { slash } from '@vitest/utils'
910
import { findUp } from 'find-up'
@@ -15,7 +16,7 @@ import { createViteServer } from './vite'
1516

1617
export async function createVitest(
1718
mode: VitestRunMode,
18-
options: UserConfig,
19+
options: CliOptions,
1920
viteOverrides: ViteUserConfig = {},
2021
vitestOptions: VitestOptions = {},
2122
): Promise<Vitest> {
@@ -33,6 +34,7 @@ export async function createVitest(
3334

3435
const config: ViteInlineConfig = {
3536
configFile: configPath,
37+
configLoader: options.configLoader,
3638
// this will make "mode": "test" | "benchmark" inside defineConfig
3739
mode: options.mode || mode,
3840
plugins: await VitestPlugin(options, ctx),

packages/vitest/src/node/project.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ export async function initializeProject(
742742
const config: ViteInlineConfig = {
743743
...restOptions,
744744
configFile,
745+
configLoader: ctx.vite.config.inlineConfig.configLoader,
745746
// this will make "mode": "test" | "benchmark" inside defineConfig
746747
mode: options.test?.mode || options.mode || ctx.config.mode,
747748
plugins: [

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/cli/deps/linked/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'ok' satisfies string

0 commit comments

Comments
 (0)