From 55a25fa4c2082e8df711a77ebc06457585939725 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 11 Nov 2024 18:07:00 +0900 Subject: [PATCH] feat: use injectEnvironmentToHooks --- .../src/node/server/environments/rolldown.ts | 6 ++-- playground/rolldown-dev-react/vite.config.ts | 2 +- playground/rolldown-dev-ssr/src/app.tsx | 3 ++ playground/rolldown-dev-ssr/vite.config.ts | 32 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/server/environments/rolldown.ts b/packages/vite/src/node/server/environments/rolldown.ts index c1aee76ac67771..becd6664e0938b 100644 --- a/packages/vite/src/node/server/environments/rolldown.ts +++ b/packages/vite/src/node/server/environments/rolldown.ts @@ -18,6 +18,7 @@ import type { ViteDevServer, } from '../..' import { CLIENT_ENTRY } from '../../constants' +import { injectEnvironmentToHooks } from '../../build' const require = createRequire(import.meta.url) @@ -221,11 +222,12 @@ class RolldownEnvironment extends DevEnvironment { } // all plugins are shared like Vite 6 `sharedConfigBuild`. - // TODO: setup PluginContext.environment - const plugins = this._plugins!.filter( + let plugins = this._plugins! + plugins = plugins.filter( // TODO: reuse core plugins (p) => !(p.name?.startsWith('vite:') || p.name === 'alias'), ) + plugins = plugins.map((p) => injectEnvironmentToHooks(this as any, p)) console.time(`[rolldown:${this.name}:build]`) const inputOptions: rolldown.InputOptions = { diff --git a/playground/rolldown-dev-react/vite.config.ts b/playground/rolldown-dev-react/vite.config.ts index d9dd3a2db2eacd..2e1177de4a86ed 100644 --- a/playground/rolldown-dev-react/vite.config.ts +++ b/playground/rolldown-dev-react/vite.config.ts @@ -36,7 +36,7 @@ export default defineConfig({ handler(id, _options) { if (id === '\0virtual:test') { console.log('[debug:load]', this.environment?.name) - return `export default "virtual-ok"` + return `export default "virtual-ok, environment.name: ${this.environment.name}"` } }, }, diff --git a/playground/rolldown-dev-ssr/src/app.tsx b/playground/rolldown-dev-ssr/src/app.tsx index 627093629d6052..2b0900cb8c7d17 100644 --- a/playground/rolldown-dev-ssr/src/app.tsx +++ b/playground/rolldown-dev-ssr/src/app.tsx @@ -1,4 +1,6 @@ import React from 'react' +// @ts-expect-error no type +import virtualTest from 'virtual:test' export function App() { const [count, setCount] = React.useState(0) @@ -7,6 +9,7 @@ export function App() {

Rolldown SSR

+
[virtual:test] {virtualTest}
) } diff --git a/playground/rolldown-dev-ssr/vite.config.ts b/playground/rolldown-dev-ssr/vite.config.ts index e4cd8536b6f090..bbf5c614d1aff3 100644 --- a/playground/rolldown-dev-ssr/vite.config.ts +++ b/playground/rolldown-dev-ssr/vite.config.ts @@ -42,5 +42,37 @@ export default defineConfig({ } }, }, + { + name: 'test', + options() { + console.log('[debug:options]', this.environment?.name) + }, + buildStart() { + console.log('[debug:buildStart]', this.environment?.name) + }, + buildEnd() { + console.log('[debug:buildEnd]', this.environment?.name) + }, + resolveId: { + handler(source, importer, _options) { + if (source === 'virtual:test') { + console.log('[debug:resolveId]', [ + this.environment?.name, + source, + importer, + ]) + return `\0virtual:test` + } + }, + }, + load: { + handler(id, _options) { + if (id === '\0virtual:test') { + console.log('[debug:load]', this.environment?.name) + return `export default "virtual-ok"` + } + }, + }, + }, ], })