Skip to content

Commit

Permalink
fix(vite-node): fix buildStart on Vite 6 (#7480)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 27, 2025
1 parent fee90d8 commit c0f47e0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/vite-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Note that when using the `--script` option, Vite Node forwards every argument an
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

```ts
import { createServer } from 'vite'
import { createServer, version as viteVersion } from 'vite'
import { ViteNodeRunner } from 'vite-node/client'
import { ViteNodeServer } from 'vite-node/server'
import { installSourcemapsSupport } from 'vite-node/source-map'
Expand All @@ -94,8 +94,10 @@ const server = await createServer({
disabled: true,
},
})
// this is need to initialize the plugins
await server.pluginContainer.buildStart({})
// For old Vite, this is need to initialize the plugins.
if (Number(viteVersion.split('.')[0]) < 6) {
await server.pluginContainer.buildStart({})
}

// create vite-node server
const node = new ViteNodeServer(server)
Expand Down
6 changes: 4 additions & 2 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ViteNodeServerOptions } from './types'
import { resolve } from 'node:path'
import cac from 'cac'
import c from 'tinyrainbow'
import { createServer, loadEnv } from 'vite'
import { createServer, loadEnv, version as viteVersion } from 'vite'
import { version } from '../package.json'
import { ViteNodeRunner } from './client'
import { createHotContext, handleMessage, viteNodeHmrPlugin } from './hmr'
Expand Down Expand Up @@ -94,7 +94,9 @@ async function run(files: string[], options: CliOptions = {}) {
},
plugins: [options.watch && viteNodeHmrPlugin()],
})
await server.pluginContainer.buildStart({})
if (Number(viteVersion.split('.')[0]) < 6) {
await server.pluginContainer.buildStart({})
}

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

Expand Down
1 change: 1 addition & 0 deletions test/vite-node/src/buildStart/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'test'
21 changes: 21 additions & 0 deletions test/vite-node/src/buildStart/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from 'vite'

const data: string[] = []

export default defineConfig({
plugins: [
{
name: 'test-plugin',
async buildStart() {
data.push('buildStart:in')
await new Promise(r => setTimeout(r, 100))
data.push('buildStart:out')
},
transform(_code, id) {
if (id.endsWith('/test.ts')) {
console.log(JSON.stringify(data))
}
},
},
],
})
6 changes: 6 additions & 0 deletions test/vite-node/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ it('error stack', async () => {
const { viteNode } = await runViteNodeCli('--watch', entryPath)
await viteNode.waitForStdout('source-map.ts:7:11')
})

it('buildStart', async () => {
const root = resolve(__dirname, '../src/buildStart')
const result = await runViteNodeCli('--root', root, resolve(root, 'test.ts'))
await result.viteNode.waitForStdout('["buildStart:in","buildStart:out"]')
})

0 comments on commit c0f47e0

Please sign in to comment.