Skip to content

Commit c0f47e0

Browse files
authored
fix(vite-node): fix buildStart on Vite 6 (#7480)
1 parent fee90d8 commit c0f47e0

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

packages/vite-node/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Note that when using the `--script` option, Vite Node forwards every argument an
8282
In Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having both (server and runner) running in the same context
8383

8484
```ts
85-
import { createServer } from 'vite'
85+
import { createServer, version as viteVersion } from 'vite'
8686
import { ViteNodeRunner } from 'vite-node/client'
8787
import { ViteNodeServer } from 'vite-node/server'
8888
import { installSourcemapsSupport } from 'vite-node/source-map'
@@ -94,8 +94,10 @@ const server = await createServer({
9494
disabled: true,
9595
},
9696
})
97-
// this is need to initialize the plugins
98-
await server.pluginContainer.buildStart({})
97+
// For old Vite, this is need to initialize the plugins.
98+
if (Number(viteVersion.split('.')[0]) < 6) {
99+
await server.pluginContainer.buildStart({})
100+
}
99101

100102
// create vite-node server
101103
const node = new ViteNodeServer(server)

packages/vite-node/src/cli.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ViteNodeServerOptions } from './types'
22
import { resolve } from 'node:path'
33
import cac from 'cac'
44
import c from 'tinyrainbow'
5-
import { createServer, loadEnv } from 'vite'
5+
import { createServer, loadEnv, version as viteVersion } from 'vite'
66
import { version } from '../package.json'
77
import { ViteNodeRunner } from './client'
88
import { createHotContext, handleMessage, viteNodeHmrPlugin } from './hmr'
@@ -94,7 +94,9 @@ async function run(files: string[], options: CliOptions = {}) {
9494
},
9595
plugins: [options.watch && viteNodeHmrPlugin()],
9696
})
97-
await server.pluginContainer.buildStart({})
97+
if (Number(viteVersion.split('.')[0]) < 6) {
98+
await server.pluginContainer.buildStart({})
99+
}
98100

99101
const env = loadEnv(server.config.mode, server.config.envDir, '')
100102

test/vite-node/src/buildStart/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'test'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { defineConfig } from 'vite'
2+
3+
const data: string[] = []
4+
5+
export default defineConfig({
6+
plugins: [
7+
{
8+
name: 'test-plugin',
9+
async buildStart() {
10+
data.push('buildStart:in')
11+
await new Promise(r => setTimeout(r, 100))
12+
data.push('buildStart:out')
13+
},
14+
transform(_code, id) {
15+
if (id.endsWith('/test.ts')) {
16+
console.log(JSON.stringify(data))
17+
}
18+
},
19+
},
20+
],
21+
})

test/vite-node/test/cli.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,9 @@ it('error stack', async () => {
5858
const { viteNode } = await runViteNodeCli('--watch', entryPath)
5959
await viteNode.waitForStdout('source-map.ts:7:11')
6060
})
61+
62+
it('buildStart', async () => {
63+
const root = resolve(__dirname, '../src/buildStart')
64+
const result = await runViteNodeCli('--root', root, resolve(root, 'test.ts'))
65+
await result.viteNode.waitForStdout('["buildStart:in","buildStart:out"]')
66+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy