MCPcopy
hub / github.com/vitejs/vite / oxcPlugin

Function oxcPlugin

packages/vite/src/node/plugins/oxc.ts:210–341  ·  view source on GitHub ↗
(config: ResolvedConfig)

Source from the content-addressed store, hash-verified

208}
209
210export function oxcPlugin(config: ResolvedConfig): Plugin {
211 const options = config.oxc as OxcOptions
212 const {
213 jsxInject,
214 include,
215 exclude,
216 jsxRefreshInclude,
217 jsxRefreshExclude,
218 ...oxcTransformOptions
219 } = options
220
221 const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/)
222 const jsxRefreshFilter =
223 jsxRefreshInclude || jsxRefreshExclude
224 ? createFilter(jsxRefreshInclude, jsxRefreshExclude)
225 : undefined
226
227 const jsxImportSource =
228 (typeof oxcTransformOptions.jsx === 'object' &&
229 oxcTransformOptions.jsx.importSource) ||
230 'react'
231 const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
232 const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`
233
234 const getModifiedOxcTransformOptions = (
235 oxcTransformOptions: OxcTransformOptions,
236 id: string,
237 code: string,
238 environment: Environment,
239 ): OxcTransformOptions => {
240 const result: OxcTransformOptions = {
241 ...oxcTransformOptions,
242 sourcemap:
243 environment.mode !== 'build' || !!environment.config.build.sourcemap,
244 }
245
246 const jsxOptions = result.jsx
247
248 // disable refresh based by the same condition as @vitejs/plugin-react
249 // https://github.com/vitejs/vite-plugin-react/blob/c8ecad052001b6fc42e508f18433e6b305bca641/packages/plugin-react/src/index.ts#L261-L269
250 const [filepath] = id.split('?')
251 const isJSX = filepath.endsWith('x')
252
253 if (
254 typeof jsxOptions === 'object' &&
255 jsxOptions.refresh &&
256 (environment.config.consumer === 'server' ||
257 (jsxRefreshFilter && !jsxRefreshFilter(id)) ||
258 !(
259 isJSX ||
260 code.includes(jsxImportRuntime) ||
261 code.includes(jsxImportDevRuntime)
262 ))
263 ) {
264 result.jsx = { ...jsxOptions, refresh: false }
265 }
266 if (jsxRefreshFilter?.(id) && !JS_TYPES_RE.test(cleanUrl(id))) {
267 result.lang = 'js'

Callers 1

resolvePluginsFunction · 0.90

Calls 1

createFilterFunction · 0.85

Tested by

no test coverage detected