-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathindex.ts
More file actions
323 lines (310 loc) · 9.86 KB
/
index.ts
File metadata and controls
323 lines (310 loc) · 9.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { createRequire } from 'node:module'
import {
type JscTarget,
type Output,
type ParserConfig,
type ReactConfig,
type Options as SWCOptions,
transform,
} from '@swc/core'
import type { Plugin } from 'vite'
import {
addRefreshWrapper,
getPreambleCode,
runtimePublicPath,
silenceUseClientWarning,
virtualPreamblePlugin,
} from '@vitejs/react-common'
import * as vite from 'vite'
import { exactRegex } from '@rolldown/pluginutils'
const resolve = createRequire(import.meta.url).resolve
type Options = {
/**
* Control where the JSX factory is imported from.
* @default "react"
*/
jsxImportSource?: string
/**
* Enable TypeScript decorators. Requires experimentalDecorators in tsconfig.
* @default false
*/
tsDecorators?: boolean
/**
* Use SWC plugins. Enable SWC at build time.
* @default undefined
*/
plugins?: [string, Record<string, any>][]
/**
* Set the target for SWC in dev. This can avoid to down-transpile private class method for example.
* For production target, see https://vite.dev/config/build-options.html#build-target
* @default "es2020"
*/
devTarget?: JscTarget
/**
* Override the default include list (.ts, .tsx, .mts, .jsx, .mdx).
* This requires to redefine the config for any file you want to be included.
* If you want to trigger fast refresh on compiled JS, use `jsx: true`.
* Exclusion of node_modules should be handled by the function if needed.
*/
parserConfig?: (id: string) => ParserConfig | undefined
/**
* React Fast Refresh runtime URL prefix.
* Useful in a module federation context to enable HMR by specifying
* the host application URL in a Vite config of a remote application.
* @example
* reactRefreshHost: 'http://localhost:3000'
*/
reactRefreshHost?: string
/**
* The future of Vite is with OXC, and from the beginning this was a design choice
* to not exposed too many specialties from SWC so that Vite React users can move to
* another transformer later.
* Also debugging why some specific version of decorators with some other unstable/legacy
* feature doesn't work is not fun, so we won't provide support for it, hence the name `useAtYourOwnRisk`
*/
useAtYourOwnRisk_mutateSwcOptions?: (options: SWCOptions) => void
/**
* If set, disables the recommendation to use `@vitejs/plugin-react`
*/
disableOxcRecommendation?: boolean
}
const react = (_options?: Options): Plugin[] => {
let hmrDisabled = true
let viteCacheRoot: string | undefined
const options = {
jsxImportSource: _options?.jsxImportSource ?? 'react',
tsDecorators: _options?.tsDecorators,
plugins: _options?.plugins
? _options?.plugins.map((el): typeof el => [resolve(el[0]), el[1]])
: undefined,
devTarget: _options?.devTarget ?? 'es2020',
parserConfig: _options?.parserConfig,
reactRefreshHost: _options?.reactRefreshHost,
useAtYourOwnRisk_mutateSwcOptions:
_options?.useAtYourOwnRisk_mutateSwcOptions,
disableOxcRecommendation: _options?.disableOxcRecommendation,
}
return [
{
name: 'vite:react-swc:resolve-runtime',
apply: 'serve',
enforce: 'pre', // Run before Vite default resolve to avoid syscalls
resolveId: {
filter: { id: exactRegex(runtimePublicPath) },
handler: (id) => (id === runtimePublicPath ? id : undefined),
},
load: {
filter: { id: exactRegex(runtimePublicPath) },
handler: (id) =>
id === runtimePublicPath
? readFileSync(
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- import.meta.dirname is stable in the newer versions and the API has not changed
join(import.meta.dirname, 'refresh-runtime.js'),
'utf-8',
).replace(
/__README_URL__/g,
'https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react-swc',
)
: undefined,
},
},
{
name: 'vite:react-swc',
apply: 'serve',
config: () => ({
esbuild: false,
// NOTE: oxc option only exists in rolldown-vite
oxc: false,
optimizeDeps: {
include: [`${options.jsxImportSource}/jsx-dev-runtime`],
...('rolldownVersion' in vite
? {
rolldownOptions: {
transform: { jsx: { runtime: 'automatic' } },
},
}
: { esbuildOptions: { jsx: 'automatic' } }),
},
}),
configResolved(config) {
viteCacheRoot = config.cacheDir
hmrDisabled = config.server.hmr === false
const mdxIndex = config.plugins.findIndex(
(p) => p.name === '@mdx-js/rollup',
)
if (
mdxIndex !== -1 &&
mdxIndex >
config.plugins.findIndex((p) => p.name === 'vite:react-swc')
) {
throw new Error(
'[vite:react-swc] The MDX plugin should be placed before this plugin',
)
}
if (
'rolldownVersion' in vite &&
!options.plugins &&
!options.useAtYourOwnRisk_mutateSwcOptions &&
!options.disableOxcRecommendation
) {
config.logger.warn(
'[vite:react-swc] We recommend switching to `@vitejs/plugin-react` for improved performance as no swc plugins are used. More information at https://vite.dev/rolldown',
)
}
},
transformIndexHtml: (_, config) => {
if (!hmrDisabled) {
return [
{
tag: 'script',
attrs: { type: 'module' },
children: getPreambleCode(config.server!.config.base),
},
]
}
},
async transform(code, _id, transformOptions) {
const id = _id.split('?')[0]
const refresh = !transformOptions?.ssr && !hmrDisabled
const result = await transformWithOptions(
id,
code,
options.devTarget,
options,
viteCacheRoot,
{
refresh,
development: true,
runtime: 'automatic',
importSource: options.jsxImportSource,
},
)
if (!result) return
if (!refresh) return result
const newCode = addRefreshWrapper(
result.code,
'@vitejs/plugin-react-swc',
id,
options.reactRefreshHost,
)
return { code: newCode ?? result.code, map: result.map }
},
},
options.plugins || options.useAtYourOwnRisk_mutateSwcOptions
? {
name: 'vite:react-swc',
apply: 'build',
enforce: 'pre', // Run before esbuild
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
}),
configResolved(config) {
viteCacheRoot = config.cacheDir
},
transform: (code, _id) =>
transformWithOptions(
_id.split('?')[0],
code,
'esnext',
options,
viteCacheRoot,
{
runtime: 'automatic',
importSource: options.jsxImportSource,
},
),
}
: {
name: 'vite:react-swc',
apply: 'build',
config: (userConfig) => ({
build: silenceUseClientWarning(userConfig),
esbuild: {
jsx: 'automatic',
jsxImportSource: options.jsxImportSource,
tsconfigRaw: {
compilerOptions: { useDefineForClassFields: true },
},
},
}),
configResolved(config) {
viteCacheRoot = config.cacheDir
},
},
virtualPreamblePlugin({
name: '@vitejs/plugin-react-swc/preamble',
isEnabled: () => !hmrDisabled,
}),
]
}
const transformWithOptions = async (
id: string,
code: string,
target: JscTarget,
options: Options,
viteCacheRoot: string | undefined,
reactConfig: ReactConfig,
) => {
const decorators = options?.tsDecorators ?? false
const parser: ParserConfig | undefined = options.parserConfig
? options.parserConfig(id)
: id.endsWith('.tsx')
? { syntax: 'typescript', tsx: true, decorators }
: id.endsWith('.ts') || id.endsWith('.mts')
? { syntax: 'typescript', tsx: false, decorators }
: id.endsWith('.jsx')
? { syntax: 'ecmascript', jsx: true }
: id.endsWith('.mdx')
? // JSX is required to trigger fast refresh transformations, even if MDX already transforms it
{ syntax: 'ecmascript', jsx: true }
: undefined
if (!parser) return
let result: Output
try {
const swcOptions: SWCOptions = {
filename: id,
swcrc: false,
configFile: false,
sourceMaps: true,
jsc: {
target,
parser,
experimental: {
plugins: options.plugins,
cacheRoot: join(viteCacheRoot ?? 'node_modules/.vite', '.swc'),
},
transform: {
useDefineForClassFields: true,
react: reactConfig,
},
},
}
if (options.useAtYourOwnRisk_mutateSwcOptions) {
options.useAtYourOwnRisk_mutateSwcOptions(swcOptions)
}
result = await transform(code, swcOptions)
} catch (e: any) {
const message: string = e.message
const fileStartIndex = message.indexOf('╭─[')
if (fileStartIndex !== -1) {
const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)\]/)
if (match) {
e.line = match[1]
e.column = match[2]
}
}
throw e
}
return result
}
export default react
// Compat for require
function pluginForCjs(this: unknown, options: Options): Plugin[] {
return react.call(this, options)
}
Object.assign(pluginForCjs, {
default: pluginForCjs,
})
export { pluginForCjs as 'module.exports' }