Skip to content

Commit b1d3676

Browse files
Copilothi-ogawa
andcommitted
Update plugin-rsc to use plugin hook filters internally
- Revert changes to plugin-react and plugin-react-swc (per review feedback) - Add @rolldown/pluginutils dependency to plugin-rsc - Add hook filters to transform hooks: rsc:vite-client-raw-import, rsc:inject-async-local-storage, rsc:rsc-css-self-accept - Add hook filters to resolveId hook: rsc:react-server-dom-webpack-alias - Add hook filters to load hooks: virtual modules for assets-manifest and client-references - Remove redundant conditional checks where hook filters now handle the filtering Co-authored-by: hi-ogawa <[email protected]>
1 parent c2fc9a3 commit b1d3676

File tree

5 files changed

+122
-162
lines changed

5 files changed

+122
-162
lines changed

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

Lines changed: 37 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -169,69 +169,34 @@ const react = (_options?: Options): Plugin[] => {
169169
]
170170
}
171171
},
172-
transform: options.parserConfig
173-
? // When custom parserConfig is provided, we can't add a filter
174-
// because the user controls which files are handled
175-
async (code, _id, transformOptions) => {
176-
const id = _id.split('?')[0]
177-
const refresh = !transformOptions?.ssr && !hmrDisabled
172+
async transform(code, _id, transformOptions) {
173+
const id = _id.split('?')[0]
174+
const refresh = !transformOptions?.ssr && !hmrDisabled
178175

179-
const result = await transformWithOptions(
180-
id,
181-
code,
182-
options.devTarget,
183-
options,
184-
viteCacheRoot,
185-
{
186-
refresh,
187-
development: true,
188-
runtime: 'automatic',
189-
importSource: options.jsxImportSource,
190-
},
191-
)
192-
if (!result) return
193-
if (!refresh) return result
194-
195-
const newCode = addRefreshWrapper(
196-
result.code,
197-
'@vitejs/plugin-react-swc',
198-
id,
199-
options.reactRefreshHost,
200-
)
201-
return { code: newCode ?? result.code, map: result.map }
202-
}
203-
: {
204-
// Add filter for default extensions: .tsx, .ts, .mts, .jsx, .mdx
205-
filter: { id: /\.(tsx|ts|mts|jsx|mdx)(?:$|\?)/ },
206-
async handler(code, _id, transformOptions) {
207-
const id = _id.split('?')[0]
208-
const refresh = !transformOptions?.ssr && !hmrDisabled
209-
210-
const result = await transformWithOptions(
211-
id,
212-
code,
213-
options.devTarget,
214-
options,
215-
viteCacheRoot,
216-
{
217-
refresh,
218-
development: true,
219-
runtime: 'automatic',
220-
importSource: options.jsxImportSource,
221-
},
222-
)
223-
if (!result) return
224-
if (!refresh) return result
225-
226-
const newCode = addRefreshWrapper(
227-
result.code,
228-
'@vitejs/plugin-react-swc',
229-
id,
230-
options.reactRefreshHost,
231-
)
232-
return { code: newCode ?? result.code, map: result.map }
233-
},
176+
const result = await transformWithOptions(
177+
id,
178+
code,
179+
options.devTarget,
180+
options,
181+
viteCacheRoot,
182+
{
183+
refresh,
184+
development: true,
185+
runtime: 'automatic',
186+
importSource: options.jsxImportSource,
234187
},
188+
)
189+
if (!result) return
190+
if (!refresh) return result
191+
192+
const newCode = addRefreshWrapper(
193+
result.code,
194+
'@vitejs/plugin-react-swc',
195+
id,
196+
options.reactRefreshHost,
197+
)
198+
return { code: newCode ?? result.code, map: result.map }
199+
},
235200
},
236201
options.plugins || options.useAtYourOwnRisk_mutateSwcOptions
237202
? {
@@ -244,37 +209,18 @@ const react = (_options?: Options): Plugin[] => {
244209
configResolved(config) {
245210
viteCacheRoot = config.cacheDir
246211
},
247-
transform: options.parserConfig
248-
? // When custom parserConfig is provided, we can't add a filter
249-
// because the user controls which files are handled
250-
(code, _id) =>
251-
transformWithOptions(
252-
_id.split('?')[0],
253-
code,
254-
'esnext',
255-
options,
256-
viteCacheRoot,
257-
{
258-
runtime: 'automatic',
259-
importSource: options.jsxImportSource,
260-
},
261-
)
262-
: {
263-
// Add filter for default extensions: .tsx, .ts, .mts, .jsx, .mdx
264-
filter: { id: /\.(tsx|ts|mts|jsx|mdx)(?:$|\?)/ },
265-
handler: (code, _id) =>
266-
transformWithOptions(
267-
_id.split('?')[0],
268-
code,
269-
'esnext',
270-
options,
271-
viteCacheRoot,
272-
{
273-
runtime: 'automatic',
274-
importSource: options.jsxImportSource,
275-
},
276-
),
212+
transform: (code, _id) =>
213+
transformWithOptions(
214+
_id.split('?')[0],
215+
code,
216+
'esnext',
217+
options,
218+
viteCacheRoot,
219+
{
220+
runtime: 'automatic',
221+
importSource: options.jsxImportSource,
277222
},
223+
),
278224
}
279225
: {
280226
name: 'vite:react-swc',

packages/plugin-react/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'
33
import { readFileSync } from 'node:fs'
44
import type * as babelCore from '@babel/core'
55
import type { ParserOptions, TransformOptions } from '@babel/core'
6+
import { createFilter } from 'vite'
67
import * as vite from 'vite'
78
import type { Plugin, ResolvedConfig } from 'vite'
89
import {
@@ -106,6 +107,7 @@ const compilerAnnotationRE = /['"]use memo['"]/
106107
export default function viteReact(opts: Options = {}): Plugin[] {
107108
const include = opts.include ?? defaultIncludeRE
108109
const exclude = opts.exclude ?? defaultExcludeRE
110+
const filter = createFilter(include, exclude)
109111

110112
const jsxImportSource = opts.jsxImportSource ?? 'react'
111113
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
@@ -245,6 +247,7 @@ export default function viteReact(opts: Options = {}): Plugin[] {
245247
},
246248
async handler(code, id, options) {
247249
const [filepath] = id.split('?')
250+
if (!filter(filepath)) return
248251

249252
const ssr = options?.ssr === true
250253
const babelOptions = (() => {

packages/plugin-rsc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"dependencies": {
4242
"@remix-run/node-fetch-server": "^0.11.0",
43+
"@rolldown/pluginutils": "1.0.0-beta.45",
4344
"es-module-lexer": "^1.7.0",
4445
"estree-walker": "^3.0.3",
4546
"magic-string": "^0.30.21",

0 commit comments

Comments
 (0)