-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathindex.ts
More file actions
159 lines (146 loc) · 4.48 KB
/
index.ts
File metadata and controls
159 lines (146 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { readFileSync } from 'node:fs'
import type { BuildOptions, Plugin } from 'vite'
import {
addRefreshWrapper,
getPreambleCode,
runtimePublicPath,
silenceUseClientWarning,
} from '@vitejs/react-common'
import { exactRegex } from '@rolldown/pluginutils'
const _dirname = dirname(fileURLToPath(import.meta.url))
const refreshRuntimePath = join(_dirname, 'refresh-runtime.js')
export interface Options {
include?: string | RegExp | Array<string | RegExp>
exclude?: string | RegExp | Array<string | RegExp>
/**
* Control where the JSX factory is imported from.
* @default 'react'
*/
jsxImportSource?: string
}
const defaultIncludeRE = /\.[tj]sx?(?:$|\?)/
const defaultExcludeRE = /\/node_modules\//
export default function viteReact(opts: Options = {}): Plugin[] {
const include = opts.include ?? defaultIncludeRE
const exclude = opts.exclude ?? defaultExcludeRE
const jsxImportSource = opts.jsxImportSource ?? 'react'
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`
const viteConfig: Plugin = {
name: 'vite:react-oxc:config',
config(userConfig, { command }) {
return {
// @ts-expect-error rolldown-vite Vite type incompatibility
build: silenceUseClientWarning(userConfig) as BuildOptions,
oxc: {
jsx: {
runtime: 'automatic',
importSource: jsxImportSource,
refresh: command === 'serve',
development: command === 'serve',
},
jsxRefreshInclude: include,
jsxRefreshExclude: exclude,
},
optimizeDeps: {
include: [
'react',
'react-dom',
jsxImportDevRuntime,
jsxImportRuntime,
],
rolldownOptions: { transform: { jsx: { runtime: 'automatic' } } },
},
}
},
configResolved(config) {
config.logger.warn(
'@vitejs/plugin-react-oxc is deprecated. ' +
'Please use @vitejs/plugin-react instead. ' +
'The changes of this plugin is now included in @vitejs/plugin-react.',
)
},
options() {
if (!this.meta.rolldownVersion) {
throw new Error(
'@vitejs/plugin-react-oxc requires rolldown-vite to be used. ' +
'See https://vitejs.dev/guide/rolldown for more details about rolldown-vite.',
)
}
},
}
const viteConfigPost: Plugin = {
name: 'vite:react-oxc:config-post',
enforce: 'post',
config(userConfig) {
if (userConfig.server?.hmr === false) {
return {
oxc: {
jsx: {
refresh: false,
},
},
}
}
},
}
const viteRefreshRuntime: Plugin = {
name: 'vite:react-oxc:refresh-runtime',
enforce: 'pre',
resolveId: {
filter: { id: exactRegex(runtimePublicPath) },
handler(id) {
return id
},
},
load: {
filter: { id: exactRegex(runtimePublicPath) },
handler(_id) {
return readFileSync(refreshRuntimePath, 'utf-8').replace(
/__README_URL__/g,
'https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-oxc',
)
},
},
}
let skipFastRefresh = false
const viteRefreshWrapper: Plugin = {
name: 'vite:react-oxc:refresh-wrapper',
apply: 'serve',
configResolved(config) {
skipFastRefresh = config.isProduction || config.server.hmr === false
},
transform: {
filter: {
id: { include, exclude },
},
handler(code, id, options) {
const ssr = options?.ssr === true
const [filepath] = id.split('?')
const isJSX = filepath.endsWith('x')
const useFastRefresh =
!skipFastRefresh &&
!ssr &&
(isJSX ||
code.includes(jsxImportDevRuntime) ||
code.includes(jsxImportRuntime))
if (!useFastRefresh) return
const newCode = addRefreshWrapper(code, '@vitejs/plugin-react-oxc', id)
return newCode ? { code: newCode, map: null } : undefined
},
},
transformIndexHtml(_, config) {
if (!skipFastRefresh)
return [
{
tag: 'script',
attrs: { type: 'module' },
children: getPreambleCode(config.server!.config.base),
},
]
},
}
return [viteConfig, viteConfigPost, viteRefreshRuntime, viteRefreshWrapper]
}