@vitejs/plugin-react 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,48 +1,55 @@
1
- 'use strict';
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
2
22
 
3
- const node_path = require('node:path');
4
- const node_url = require('node:url');
5
- const node_fs = require('node:fs');
6
- const vite = require('vite');
7
- const pluginutils = require('@rolldown/pluginutils');
8
-
9
- var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
- function _interopNamespaceCompat(e) {
11
- if (e && typeof e === 'object' && 'default' in e) return e;
12
- const n = Object.create(null);
13
- if (e) {
14
- for (const k in e) {
15
- n[k] = e[k];
16
- }
17
- }
18
- n.default = e;
19
- return n;
20
- }
21
-
22
- const vite__namespace = /*#__PURE__*/_interopNamespaceCompat(vite);
23
+ //#endregion
24
+ const node_path = __toESM(require("node:path"));
25
+ const node_url = __toESM(require("node:url"));
26
+ const node_fs = __toESM(require("node:fs"));
27
+ const vite = __toESM(require("vite"));
28
+ const __rolldown_pluginutils = __toESM(require("@rolldown/pluginutils"));
23
29
 
30
+ //#region ../common/refresh-utils.ts
24
31
  const runtimePublicPath = "/@react-refresh";
25
32
  const reactCompRE = /extends\s+(?:React\.)?(?:Pure)?Component/;
26
33
  const refreshContentRE = /\$RefreshReg\$\(/;
27
- const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(
28
- 1
29
- )}";
34
+ const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(1)}";
30
35
  injectIntoGlobalHook(window);
31
36
  window.$RefreshReg$ = () => {};
32
37
  window.$RefreshSig$ = () => (type) => type;`;
33
38
  const getPreambleCode = (base) => preambleCode.replace("__BASE__", base);
34
39
  const avoidSourceMapOption = Symbol();
35
40
  function addRefreshWrapper(code, map, pluginName, id, reactRefreshHost = "") {
36
- const hasRefresh = refreshContentRE.test(code);
37
- const onlyReactComp = !hasRefresh && reactCompRE.test(code);
38
- const normalizedMap = map === avoidSourceMapOption ? null : map;
39
- if (!hasRefresh && !onlyReactComp) return { code, map: normalizedMap };
40
- const avoidSourceMap = map === avoidSourceMapOption;
41
- const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
42
- let newCode = code;
43
- if (hasRefresh) {
44
- const refreshHead = removeLineBreaksIfNeeded(
45
- `let prevRefreshReg;
41
+ const hasRefresh = refreshContentRE.test(code);
42
+ const onlyReactComp = !hasRefresh && reactCompRE.test(code);
43
+ const normalizedMap = map === avoidSourceMapOption ? null : map;
44
+ if (!hasRefresh && !onlyReactComp) return {
45
+ code,
46
+ map: normalizedMap
47
+ };
48
+ const avoidSourceMap = map === avoidSourceMapOption;
49
+ const newMap = typeof normalizedMap === "string" ? JSON.parse(normalizedMap) : normalizedMap;
50
+ let newCode = code;
51
+ if (hasRefresh) {
52
+ const refreshHead = removeLineBreaksIfNeeded(`let prevRefreshReg;
46
53
  let prevRefreshSig;
47
54
 
48
55
  if (import.meta.hot && !inWebWorker) {
@@ -58,363 +65,279 @@ if (import.meta.hot && !inWebWorker) {
58
65
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
59
66
  }
60
67
 
61
- `,
62
- avoidSourceMap
63
- );
64
- newCode = `${refreshHead}${newCode}
68
+ `, avoidSourceMap);
69
+ newCode = `${refreshHead}${newCode}
65
70
 
66
71
  if (import.meta.hot && !inWebWorker) {
67
72
  window.$RefreshReg$ = prevRefreshReg;
68
73
  window.$RefreshSig$ = prevRefreshSig;
69
74
  }
70
75
  `;
71
- if (newMap) {
72
- newMap.mappings = ";".repeat(16) + newMap.mappings;
73
- }
74
- }
75
- const sharedHead = removeLineBreaksIfNeeded(
76
- `import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
76
+ if (newMap) newMap.mappings = ";".repeat(16) + newMap.mappings;
77
+ }
78
+ const sharedHead = removeLineBreaksIfNeeded(`import * as RefreshRuntime from "${reactRefreshHost}${runtimePublicPath}";
77
79
  const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
78
80
 
79
- `,
80
- avoidSourceMap
81
- );
82
- newCode = `${sharedHead}${newCode}
81
+ `, avoidSourceMap);
82
+ newCode = `${sharedHead}${newCode}
83
83
 
84
84
  if (import.meta.hot && !inWebWorker) {
85
85
  RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
86
- RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(
87
- id
88
- )}, currentExports);
86
+ RefreshRuntime.registerExportsForReactRefresh(${JSON.stringify(id)}, currentExports);
89
87
  import.meta.hot.accept((nextExports) => {
90
88
  if (!nextExports) return;
91
- const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(
92
- id
93
- )}, currentExports, nextExports);
89
+ const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate(${JSON.stringify(id)}, currentExports, nextExports);
94
90
  if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
95
91
  });
96
92
  });
97
93
  }
