From ce7927d0df0f4dd55b17463cf1d15a1070831354 Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 23 Mar 2022 14:06:30 +0800 Subject: [PATCH] fix: type error (#1002) --- packages/vitest/rollup.config.js | 30 ++++++++++++------- packages/vitest/src/config.ts | 4 +-- packages/vitest/src/defaults.ts | 5 +++- .../custom-reporter.vitest.config.ts | 8 ++--- test/reporters/tsconfig.json | 9 ++++++ 5 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 test/reporters/tsconfig.json diff --git a/packages/vitest/rollup.config.js b/packages/vitest/rollup.config.js index 38f19185a368..1b737688060c 100644 --- a/packages/vitest/rollup.config.js +++ b/packages/vitest/rollup.config.js @@ -9,6 +9,7 @@ import alias from '@rollup/plugin-alias' import license from 'rollup-plugin-license' import c from 'picocolors' import fg from 'fast-glob' +import { defineConfig } from 'rollup' import pkg from './package.json' @@ -101,17 +102,24 @@ export default ({ watch }) => [ external, plugins, }, - ...dtsEntries.map(input => ({ - input, - output: { - file: input.replace('src/', 'dist/').replace('.ts', '.d.ts'), - format: 'esm', - }, - external, - plugins: [ - dts({ respectExternal: true }), - ], - })), + ...dtsEntries.map((input) => { + const _external = external + // index is vitest default types export + if (!input.includes('index')) + _external.push('vitest') + + return defineConfig({ + input, + output: { + file: input.replace('src/', 'dist/').replace('.ts', '.d.ts'), + format: 'esm', + }, + external: _external, + plugins: [ + dts({ respectExternal: true }), + ], + }) + }), ] function licensePlugin() { diff --git a/packages/vitest/src/config.ts b/packages/vitest/src/config.ts index ddd587d8fcbc..bc33c934a7bc 100644 --- a/packages/vitest/src/config.ts +++ b/packages/vitest/src/config.ts @@ -1,10 +1,10 @@ import type { UserConfig as ViteUserConfig } from 'vite' -import type { InlineConfig } from './types' export interface UserConfig extends ViteUserConfig { - test?: InlineConfig + test?: ViteUserConfig['test'] } +// will import vitest declare test in module 'vite' export { configDefaults } from './defaults' export function defineConfig(config: UserConfig) { diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 284cb198ec35..6a53b23fec26 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -1,4 +1,7 @@ -import type { ResolvedC8Options, UserConfig } from './types' +// rollup dts building will external vitest +// so output dts entry using vitest to import internal types +// eslint-disable-next-line no-restricted-imports +import type { ResolvedC8Options, UserConfig } from 'vitest' export const defaultInclude = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'] export const defaultExclude = ['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**'] diff --git a/test/reporters/custom-reporter.vitest.config.ts b/test/reporters/custom-reporter.vitest.config.ts index 302e1e0b2a43..8a9dc7cae073 100644 --- a/test/reporters/custom-reporter.vitest.config.ts +++ b/test/reporters/custom-reporter.vitest.config.ts @@ -1,7 +1,5 @@ -import type { Reporter } from 'vitest' -import type { Vitest } from 'vitest/src/node' - -import { defineConfig } from 'vite' +import type { Reporter, Vitest } from 'vitest' +import { defineConfig } from 'vitest/config' class TestReporter implements Reporter { ctx!: Vitest @@ -18,6 +16,6 @@ class TestReporter implements Reporter { export default defineConfig({ test: { include: ['tests/reporters.spec.ts'], - reporters: new TestReporter(), + reporters: [new TestReporter()], }, }) diff --git a/test/reporters/tsconfig.json b/test/reporters/tsconfig.json new file mode 100644 index 000000000000..8e64e567ddc8 --- /dev/null +++ b/test/reporters/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "paths": { + "vitest": ["./packages/vitest/index.d.ts"], + "vitest/node": ["./packages/vitest/node.d.ts"], + "vitest/config": ["./packages/vitest/config.d.ts"], + } + } +}