Skip to content

Commit 0040e18

Browse files
committed
fix: setup tests
i
1 parent 8315ebf commit 0040e18

File tree

5 files changed

+73
-24
lines changed

5 files changed

+73
-24
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"typecheck": "tsc -p scripts && tsc -p playground && tsc -p packages/plugin-react",
2424
"test": "pnpm run test-serve && pnpm run test-build && pnpm --filter ./packages/plugin-react-swc run test",
2525
"test-serve": "vitest run -c playground/vitest.config.e2e.ts",
26+
"test-full-bundle-mode-serve": "VITE_TEST_FULL_BUNDLE_MODE=1 vitest run -c playground/vitest.config.e2e.ts",
2627
"test-build": "VITE_TEST_BUILD=1 vitest run -c playground/vitest.config.e2e.ts",
2728
"debug-serve": "VITE_DEBUG_SERVE=1 vitest run -c playground/vitest.config.e2e.ts",
2829
"debug-build": "VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 vitest run -c playground/vitest.config.e2e.ts",

packages/plugin-react-oxc/src/index.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
103103
}
104104

105105
let skipFastRefresh = false
106-
106+
let base: string | undefined
107107
const viteRefreshWrapper: Plugin = {
108108
name: 'vite:react-oxc:refresh-wrapper',
109109
apply: 'serve',
110110
configResolved(config) {
111+
base = config.base
111112
skipFastRefresh = config.isProduction || config.server.hmr === false
112113
},
113114
transform: {
@@ -136,15 +137,22 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
136137
return { code: newCode, map: null }
137138
},
138139
},
139-
transformIndexHtml(_, config) {
140-
if (!skipFastRefresh)
141-
return [
142-
{
143-
tag: 'script',
144-
attrs: { type: 'module' },
145-
children: getPreambleCode(config.server!.config.base),
146-
},
147-
]
140+
transformIndexHtml: {
141+
handler() {
142+
if (!skipFastRefresh)
143+
return [
144+
{
145+
tag: 'script',
146+
attrs: { type: 'module' },
147+
// !!! Rolldown vite full bunlde module break changes, config.server is invalid
148+
// children: getPreambleCode(config.server!.config.base),
149+
children: getPreambleCode(base!),
150+
},
151+
]
152+
},
153+
// Rolldown vite full bunlde module break changes.
154+
// Changed it to make sure the inject module could be bundled
155+
order: 'pre',
148156
},
149157
}
150158

packages/plugin-react-swc/src/index.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const react = (_options?: Options): PluginOption[] => {
9292
_options?.useAtYourOwnRisk_mutateSwcOptions,
9393
}
9494

95+
let base: string | undefined
9596
return [
9697
{
9798
name: 'vite:react-swc:resolve-runtime',
@@ -128,6 +129,7 @@ const react = (_options?: Options): PluginOption[] => {
128129
},
129130
}),
130131
configResolved(config) {
132+
base = config.base
131133
if (config.server.hmr === false) hmrDisabled = true
132134
const mdxIndex = config.plugins.findIndex(
133135
(p) => p.name === '@mdx-js/rollup',
@@ -142,16 +144,23 @@ const react = (_options?: Options): PluginOption[] => {
142144
)
143145
}
144146
},
145-
transformIndexHtml: (_, config) => {
146-
if (!hmrDisabled) {
147-
return [
148-
{
149-
tag: 'script',
150-
attrs: { type: 'module' },
151-
children: getPreambleCode(config.server!.config.base),
152-
},
153-
]
154-
}
147+
transformIndexHtml: {
148+
handler() {
149+
if (!hmrDisabled) {
150+
return [
151+
{
152+
tag: 'script',
153+
attrs: { type: 'module' },
154+
// !!! Rolldown vite full bunlde module break changes, config.server is invalid
155+
// children: getPreambleCode(config.server!.config.base),
156+
children: getPreambleCode(base!),
157+
},
158+
]
159+
}
160+
},
161+
// Rolldown vite full bunlde module break changes.
162+
// Changed it to make sure the inject module could be bundled
163+
order: 'pre',
155164
},
156165
async transform(code, _id, transformOptions) {
157166
const id = _id.split('?')[0]

playground/vitest.config.e2e.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ export default defineConfig({
1111
},
1212
test: {
1313
pool: 'forks',
14-
include: ['./playground/**/*.spec.[tj]s'],
14+
include: process.env.VITE_TEST_FULL_BUNDLE_MODE
15+
? [
16+
'./playground/class-components/**/*.spec.[tj]s',
17+
'./playground/compiler/**/*.spec.[tj]s',
18+
'./playground/compiler-react-18/**/*.spec.[tj]s',
19+
'./playground/mdx/**/*.spec.[tj]s',
20+
// './playground/react/**/*.spec.[tj]s',
21+
// './playground/react-classic/**/*.spec.[tj]s',
22+
'./playground/react-emotion/**/*.spec.[tj]s',
23+
'./playground/react-env/**/*.spec.[tj]s',
24+
'./playground/react-sourcemap/**/*.spec.[tj]s',
25+
// './playground/ssr-react/**/*.spec.[tj]s',
26+
]
27+
: ['./playground/**/*.spec.[tj]s'],
1528
setupFiles: ['./playground/vitestSetup.ts'],
1629
globalSetup: ['./playground/vitestGlobalSetup.ts'],
1730
testTimeout: timeout,

playground/vitestSetup.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,39 @@ async function loadConfig(configEnv: ConfigEnv) {
214214
// tests are flaky when `emptyOutDir` is `true`
215215
emptyOutDir: false,
216216
},
217+
experimental: {
218+
fullBundleMode: !!process.env.VITE_TEST_FULL_BUNDLE_MODE,
219+
},
217220
customLogger: createInMemoryLogger(serverLogs),
218221
}
219222
return mergeConfig(options, config || {})
220223
}
221224

222225
export async function startDefaultServe(): Promise<void> {
223-
const { build, createBuilder, createServer, mergeConfig, preview } =
224-
await importVite()
226+
const {
227+
build,
228+
createBuilder,
229+
createServer,
230+
mergeConfig,
231+
preview,
232+
createServerWithResolvedConfig,
233+
} = await importVite()
225234

226235
setupConsoleWarnCollector(serverLogs)
227236

228237
if (!isBuild) {
229238
process.env.VITE_INLINE = 'inline-serve'
230239
const config = await loadConfig({ command: 'serve', mode: 'development' })
231-
viteServer = server = await (await createServer(config)).listen()
240+
241+
if (process.env.VITE_TEST_FULL_BUNDLE_MODE) {
242+
const builder = await createBuilder(config, null, 'serve')
243+
viteServer = server = await createServerWithResolvedConfig(builder.config)
244+
await server.listen()
245+
await builder.buildApp(server)
246+
} else {
247+
viteServer = server = await (await createServer(config)).listen()
248+
}
249+
232250
viteTestUrl = stripTrailingSlashIfNeeded(
233251
server.resolvedUrls.local[0],
234252
server.config.base,

0 commit comments

Comments
 (0)