Skip to content

Commit

Permalink
feat: use injectEnvironmentToHooks
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Nov 11, 2024
1 parent a69a5ff commit 55a25fa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/vite/src/node/server/environments/rolldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
ViteDevServer,
} from '../..'
import { CLIENT_ENTRY } from '../../constants'
import { injectEnvironmentToHooks } from '../../build'

const require = createRequire(import.meta.url)

Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion playground/rolldown-dev-react/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`
}
},
},
Expand Down
3 changes: 3 additions & 0 deletions playground/rolldown-dev-ssr/src/app.tsx
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -7,6 +9,7 @@ export function App() {
<h1>Rolldown SSR</h1>
<Hydrated />
<button onClick={() => setCount((c) => c + 1)}>Count: {count}</button>
<pre>[virtual:test] {virtualTest}</pre>
</div>
)
}
Expand Down
32 changes: 32 additions & 0 deletions playground/rolldown-dev-ssr/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
},
},
},
],
})

0 comments on commit 55a25fa

Please sign in to comment.