Skip to content

Commit fb76aaf

Browse files
committed
chore: inject react-refresh
1 parent 2b6c08e commit fb76aaf

File tree

10 files changed

+72
-5
lines changed

10 files changed

+72
-5
lines changed

packages/vitest/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
},
8484
"files": [
8585
"dist",
86+
"setup",
8687
"bin",
8788
"*.d.ts",
8889
"*.d.cts",
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (typeof window !== 'undefined') {
2+
const { default: RefreshRuntime } = await import('/@react-refresh')
3+
4+
RefreshRuntime.injectIntoGlobalHook(window)
5+
window.$RefreshReg$ = () => {}
6+
window.$RefreshSig$ = () => type => type
7+
window.__vite_plugin_react_preamble_installed__ = true
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Plugin } from 'vite'
2+
import { resolve } from 'pathe'
3+
import { hasVitePlugin } from '../../utils/config-helpers'
4+
import { distDir } from '../../paths'
5+
6+
export function CommonPluginsHandler(): Plugin {
7+
return {
8+
name: 'vitest:common-plugins-enforcer',
9+
enforce: 'post',
10+
config: {
11+
order: 'post',
12+
async handler(config) {
13+
const plugins = (config.plugins || [])
14+
const hasReact = await hasVitePlugin(plugins, 'vite:react-babel')
15+
if (hasReact) {
16+
return {
17+
test: {
18+
deps: {
19+
inline: [/vitest\/setup\/react-refresh.js/],
20+
},
21+
// Since this is an official plugin, it should be OK to inline reac-refresh HRM logic
22+
setupFiles: [resolve(distDir, '../setup/react-refresh.js')],
23+
},
24+
}
25+
}
26+
},
27+
},
28+
}
29+
}

packages/vitest/src/node/plugins/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { GlobalSetupPlugin } from './globalSetup'
1414
import { CSSEnablerPlugin } from './cssEnabler'
1515
import { CoverageTransform } from './coverageTransform'
1616
import { MocksPlugin } from './mocks'
17+
import { CommonPluginsHandler } from './commonPluginsHandler'
1718

1819
export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('test')): Promise<VitePlugin[]> {
1920
const userConfig = deepMerge({}, options) as UserConfig
@@ -240,6 +241,7 @@ export async function VitestPlugin(options: UserConfig = {}, ctx = new Vitest('t
240241
? await UIPlugin()
241242
: null,
242243
MocksPlugin(),
244+
CommonPluginsHandler(),
243245
]
244246
.filter(notNullish)
245247
}

packages/vitest/src/node/plugins/workspace.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { CSSEnablerPlugin } from './cssEnabler'
1212
import { EnvReplacerPlugin } from './envReplacer'
1313
import { GlobalSetupPlugin } from './globalSetup'
1414
import { MocksPlugin } from './mocks'
15+
import { CommonPluginsHandler } from './commonPluginsHandler'
1516

1617
interface WorkspaceOptions extends UserWorkspaceConfig {
1718
root?: string
@@ -171,5 +172,6 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp
171172
CoverageTransform(project.ctx),
172173
GlobalSetupPlugin(project, project.ctx.logger),
173174
MocksPlugin(),
175+
CommonPluginsHandler(),
174176
]
175177
}

packages/vitest/src/runtime/execute.ts

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export async function startViteNode(ctx: ContextRPC) {
8181
})
8282

8383
const environment = await loadEnvironment(ctx.environment.name, executor)
84+
ctx.environment.environment = environment
8485
transformMode = environment.transformMode ?? 'ssr'
8586

8687
const { run } = await import(pathToFileURL(resolve(distDir, 'entry.js')).href)

packages/vitest/src/runtime/loader.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ export const resolve: Resolver = async (url, context, next) => {
4444
const { parentURL } = context
4545
const state = getWorkerState()
4646
const resolver = state?.rpc.resolveId
47+
const environment = state?.ctx.environment.environment
4748

48-
if (!parentURL || isNodeBuiltin(url) || !resolver)
49+
if (!parentURL || isNodeBuiltin(url) || !resolver || !environment)
4950
return next(url, context, next)
5051

5152
const id = normalizeModuleId(url)
5253
const importer = normalizeModuleId(parentURL)
53-
const resolved = await resolver(id, importer, state.ctx.environment.name)
54+
const resolved = await resolver(id, importer, environment.transformMode ?? 'ssr')
5455

5556
let result: ResolveResult
5657
let filepath: string

packages/vitest/src/types/rpc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface RunnerRPC {
3434

3535
export interface ContextTestEnvironment {
3636
name: VitestEnvironment
37+
environment?: Environment
3738
options: EnvironmentOptions | null
3839
}
3940

packages/vitest/src/utils/config-helpers.ts

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PluginOption } from 'vite'
12
import type { BenchmarkBuiltinReporters, BuiltinReporters } from '../node/reporters'
23

34
interface PotentialConfig {
@@ -13,3 +14,24 @@ export function getOutputFile(config: PotentialConfig | undefined, reporter: Bui
1314

1415
return config.outputFile[reporter]
1516
}
17+
18+
async function isVitePlugin(plugin: PluginOption, name: string): Promise<boolean> {
19+
if (!plugin)
20+
return false
21+
if (Array.isArray(plugin))
22+
return hasVitePlugin(plugin, name)
23+
if (plugin instanceof Promise)
24+
return isVitePlugin(await plugin, name)
25+
if (typeof plugin === 'object')
26+
return plugin.name === name
27+
return false
28+
}
29+
30+
export async function hasVitePlugin(plugins: PluginOption[], name: string): Promise<boolean> {
31+
for (const plugin of plugins) {
32+
if (await isVitePlugin(plugin, name))
33+
return true
34+
}
35+
36+
return false
37+
}

packages/web-worker/src/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ export function createMessageEvent(data: any, transferOrOptions: StructuredSeria
6161
}
6262

6363
export function getRunnerOptions(): any {
64-
const { config, ctx, rpc, mockMap, moduleCache } = getWorkerState()
64+
const { config, rpc, mockMap, moduleCache } = getWorkerState()
6565

6666
return {
6767
fetchModule(id: string) {
68-
return rpc.fetch(id, ctx.environment.name)
68+
return rpc.fetch(id, 'web')
6969
},
7070
resolveId(id: string, importer?: string) {
71-
return rpc.resolveId(id, importer, ctx.environment.name)
71+
return rpc.resolveId(id, importer, 'web')
7272
},
7373
moduleCache,
7474
mockMap,

0 commit comments

Comments
 (0)