98
94
  `;
99
- if (newMap) {
100
- newMap.mappings = ";;;" + newMap.mappings;
101
- }
102
- return { code: newCode, map: newMap };
95
+ if (newMap) newMap.mappings = ";;;" + newMap.mappings;
96
+ return {
97
+ code: newCode,
98
+ map: newMap
99
+ };
103
100
  }
104
101
  function removeLineBreaksIfNeeded(code, enabled) {
105
- return enabled ? code.replace(/\n/g, "") : code;
102
+ return enabled ? code.replace(/\n/g, "") : code;
106
103
  }
107
104
 
108
- const silenceUseClientWarning = (userConfig) => ({
109
- rollupOptions: {
110
- onwarn(warning, defaultHandler) {
111
- if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) {
112
- return;
113
- }
114
- if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) {
115
- return;
116
- }
117
- if (userConfig.build?.rollupOptions?.onwarn) {
118
- userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
119
- } else {
120
- defaultHandler(warning);
121
- }
122
- }
123
- }
124
- });
105
+ //#endregion
106
+ //#region ../common/warning.ts
107
+ const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warning, defaultHandler) {
108
+ var _userConfig$build;
109
+ if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) return;
110
+ if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) return;
111
+ if ((_userConfig$build = userConfig.build) === null || _userConfig$build === void 0 || (_userConfig$build = _userConfig$build.rollupOptions) === null || _userConfig$build === void 0 ? void 0 : _userConfig$build.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
112
+ else defaultHandler(warning);
113
+ } } });
125
114
 
126
- const _dirname = node_path.dirname(node_url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))));
127
- const refreshRuntimePath = node_path.join(_dirname, "refresh-runtime.js") ;
115
+ //#endregion
116
+ //#region src/index.ts
117
+ const _dirname = (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
118
+ const refreshRuntimePath = (0, node_path.join)(_dirname, "refresh-runtime.js");
128
119
  let babel;
129
120
  async function loadBabel() {
130
- if (!babel) {
131
- babel = await import('@babel/core');
132
- }
133
- return babel;
121
+ if (!babel) babel = await import("@babel/core");
122
+ return babel;
134
123
  }
135
124
  const defaultIncludeRE = /\.[tj]sx?$/;
136
125
  const tsRE = /\.tsx?$/;
137
126
  function viteReact(opts = {}) {
138
- const include = opts.include ?? defaultIncludeRE;
139
- const exclude = opts.exclude;
140
- const filter = vite.createFilter(include, exclude);
141
- const jsxImportSource = opts.jsxImportSource ?? "react";
142
- const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
143
- const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
144
- let runningInVite = false;
145
- let isProduction = true;
146
- let projectRoot = process.cwd();
147
- let skipFastRefresh = true;
148
- let runPluginOverrides;
149
- let staticBabelOptions;
150
- const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
151
- const viteBabel = {
152
- name: "vite:react-babel",
153
- enforce: "pre",
154
- config() {
155
- if (opts.jsxRuntime === "classic") {
156
- if ("rolldownVersion" in vite__namespace) {
157
- return {
158
- oxc: {
159
- jsx: {
160
- runtime: "classic",
161
- // disable __self and __source injection even in dev
162
- // as this plugin injects them by babel and oxc will throw
163
- // if development is enabled and those properties are already present
164
- development: false
165
- }
166
- }
167
- };
168
- } else {
169
- return {
170
- esbuild: {
171
- jsx: "transform"
172
- }
173
- };
174
- }
175
- } else {
176
- return {
177
- esbuild: {
178
- jsx: "automatic",
179
- jsxImportSource: opts.jsxImportSource
180
- },
181
- optimizeDeps: "rolldownVersion" in vite__namespace ? { rollupOptions: { jsx: { mode: "automatic" } } } : { esbuildOptions: { jsx: "automatic" } }
182
- };
183
- }
184
- },
185
- configResolved(config) {
186
- runningInVite = true;
187
- projectRoot = config.root;
188
- isProduction = config.isProduction;
189
- skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
190
- if ("jsxPure" in opts) {
191
- config.logger.warnOnce(
192
- "[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly."
193
- );
194
- }
195
- const hooks = config.plugins.map((plugin) => plugin.api?.reactBabel).filter(defined);
196
- if ("rolldownVersion" in vite__namespace && !opts.babel && !hooks.length && !opts.disableOxcRecommendation) {
197
- config.logger.warn(
198
- "[vite:react-babel] We recommend switching to `@vitejs/plugin-react-oxc` for improved performance. More information at https://vite.dev/rolldown"
199
- );
200
- }
201
- if (hooks.length > 0) {
202
- runPluginOverrides = (babelOptions, context) => {
203
- hooks.forEach((hook) => hook(babelOptions, context, config));
204
- };
205
- } else if (typeof opts.babel !== "function") {
206
- staticBabelOptions = createBabelOptions(opts.babel);
207
- if (canSkipBabel(staticBabelOptions.plugins, staticBabelOptions) && skipFastRefresh && (opts.jsxRuntime === "classic" ? isProduction : true)) {
208
- delete viteBabel.transform;
209
- }
210
- }
211
- },
212
- options(options) {
213
- if (!runningInVite) {
214
- options.jsx = {
215
- mode: opts.jsxRuntime,
216
- importSource: opts.jsxImportSource
217
- };
218
- return options;
219
- }
220
- },
221
- transform: {
222
- filter: {
223
- id: {
224
- include: pluginutils.makeIdFiltersToMatchWithQuery(include),
225
- exclude: [
226
- ...exclude ? pluginutils.makeIdFiltersToMatchWithQuery(ensureArray(exclude)) : [],
227
- /\/node_modules\//
228
- ]
229
- }
230
- },
231
- async handler(code, id, options) {
232
- if (id.includes("/node_modules/")) return;
233
- const [filepath] = id.split("?");
234
- if (!filter(filepath)) return;
235
- const ssr = options?.ssr === true;
236
- const babelOptions = (() => {
237
- if (staticBabelOptions) return staticBabelOptions;
238
- const newBabelOptions = createBabelOptions(
239
- typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel
240
- );
241
- runPluginOverrides?.(newBabelOptions, { id, ssr });
242
- return newBabelOptions;
243
- })();
244
- const plugins = [...babelOptions.plugins];
245
- const isJSX = filepath.endsWith("x");
246
- const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
247
- if (useFastRefresh) {
248
- plugins.push([
249
- await loadPlugin("react-refresh/babel"),
250
- { skipEnvCheck: true }
251
- ]);
252
- }
253
- if (opts.jsxRuntime === "classic" && isJSX) {
254
- if (!isProduction) {
255
- plugins.push(
256
- await loadPlugin("@babel/plugin-transform-react-jsx-self"),
257
- await loadPlugin("@babel/plugin-transform-react-jsx-source")
258
- );
259
- }
260
- }
261
- if (canSkipBabel(plugins, babelOptions)) {
262
- return;
263
- }
264
- const parserPlugins = [...babelOptions.parserOpts.plugins];
265
- if (!filepath.endsWith(".ts")) {
266
- parserPlugins.push("jsx");
267
- }
268
- if (tsRE.test(filepath)) {
269
- parserPlugins.push("typescript");
270
- }
271
- const babel2 = await loadBabel();
272
- const result = await babel2.transformAsync(code, {
273
- ...babelOptions,
274
- root: projectRoot,
275
- filename: id,
276
- sourceFileName: filepath,
277
- // Required for esbuild.jsxDev to provide correct line numbers
278
- // This creates issues the react compiler because the re-order is too important
279
- // People should use @babel/plugin-transform-react-jsx-development to get back good line numbers
280
- retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
281
- parserOpts: {
282
- ...babelOptions.parserOpts,
283
- sourceType: "module",
284
- allowAwaitOutsideFunction: true,
285
- plugins: parserPlugins
286
- },
287
- generatorOpts: {
288
- ...babelOptions.generatorOpts,
289
- // import attributes parsing available without plugin since 7.26
290
- importAttributesKeyword: "with",
291
- decoratorsBeforeExport: true
292
- },
293
- plugins,
294
- sourceMaps: true
295
- });
296
- if (result) {
297
- if (!useFastRefresh) {
298
- return { code: result.code, map: result.map };
299
- }
300
- return addRefreshWrapper(
301
- result.code,
302
- result.map,
303
- "@vitejs/plugin-react",
304
- id,
305
- opts.reactRefreshHost
306
- );
307
- }
308
- }
309
- }
310
- };
311
- const dependencies = [
312
- "react",
313
- "react-dom",
314
- jsxImportDevRuntime,
315
- jsxImportRuntime
316
- ];
317
- const staticBabelPlugins = typeof opts.babel === "object" ? opts.babel?.plugins ?? [] : [];
318
- const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
319
- if (reactCompilerPlugin != null) {
320
- const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
321
- dependencies.push(reactCompilerRuntimeModule);
322
- }
323
- const viteReactRefresh = {
324
- name: "vite:react-refresh",
325
- enforce: "pre",
326
- config: (userConfig) => ({
327
- build: silenceUseClientWarning(userConfig),
328
- optimizeDeps: {
329
- include: dependencies
330
- },
331
- resolve: {
332
- dedupe: ["react", "react-dom"]
333
- }
334
- }),
335
- resolveId: {
336
- filter: { id: pluginutils.exactRegex(runtimePublicPath) },
337
- handler(id) {
338
- if (id === runtimePublicPath) {
339
- return id;
340
- }
341
- }
342
- },
343
- load: {
344
- filter: { id: pluginutils.exactRegex(runtimePublicPath) },
345
- handler(id) {
346
- if (id === runtimePublicPath) {
347
- return node_fs.readFileSync(refreshRuntimePath, "utf-8").replace(
348
- /__README_URL__/g,
349
- "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react"
350
- );
351
- }
352
- }
353
- },
354
- transformIndexHtml(_, config) {
355
- if (!skipFastRefresh)
356
- return [
357
- {
358
- tag: "script",
359
- attrs: { type: "module" },
360
- children: getPreambleCode(config.server.config.base)
361
- }
362
- ];
363
- }
364
- };
365
- return [viteBabel, viteReactRefresh];
127
+ var _opts$babel;
128
+ const include = opts.include ?? defaultIncludeRE;
129
+ const exclude = opts.exclude;
130
+ const filter = (0, vite.createFilter)(include, exclude);
131
+ const jsxImportSource = opts.jsxImportSource ?? "react";
132
+ const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
133
+ const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
134
+ let runningInVite = false;
135
+ let isProduction = true;
136
+ let projectRoot = process.cwd();
137
+ let skipFastRefresh = true;
138
+ let runPluginOverrides;
139
+ let staticBabelOptions;
140
+ const importReactRE = /\bimport\s+(?:\*\s+as\s+)?React\b/;
141
+ const viteBabel = {
142
+ name: "vite:react-babel",
143
+ enforce: "pre",
144
+ config() {
145
+ if (opts.jsxRuntime === "classic") if ("rolldownVersion" in vite) return { oxc: { jsx: {
146
+ runtime: "classic",
147
+ development: false
148
+ } } };
149
+ else return { esbuild: { jsx: "transform" } };
150
+ else return {
151
+ esbuild: {
152
+ jsx: "automatic",
153
+ jsxImportSource: opts.jsxImportSource
154
+ },
155
+ optimizeDeps: "rolldownVersion" in vite ? { rollupOptions: { jsx: { mode: "automatic" } } } : { esbuildOptions: { jsx: "automatic" } }
156
+ };
157
+ },
158
+ configResolved(config) {
159
+ runningInVite = true;
160
+ projectRoot = config.root;
161
+ isProduction = config.isProduction;
162
+ skipFastRefresh = isProduction || config.command === "build" || config.server.hmr === false;
163
+ if ("jsxPure" in opts) config.logger.warnOnce("[@vitejs/plugin-react] jsxPure was removed. You can configure esbuild.jsxSideEffects directly.");
164
+ const hooks = config.plugins.map((plugin) => {
165
+ var _plugin$api;
166
+ return (_plugin$api = plugin.api) === null || _plugin$api === void 0 ? void 0 : _plugin$api.reactBabel;
167
+ }).filter(defined);
168
+ if ("rolldownVersion" in vite && !opts.babel && !hooks.length && !opts.disableOxcRecommendation) config.logger.warn("[vite:react-babel] We recommend switching to `@vitejs/plugin-react-oxc` for improved performance. More information at https://vite.dev/rolldown");
169
+ if (hooks.length > 0) runPluginOverrides = (babelOptions, context) => {
170
+ hooks.forEach((hook) => hook(babelOptions, context, config));
171
+ };
172
+ else if (typeof opts.babel !== "function") {
173
+ staticBabelOptions = createBabelOptions(opts.babel);
174
+ if (canSkipBabel(staticBabelOptions.plugins, staticBabelOptions) && skipFastRefresh && (opts.jsxRuntime === "classic" ? isProduction : true)) delete viteBabel.transform;
175
+ }
176
+ },
177
+ options(options) {
178
+ if (!runningInVite) {
179
+ options.jsx = {
180
+ mode: opts.jsxRuntime,
181
+ importSource: opts.jsxImportSource
182
+ };
183
+ return options;
184
+ }
185
+ },
186
+ transform: {
187
+ filter: { id: {
188
+ include: (0, __rolldown_pluginutils.makeIdFiltersToMatchWithQuery)(include),
189
+ exclude: [...exclude ? (0, __rolldown_pluginutils.makeIdFiltersToMatchWithQuery)(ensureArray(exclude)) : [], /\/node_modules\//]
190
+ } },
191
+ async handler(code, id, options) {
192
+ if (id.includes("/node_modules/")) return;
193
+ const [filepath] = id.split("?");
194
+ if (!filter(filepath)) return;
195
+ const ssr = (options === null || options === void 0 ? void 0 : options.ssr) === true;
196
+ const babelOptions = (() => {
197
+ if (staticBabelOptions) return staticBabelOptions;
198
+ const newBabelOptions = createBabelOptions(typeof opts.babel === "function" ? opts.babel(id, { ssr }) : opts.babel);
199
+ runPluginOverrides === null || runPluginOverrides === void 0 || runPluginOverrides(newBabelOptions, {
200
+ id,
201
+ ssr
202
+ });
203
+ return newBabelOptions;
204
+ })();
205
+ const plugins = [...babelOptions.plugins];
206
+ const isJSX = filepath.endsWith("x");
207
+ const useFastRefresh = !skipFastRefresh && !ssr && (isJSX || (opts.jsxRuntime === "classic" ? importReactRE.test(code) : code.includes(jsxImportDevRuntime) || code.includes(jsxImportRuntime)));
208
+ if (useFastRefresh) plugins.push([await loadPlugin("react-refresh/babel"), { skipEnvCheck: true }]);
209
+ if (opts.jsxRuntime === "classic" && isJSX) {
210
+ if (!isProduction) plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source"));
211
+ }
212
+ if (canSkipBabel(plugins, babelOptions)) return;
213
+ const parserPlugins = [...babelOptions.parserOpts.plugins];
214
+ if (!filepath.endsWith(".ts")) parserPlugins.push("jsx");
215
+ if (tsRE.test(filepath)) parserPlugins.push("typescript");
216
+ const babel$1 = await loadBabel();
217
+ const result = await babel$1.transformAsync(code, {
218
+ ...babelOptions,
219
+ root: projectRoot,
220
+ filename: id,
221
+ sourceFileName: filepath,
222
+ retainLines: getReactCompilerPlugin(plugins) != null ? false : !isProduction && isJSX && opts.jsxRuntime !== "classic",
223
+ parserOpts: {
224
+ ...babelOptions.parserOpts,
225
+ sourceType: "module",
226
+ allowAwaitOutsideFunction: true,
227
+ plugins: parserPlugins
228
+ },
229
+ generatorOpts: {
230
+ ...babelOptions.generatorOpts,
231
+ importAttributesKeyword: "with",
232
+ decoratorsBeforeExport: true
233
+ },
234
+ plugins,
235
+ sourceMaps: true
236
+ });
237
+ if (result) {
238
+ if (!useFastRefresh) return {
239
+ code: result.code,
240
+ map: result.map
241
+ };
242
+ return addRefreshWrapper(result.code, result.map, "@vitejs/plugin-react", id, opts.reactRefreshHost);
243
+ }
244
+ }
245
+ }
246
+ };
247
+ const dependencies = [
248
+ "react",
249
+ "react-dom",
250
+ jsxImportDevRuntime,
251
+ jsxImportRuntime
252
+ ];
253
+ const staticBabelPlugins = typeof opts.babel === "object" ? ((_opts$babel = opts.babel) === null || _opts$babel === void 0 ? void 0 : _opts$babel.plugins) ?? [] : [];
254
+ const reactCompilerPlugin = getReactCompilerPlugin(staticBabelPlugins);
255
+ if (reactCompilerPlugin != null) {
256
+ const reactCompilerRuntimeModule = getReactCompilerRuntimeModule(reactCompilerPlugin);
257
+ dependencies.push(reactCompilerRuntimeModule);
258
+ }
259
+ const viteReactRefresh = {
260
+ name: "vite:react-refresh",
261
+ enforce: "pre",
262
+ config: (userConfig) => ({
263
+ build: silenceUseClientWarning(userConfig),
264
+ optimizeDeps: { include: dependencies },
265
+ resolve: { dedupe: ["react", "react-dom"] }
266
+ }),
267
+ resolveId: {
268
+ filter: { id: (0, __rolldown_pluginutils.exactRegex)(runtimePublicPath) },
269
+ handler(id) {
270
+ if (id === runtimePublicPath) return id;
271
+ }
272
+ },
273
+ load: {
274
+ filter: { id: (0, __rolldown_pluginutils.exactRegex)(runtimePublicPath) },
275
+ handler(id) {
276
+ if (id === runtimePublicPath) return (0, node_fs.readFileSync)(refreshRuntimePath, "utf-8").replace(/__README_URL__/g, "https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react");
277
+ }
278
+ },
279
+ transformIndexHtml(_, config) {
280
+ if (!skipFastRefresh) return [{
281
+ tag: "script",
282
+ attrs: { type: "module" },
283
+ children: getPreambleCode(config.server.config.base)
284
+ }];
285
+ }
286
+ };
287
+ return [viteBabel, viteReactRefresh];
366
288
  }
367
289
  viteReact.preambleCode = preambleCode;
368
290
  function canSkipBabel(plugins, babelOptions) {
369
- return !(plugins.length || babelOptions.presets.length || babelOptions.configFile || babelOptions.babelrc);
291
+ return !(plugins.length || babelOptions.presets.length || babelOptions.configFile || babelOptions.babelrc);
370
292
  }
371
293
  const loadedPlugin = /* @__PURE__ */ new Map();
372
294
  function loadPlugin(path) {
373
- const cached = loadedPlugin.get(path);
374
- if (cached) return cached;
375
- const promise = import(path).then((module) => {
376
- const value = module.default || module;
377
- loadedPlugin.set(path, value);
378
- return value;
379
- });
380
- loadedPlugin.set(path, promise);
381
- return promise;
295
+ const cached = loadedPlugin.get(path);
296
+ if (cached) return cached;
297
+ const promise = import(path).then((module$1) => {
298
+ const value = module$1.default || module$1;
299
+ loadedPlugin.set(path, value);
300
+ return value;
301
+ });
302
+ loadedPlugin.set(path, promise);
303
+ return promise;
382
304
  }
383
305
  function createBabelOptions(rawOptions) {
384
- const babelOptions = {
385
- babelrc: false,
386
- configFile: false,
387
- ...rawOptions
388
- };
389
- babelOptions.plugins ||= [];
390
- babelOptions.presets ||= [];
391
- babelOptions.overrides ||= [];
392
- babelOptions.parserOpts ||= {};
393
- babelOptions.parserOpts.plugins ||= [];
394
- return babelOptions;
306
+ var _babelOptions$parserO;
307
+ const babelOptions = {
308
+ babelrc: false,
309
+ configFile: false,
310
+ ...rawOptions
311
+ };
312
+ babelOptions.plugins || (babelOptions.plugins = []);
313
+ babelOptions.presets || (babelOptions.presets = []);
314
+ babelOptions.overrides || (babelOptions.overrides = []);
315
+ babelOptions.parserOpts || (babelOptions.parserOpts = {});
316
+ (_babelOptions$parserO = babelOptions.parserOpts).plugins || (_babelOptions$parserO.plugins = []);
317
+ return babelOptions;
395
318
  }
396
319
  function defined(value) {
397
- return value !== void 0;
320
+ return value !== void 0;
398
321
  }
399
322
  function getReactCompilerPlugin(plugins) {
400
- return plugins.find(
401
- (p) => p === "babel-plugin-react-compiler" || Array.isArray(p) && p[0] === "babel-plugin-react-compiler"
402
- );
323
+ return plugins.find((p) => p === "babel-plugin-react-compiler" || Array.isArray(p) && p[0] === "babel-plugin-react-compiler");
403
324
  }
404
325
  function getReactCompilerRuntimeModule(plugin) {
405
- let moduleName = "react/compiler-runtime";
406
- if (Array.isArray(plugin)) {
407
- if (plugin[1]?.target === "17" || plugin[1]?.target === "18") {
408
- moduleName = "react-compiler-runtime";
409
- } else if (typeof plugin[1]?.runtimeModule === "string") {
410
- moduleName = plugin[1]?.runtimeModule;
411
- }
412
- }
413
- return moduleName;
326
+ let moduleName = "react/compiler-runtime";
327
+ if (Array.isArray(plugin)) {
328
+ var _plugin$, _plugin$2, _plugin$3;
329
+ if (((_plugin$ = plugin[1]) === null || _plugin$ === void 0 ? void 0 : _plugin$.target) === "17" || ((_plugin$2 = plugin[1]) === null || _plugin$2 === void 0 ? void 0 : _plugin$2.target) === "18") moduleName = "react-compiler-runtime";
330
+ else if (typeof ((_plugin$3 = plugin[1]) === null || _plugin$3 === void 0 ? void 0 : _plugin$3.runtimeModule) === "string") {
331
+ var _plugin$4;
332
+ moduleName = (_plugin$4 = plugin[1]) === null || _plugin$4 === void 0 ? void 0 : _plugin$4.runtimeModule;
333
+ }
334
+ }
335
+ return moduleName;
414
336
  }
415
337
  function ensureArray(value) {
416
- return Array.isArray(value) ? value : [value];
338
+ return Array.isArray(value) ? value : [value];
417
339
  }
418
340
 
341
+ //#endregion
419
342
  module.exports = viteReact;
420
- module.exports.default = viteReact;
343
+ module.exports.default = module.exports