Skip to content

Commit 56dbfa6

Browse files
authored
refactor: make a distinction between node and runtime types (#6214)
1 parent 1affb99 commit 56dbfa6

File tree

125 files changed

+866
-778
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+866
-778
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
},
6868
"pnpm": {
6969
"overrides": {
70+
"acorn": "8.11.3",
7071
"mlly": "^1.7.1",
7172
"rollup": "$rollup",
7273
"vite": "$vite",

packages/vitest/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,27 @@ License: MIT
438438
By: Mathias Bynens
439439
Repository: https://github.com/mathiasbynens/emoji-regex.git
440440

441+
> Copyright Mathias Bynens <https://mathiasbynens.be/>
442+
>
443+
> Permission is hereby granted, free of charge, to any person obtaining
444+
> a copy of this software and associated documentation files (the
445+
> "Software"), to deal in the Software without restriction, including
446+
> without limitation the rights to use, copy, modify, merge, publish,
447+
> distribute, sublicense, and/or sell copies of the Software, and to
448+
> permit persons to whom the Software is furnished to do so, subject to
449+
> the following conditions:
450+
>
451+
> The above copyright notice and this permission notice shall be
452+
> included in all copies or substantial portions of the Software.
453+
>
454+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
455+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
456+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
457+
> NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
458+
> LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
459+
> OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
460+
> WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
461+
441462
---------------------------------------
442463

443464
## expect-type

packages/vitest/rollup.config.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs'
22
import { builtinModules, createRequire } from 'node:module'
33
import { fileURLToPath } from 'node:url'
4-
import { dirname, join, normalize, relative, resolve } from 'pathe'
4+
import { dirname, join, normalize, resolve } from 'pathe'
55
import esbuild from 'rollup-plugin-esbuild'
66
import dts from 'rollup-plugin-dts'
77
import nodeResolve from '@rollup/plugin-node-resolve'
@@ -17,13 +17,13 @@ const pkg = require('./package.json')
1717

1818
const entries = {
1919
'path': 'src/paths.ts',
20-
'index': 'src/index.ts',
20+
'index': 'src/public/index.ts',
2121
'cli': 'src/node/cli.ts',
22-
'node': 'src/node.ts',
22+
'node': 'src/public/node.ts',
2323
'suite': 'src/suite.ts',
2424
'browser': 'src/browser.ts',
2525
'runners': 'src/runners.ts',
26-
'environments': 'src/environments.ts',
26+
'environments': 'src/public/environments.ts',
2727
'spy': 'src/integrations/spy.ts',
2828
'coverage': 'src/coverage.ts',
2929
'utils': 'src/public/utils.ts',
@@ -45,9 +45,9 @@ const entries = {
4545
}
4646

4747
const dtsEntries = {
48-
index: 'src/index.ts',
49-
node: 'src/node.ts',
50-
environments: 'src/environments.ts',
48+
index: 'src/public/index.ts',
49+
node: 'src/public/node.ts',
50+
environments: 'src/public/environments.ts',
5151
browser: 'src/browser.ts',
5252
runners: 'src/runners.ts',
5353
suite: 'src/suite.ts',
@@ -107,35 +107,7 @@ export default ({ watch }) =>
107107
output: {
108108
dir: 'dist',
109109
format: 'esm',
110-
chunkFileNames: (chunkInfo) => {
111-
let id
112-
= chunkInfo.facadeModuleId
113-
|| Object.keys(chunkInfo.moduleIds).find(
114-
i =>
115-
!i.includes('node_modules')
116-
&& (i.includes('src/') || i.includes('src\\')),
117-
)
118-
if (id) {
119-
id = normalize(id)
120-
const parts = Array.from(
121-
new Set(
122-
relative(process.cwd(), id)
123-
.split(/\//g)
124-
.map(i => i.replace(/\..*$/, ''))
125-
.filter(
126-
i =>
127-
!['src', 'index', 'dist', 'node_modules'].some(j =>
128-
i.includes(j),
129-
) && i.match(/^[\w-]+$/),
130-
),
131-
),
132-
)
133-
if (parts.length) {
134-
return `chunks/${parts.slice(-2).join('-')}.[hash].js`
135-
}
136-
}
137-
return 'vendor/[name].[hash].js'
138-
},
110+
chunkFileNames: 'chunks/[name].[hash].js',
139111
},
140112
external,
141113
plugins: [...plugins, !watch && licensePlugin()],
@@ -163,6 +135,7 @@ export default ({ watch }) =>
163135
entryFileNames: chunk =>
164136
`${normalize(chunk.name).replace('src/', '')}.d.ts`,
165137
format: 'esm',
138+
chunkFileNames: 'chunks/[name].[hash].d.ts',
166139
},
167140
external,
168141
plugins: [dts({ respectExternal: true })],

packages/vitest/src/api/setup.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,14 @@ import { parse, stringify } from 'flatted'
55
import type { WebSocket } from 'ws'
66
import { WebSocketServer } from 'ws'
77
import type { ViteDevServer } from 'vite'
8+
import type { File, TaskResultPack } from '@vitest/runner'
89
import { API_PATH } from '../constants'
9-
import type { Vitest } from '../node'
10-
import type {
11-
Awaitable,
12-
File,
13-
ModuleGraphData,
14-
Reporter,
15-
SerializableSpec,
16-
TaskResultPack,
17-
UserConsoleLog,
18-
} from '../types'
10+
import type { Vitest } from '../node/core'
11+
import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/general'
12+
import type { Reporter } from '../node/types/reporter'
1913
import { getModuleGraph, isPrimitive, noop, stringifyReplace } from '../utils'
2014
import { parseErrorStacktrace } from '../utils/source-map'
15+
import type { SerializedSpec } from '../runtime/types/utils'
2116
import type {
2217
TransformResultWithSource,
2318
WebSocketEvents,
@@ -165,7 +160,7 @@ export class WebSocketReporter implements Reporter {
165160
})
166161
}
167162

168-
onSpecsCollected(specs?: SerializableSpec[] | undefined): Awaitable<void> {
163+
onSpecsCollected(specs?: SerializedSpec[] | undefined): Awaitable<void> {
169164
if (this.clients.size === 0) {
170165
return
171166
}

packages/vitest/src/api/types.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
import type { TransformResult } from 'vite'
21
import type { BirpcReturn } from 'birpc'
3-
import type {
4-
File,
5-
ModuleGraphData,
6-
Reporter,
7-
SerializableSpec,
8-
SerializedConfig,
9-
TaskResultPack,
10-
} from '../types'
2+
import type { File, TaskResultPack } from '@vitest/runner'
3+
import type { Awaitable, ModuleGraphData, UserConsoleLog } from '../types/general'
4+
import type { SerializedConfig } from '../runtime/config'
5+
import type { SerializedSpec } from '../runtime/types/utils'
116

12-
export interface TransformResultWithSource extends TransformResult {
7+
interface SourceMap {
8+
file: string
9+
mappings: string
10+
names: string[]
11+
sources: string[]
12+
sourcesContent?: string[]
13+
version: number
14+
toString: () => string
15+
toUrl: () => string
16+
}
17+
18+
export interface TransformResultWithSource {
19+
code: string
20+
map: SourceMap | {
21+
mappings: ''
22+
} | null
23+
etag?: string
24+
deps?: string[]
25+
dynamicDeps?: string[]
1326
source?: string
1427
}
1528

1629
export interface WebSocketHandlers {
1730
onCollected: (files?: File[]) => Promise<void>
1831
onTaskUpdate: (packs: TaskResultPack[]) => void
1932
getFiles: () => File[]
20-
getTestFiles: () => Promise<SerializableSpec[]>
33+
getTestFiles: () => Promise<SerializedSpec[]>
2134
getPaths: () => string[]
2235
getConfig: () => SerializedConfig
2336
getModuleGraph: (
@@ -37,16 +50,17 @@ export interface WebSocketHandlers {
3750
getUnhandledErrors: () => unknown[]
3851
}
3952

40-
export interface WebSocketEvents
41-
extends Pick<
42-
Reporter,
43-
| 'onCollected'
44-
| 'onFinished'
45-
| 'onTaskUpdate'
46-
| 'onUserConsoleLog'
47-
| 'onPathsCollected'
48-
| 'onSpecsCollected'
49-
> {
53+
export interface WebSocketEvents {
54+
onCollected?: (files?: File[]) => Awaitable<void>
55+
onFinished?: (
56+
files: File[],
57+
errors: unknown[],
58+
coverage?: unknown
59+
) => Awaitable<void>
60+
onTaskUpdate?: (packs: TaskResultPack[]) => Awaitable<void>
61+
onUserConsoleLog?: (log: UserConsoleLog) => Awaitable<void>
62+
onPathsCollected?: (paths?: string[]) => Awaitable<void>
63+
onSpecsCollected?: (specs?: SerializedSpec[]) => Awaitable<void>
5064
onFinishedReportCoverage: () => void
5165
}
5266

packages/vitest/src/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import './node/types/vite'
2+
13
import type { ConfigEnv, UserConfig as ViteUserConfig } from 'vite'
2-
import type { ProjectConfig } from './types'
4+
import type { ProjectConfig } from './node/types/config'
35

46
export interface UserWorkspaceConfig extends ViteUserConfig {
57
test?: ProjectConfig

packages/vitest/src/create/browser/creator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Agent } from '@antfu/install-pkg'
77
import { detectPackageManager, installPackage } from '@antfu/install-pkg'
88
import { findUp } from 'find-up'
99
import { execa } from 'execa'
10-
import type { BrowserBuiltinProvider } from '../../types/browser'
10+
import type { BrowserBuiltinProvider } from '../../node/types/browser'
1111
import { configFiles } from '../../constants'
1212
import { generateExampleFiles } from './examples'
1313

packages/vitest/src/defaults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
CoverageV8Options,
55
ResolvedCoverageOptions,
66
UserConfig,
7-
} from './types'
7+
} from './node/types/config'
88
import { isCI } from './utils/env'
99

1010
export { defaultBrowserPort } from './constants'

packages/vitest/src/environments.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/vitest/src/index.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/vitest/src/integrations/chai/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import {
1111
getState,
1212
setState,
1313
} from '@vitest/expect'
14-
import type { Assertion, ExpectStatic } from '@vitest/expect'
15-
import type { MatcherState } from '../../types/chai'
14+
import type { Assertion, ExpectStatic, MatcherState } from '@vitest/expect'
1615
import { getTestName } from '../../utils/tasks'
17-
import { getCurrentEnvironment, getWorkerState } from '../../utils/global'
16+
import { getCurrentEnvironment, getWorkerState } from '../../runtime/utils'
1817
import { createExpectPoll } from './poll'
1918

2019
export function createExpect(test?: TaskPopulated) {

packages/vitest/src/integrations/coverage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SerializedCoverageConfig } from '../runtime/config'
22
import type {
33
CoverageProvider,
44
CoverageProviderModule,
5-
} from '../types'
5+
} from '../node/types/coverage'
66

77
interface Loader {
88
executeId: (id: string) => Promise<{ default: CoverageProviderModule }>

packages/vitest/src/integrations/css/css-modules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createHash } from 'node:crypto'
2-
import type { CSSModuleScopeStrategy } from '../../types'
2+
import type { CSSModuleScopeStrategy } from '../../node/types/config'
33

44
export function generateCssFilenameHash(filepath: string) {
55
return createHash('md5').update(filepath).digest('hex').slice(0, 6)

packages/vitest/src/integrations/env/edge-runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Environment } from '../../types'
1+
import type { Environment } from '../../types/environment'
22
import { populateGlobal } from './utils'
33
import { KEYS } from './jsdom-keys'
44

packages/vitest/src/integrations/env/happy-dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Environment } from '../../types'
1+
import type { Environment } from '../../types/environment'
22
import { populateGlobal } from './utils'
33

44
async function teardownWindow(win: {
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { VitestEnvironment } from '../../types/config'
21
import node from './node'
32
import jsdom from './jsdom'
43
import happy from './happy-dom'
@@ -12,25 +11,3 @@ export const environments = {
1211
}
1312

1413
export const envs = Object.keys(environments)
15-
16-
export const envPackageNames: Record<
17-
Exclude<keyof typeof environments, 'node'>,
18-
string
19-
> = {
20-
'jsdom': 'jsdom',
21-
'happy-dom': 'happy-dom',
22-
'edge-runtime': '@edge-runtime/vm',
23-
}
24-
25-
export function getEnvPackageName(env: VitestEnvironment) {
26-
if (env === 'node') {
27-
return null
28-
}
29-
if (env in envPackageNames) {
30-
return (envPackageNames as any)[env]
31-
}
32-
if (env[0] === '.' || env[0] === '/') {
33-
return null
34-
}
35-
return `vitest-environment-${env}`
36-
}

packages/vitest/src/integrations/env/jsdom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Environment } from '../../types'
1+
import type { Environment } from '../../types/environment'
22
import { populateGlobal } from './utils'
33

44
function catchWindowErrors(window: Window) {

packages/vitest/src/integrations/env/loader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { readFileSync } from 'node:fs'
22
import { normalize, resolve } from 'pathe'
33
import { ViteNodeRunner } from 'vite-node/client'
44
import type { ViteNodeRunnerOptions } from 'vite-node'
5-
import type { BuiltinEnvironment, VitestEnvironment } from '../../types/config'
6-
import type { ContextRPC, Environment, WorkerRPC } from '../../types'
5+
import type { BuiltinEnvironment, VitestEnvironment } from '../../node/types/config'
6+
import type { ContextRPC, WorkerRPC } from '../../types/worker'
7+
import type { Environment } from '../../types/environment'
78
import { environments } from './index'
89

910
function isBuiltinEnvironment(

packages/vitest/src/integrations/env/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Console } from 'node:console'
2-
import type { Environment } from '../../types'
2+
import type { Environment } from '../../types/environment'
33

44
// some globals we do not want, either because deprecated or we set it ourselves
55
const denyList = new Set([

packages/vitest/src/integrations/globals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { globalApis } from '../constants'
2-
import * as index from '../index'
2+
import * as index from '../public/index'
33

44
export function registerApiGlobally() {
55
globalApis.forEach((api) => {

packages/vitest/src/integrations/inject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ProvidedContext } from '../types/general'
2-
import { getWorkerState } from '../utils/global'
2+
import { getWorkerState } from '../runtime/utils'
33

44
/**
55
* Gives access to injected context provided from the main thread.

packages/vitest/src/integrations/run-once.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getWorkerState } from '../utils/global'
1+
import { getWorkerState } from '../runtime/utils'
22

33
const filesCount = new Map<string, number>()
44
const cache = new Map<string, any>()

0 commit comments

Comments
 (0)