diff --git a/docs/config/index.md b/docs/config/index.md index 671f28ce797b..0b4627b85354 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -8,10 +8,10 @@ - Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts` - Use `process.env.VITEST` or `mode` property on `defineConfig` (will be set to `test` if not overridden) to conditionally apply different configuration in `vite.config.ts` -To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file. +To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file, if you are importing `defineConfig` from `vite` itself. ```ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { @@ -23,7 +23,7 @@ export default defineConfig({ You can retrieve Vitest's default options to expand them if needed: ```ts -import { defineConfig, configDefaults } from 'vitest/node' +import { defineConfig, configDefaults } from 'vitest/config' export default defineConfig({ test: { @@ -77,7 +77,7 @@ By default, `vitest` does not provide global APIs for explicitness. If you prefe ```ts // vite.config.ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { @@ -101,7 +101,7 @@ If you are already using [`unplugin-auto-import`](https://github.com/antfu/unplu ```ts // vite.config.ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ @@ -350,7 +350,7 @@ Vite plugins will receive `ssr: false` flag when processing those files. When you use JSX as component models other than React (e.g. Vue JSX or SolidJS), you might want to config as following to make `.tsx` / `.jsx` transformed as client-side components: ```ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { diff --git a/docs/guide/features.md b/docs/guide/features.md index 7b9292b6d00c..88e32ace01c9 100644 --- a/docs/guide/features.md +++ b/docs/guide/features.md @@ -212,7 +212,7 @@ After that, change the `environment` option in your config file: ```ts // vite.config.ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { @@ -238,7 +238,7 @@ To configure it, set `test.coverage` options in your config file: ```ts // vite.config.ts -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { diff --git a/docs/guide/index.md b/docs/guide/index.md index 361005fa2eb3..7086a9fcb70f 100644 --- a/docs/guide/index.md +++ b/docs/guide/index.md @@ -37,11 +37,10 @@ One of the main advantages of Vitest is its unified configuration with Vite. If - Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts` - Use `process.env.VITEST` or `mode` property on `defineConfig` (will be set to `test` if not overridden) to conditionally apply different configuration in `vite.config.ts` -To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file. +To configure `vitest` itself, add `test` property in your Vite config. You'll also need to add a reference to Vitest types using a [triple slash command](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) at the top of your config file, if you are importing `defineConfig` from `vite` itself. ```ts -/// -import { defineConfig } from 'vitest/node' +import { defineConfig } from 'vitest/config' export default defineConfig({ test: { diff --git a/packages/vitest/package.json b/packages/vitest/package.json index 36b89578fd78..516e72e87758 100644 --- a/packages/vitest/package.json +++ b/packages/vitest/package.json @@ -35,6 +35,11 @@ "./node": { "import": "./dist/node.js", "types": "./dist/node.d.ts" + }, + "./config": { + "import": "./dist/config.js", + "require": "./dist/config.cjs", + "types": "./dist/config.d.ts" } }, "main": "./dist/index.js", @@ -127,4 +132,4 @@ "engines": { "node": ">=14.14.0" } -} +} \ No newline at end of file diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index d58e7bdb68f8..636abeb067ef 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -24,6 +24,7 @@ const entries = [ const dtsEntries = [ 'src/index.ts', 'src/node.ts', + 'src/config.ts', ] const external = [ @@ -34,6 +35,25 @@ const external = [ 'c8', ] +const plugins = [ + alias({ + entries: [ + { find: /^node:(.+)$/, replacement: '$1' }, + { find: 'vite-node/server', replacement: path.resolve(__dirname, '../vite-node/src/server.ts') }, + { find: 'vite-node/client', replacement: path.resolve(__dirname, '../vite-node/src/client.ts') }, + { find: 'vite-node/utils', replacement: path.resolve(__dirname, '../vite-node/src/utils.ts') }, + ], + }), + resolve({ + preferBuiltins: true, + }), + json(), + commonjs(), + esbuild({ + target: 'node14', + }), +] + export default ({ watch }) => [ { input: entries, @@ -44,22 +64,7 @@ export default ({ watch }) => [ }, external, plugins: [ - alias({ - entries: [ - { find: /^node:(.+)$/, replacement: '$1' }, - { find: 'vite-node/server', replacement: path.resolve(__dirname, '../vite-node/src/server.ts') }, - { find: 'vite-node/client', replacement: path.resolve(__dirname, '../vite-node/src/client.ts') }, - { find: 'vite-node/utils', replacement: path.resolve(__dirname, '../vite-node/src/utils.ts') }, - ], - }), - resolve({ - preferBuiltins: true, - }), - json(), - commonjs(), - esbuild({ - target: 'node14', - }), + ...plugins, !watch && licensePlugin(), ], onwarn(message) { @@ -68,6 +73,21 @@ export default ({ watch }) => [ console.error(message) }, }, + { + input: 'src/config.ts', + output: [ + { + file: 'dist/config.cjs', + format: 'cjs', + }, + { + file: 'dist/config.js', + format: 'esm', + }, + ], + external, + plugins, + }, ...dtsEntries.map(input => ({ input, output: { diff --git a/packages/vitest/src/config.ts b/packages/vitest/src/config.ts new file mode 100644 index 000000000000..ca7f01e85ea9 --- /dev/null +++ b/packages/vitest/src/config.ts @@ -0,0 +1,6 @@ +import type { UserConfig } from 'vite' + +export { configDefaults } from './defaults' +export function defineConfig(config: UserConfig) { + return config +} diff --git a/packages/vitest/src/node/index.ts b/packages/vitest/src/node/index.ts index 2451a9a20494..5a492af4ce05 100644 --- a/packages/vitest/src/node/index.ts +++ b/packages/vitest/src/node/index.ts @@ -1,10 +1,3 @@ -import type { UserConfig } from 'vite' - -export { configDefaults } from '../defaults' -export function defineConfig(config: UserConfig) { - return config -} - export type { Vitest } from './core' export { createVitest } from './create' export { VitestPlugin } from './plugins' diff --git a/tsconfig.json b/tsconfig.json index db5ba4930168..631fbc6f097d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,7 @@ "vitest/global": ["./packages/vitest/global.d.ts"], "vitest/globals": ["./packages/vitest/globals.d.ts"], "vitest/node": ["./packages/vitest/src/node/index.ts"], + "vitest/config": ["./packages/vitest/src/config.ts"], "vite-node": ["./packages/vite-node/src/index.ts"], "vite-node/client": ["./packages/vite-node/src/client.ts"], "vite-node/server": ["./packages/vite-node/src/server.ts"],