Skip to content

Commit e922e92

Browse files
authored
fix: configure oxc instead of esbuild on rolldown-vite (#8378)
1 parent 4238567 commit e922e92

File tree

5 files changed

+82
-40
lines changed

5 files changed

+82
-40
lines changed

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
notNullish,
77
} from '@vitest/utils'
88
import { relative } from 'pathe'
9+
import * as vite from 'vite'
910
import { defaultPort } from '../../constants'
1011
import { configDefaults } from '../../defaults'
1112
import { generateScopedClassName } from '../../integrations/css/css-modules'
@@ -76,22 +77,12 @@ export async function VitestPlugin(
7677

7778
const resolveOptions = getDefaultResolveOptions()
7879

79-
const config: ViteConfig = {
80+
let config: ViteConfig = {
8081
root: viteConfig.test?.root || options.root,
8182
define: {
8283
// disable replacing `process.env.NODE_ENV` with static string by vite:client-inject
8384
'process.env.NODE_ENV': 'process.env.NODE_ENV',
8485
},
85-
esbuild:
86-
viteConfig.esbuild === false
87-
? false
88-
: {
89-
// Lowest target Vitest supports is Node18
90-
target: viteConfig.esbuild?.target || 'node18',
91-
sourcemap: 'external',
92-
// Enables using ignore hint for coverage providers with @preserve keyword
93-
legalComments: 'inline',
94-
},
9586
resolve: {
9687
...resolveOptions,
9788
alias: testConfig.alias,
@@ -147,6 +138,35 @@ export async function VitestPlugin(
147138
},
148139
}
149140

141+
if ('rolldownVersion' in vite) {
142+
config = {
143+
...config,
144+
// eslint-disable-next-line ts/ban-ts-comment
145+
// @ts-ignore rolldown-vite only
146+
oxc: viteConfig.oxc === false
147+
? false
148+
: {
149+
// eslint-disable-next-line ts/ban-ts-comment
150+
// @ts-ignore rolldown-vite only
151+
target: viteConfig.oxc?.target || 'node18',
152+
},
153+
}
154+
}
155+
else {
156+
config = {
157+
...config,
158+
esbuild: viteConfig.esbuild === false
159+
? false
160+
: {
161+
// Lowest target Vitest supports is Node18
162+
target: viteConfig.esbuild?.target || 'node18',
163+
sourcemap: 'external',
164+
// Enables using ignore hint for coverage providers with @preserve keyword
165+
legalComments: 'inline',
166+
},
167+
}
168+
}
169+
150170
// inherit so it's available in VitestOptimizer
151171
// I cannot wait to rewrite all of this in Vitest 4
152172
if (options.cache != null) {

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ResolvedConfig, TestProjectInlineConfiguration } from '../types/co
44
import { existsSync, readFileSync } from 'node:fs'
55
import { deepMerge } from '@vitest/utils'
66
import { basename, dirname, relative, resolve } from 'pathe'
7+
import * as vite from 'vite'
78
import { configDefaults } from '../../defaults'
89
import { generateScopedClassName } from '../../integrations/css/css-modules'
910
import { VitestFilteredOutProjectError } from '../errors'
@@ -117,7 +118,7 @@ export function WorkspaceVitestPlugin(
117118
const root = testConfig.root || viteConfig.root || options.root
118119

119120
const resolveOptions = getDefaultResolveOptions()
120-
const config: ViteConfig = {
121+
let config: ViteConfig = {
121122
root,
122123
define: {
123124
// disable replacing `process.env.NODE_ENV` with static string by vite:client-inject
@@ -127,15 +128,6 @@ export function WorkspaceVitestPlugin(
127128
...resolveOptions,
128129
alias: testConfig.alias,
129130
},
130-
esbuild: viteConfig.esbuild === false
131-
? false
132-
: {
133-
// Lowest target Vitest supports is Node18
134-
target: viteConfig.esbuild?.target || 'node18',
135-
sourcemap: 'external',
136-
// Enables using ignore hint for coverage providers with @preserve keyword
137-
legalComments: 'inline',
138-
},
139131
server: {
140132
// disable watch mode in workspaces,
141133
// because it is handled by the top-level watcher
@@ -162,6 +154,35 @@ export function WorkspaceVitestPlugin(
162154
test: {},
163155
}
164156

157+
if ('rolldownVersion' in vite) {
158+
config = {
159+
...config,
160+
// eslint-disable-next-line ts/ban-ts-comment
161+
// @ts-ignore rolldown-vite only
162+
oxc: viteConfig.oxc === false
163+
? false
164+
: {
165+
// eslint-disable-next-line ts/ban-ts-comment
166+
// @ts-ignore rolldown-vite only
167+
target: viteConfig.oxc?.target || 'node18',
168+
},
169+
}
170+
}
171+
else {
172+
config = {
173+
...config,
174+
esbuild: viteConfig.esbuild === false
175+
? false
176+
: {
177+
// Lowest target Vitest supports is Node18
178+
target: viteConfig.esbuild?.target || 'node18',
179+
sourcemap: 'external',
180+
// Enables using ignore hint for coverage providers with @preserve keyword
181+
legalComments: 'inline',
182+
},
183+
}
184+
}
185+
165186
;(config.test as ResolvedConfig).defines = defines
166187

167188
const classNameStrategy
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expect, it } from 'vitest'
2+
3+
it('decorators work', () => {
4+
expect(Sut.mocked).toBe(true)
5+
expect(new Sut()).toBeInstanceOf(Sut)
6+
})
7+
8+
function exampleDecorator(ClassExample: any, context: ClassDecoratorContext): any {
9+
if (context.kind !== 'class') {
10+
throw new Error('not a class to decorate')
11+
}
12+
ClassExample.mocked = true
13+
return ClassExample
14+
}
15+
16+
@exampleDecorator
17+
class Sut {
18+
static mocked = false
19+
}

test/core/test/esnext.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ it.skipIf(Number(version) < 20)('"v" flag in regexp', () => {
77
expect('👍🏼👍🏼👍🏼'.match(regexp)).toEqual(['👍🏼', '👍🏼', '👍🏼'])
88
})
99

10-
it('decorators work', () => {
11-
expect(Sut.mocked).toBe(true)
12-
expect(new Sut()).toBeInstanceOf(Sut)
13-
})
14-
1510
it('new "using" feature', () => {
1611
let getResource = (): any => {
1712
throw new Error('don\'t call me')
@@ -37,16 +32,3 @@ function resourceful(resourceDefault: string) {
3732
},
3833
}
3934
}
40-
41-
function exampleDecorator(ClassExample: any, context: ClassDecoratorContext): any {
42-
if (context.kind !== 'class') {
43-
throw new Error('not a class to decorate')
44-
}
45-
ClassExample.mocked = true
46-
return ClassExample
47-
}
48-
49-
@exampleDecorator
50-
class Sut {
51-
static mocked = false
52-
}

test/core/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default defineConfig({
7373
...defaultExclude,
7474
// FIXME: wait for ecma decorator support in rolldown/oxc
7575
// https://github.com/oxc-project/oxc/issues/9170
76-
...(rolldownVersion ? ['**/esnext.test.ts'] : []),
76+
...(rolldownVersion ? ['**/esnext-decorator.test.ts'] : []),
7777
],
7878
slowTestThreshold: 1000,
7979
testTimeout: process.env.CI ? 10_000 : 5_000,

0 commit comments

Comments
 (0)