@vitejs/plugin-react 6.1.2 → 6.2.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/README.md CHANGED
@@ -8,8 +8,8 @@ The default Vite plugin for React projects.
8
8
 
9
9
  ```js
10
10
  // vite.config.js
11
- import { defineConfig } from 'vite'
12
11
  import react from '@vitejs/plugin-react'
12
+ import { defineConfig } from 'vite'
13
13
 
14
14
  export default defineConfig({
15
15
  plugins: [react()],
@@ -23,9 +23,9 @@ export default defineConfig({
23
23
  Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
24
24
 
25
25
  ```js
26
- import { defineConfig } from 'vite'
27
- import react from '@vitejs/plugin-react'
28
26
  import mdx from '@mdx-js/rollup'
27
+ import react from '@vitejs/plugin-react'
28
+ import { defineConfig } from 'vite'
29
29
 
30
30
  export default defineConfig({
31
31
  plugins: [
@@ -41,8 +41,8 @@ The default value is `/node_modules/`. You may use it to exclude JSX/TSX files t
41
41
  Except if explicitly desired, you should keep `node_modules` in the exclude list:
42
42
 
43
43
  ```js
44
- import { defineConfig } from 'vite'
45
44
  import react from '@vitejs/plugin-react'
45
+ import { defineConfig } from 'vite'
46
46
 
47
47
  export default defineConfig({
48
48
  plugins: [
@@ -92,8 +92,8 @@ npm install -D oxc-transform-react
92
92
 
93
93
  ```js
94
94
  // vite.config.js
95
- import { defineConfig } from 'vite'
96
95
  import react from '@vitejs/plugin-react'
96
+ import { defineConfig } from 'vite'
97
97
 
98
98
  export default defineConfig({
99
99
  plugins: [react({ compiler: true })],
@@ -128,9 +128,9 @@ npm install -D @types/babel__core
128
128
 
129
129
  ```js
130
130
  // vite.config.js
131
- import { defineConfig } from 'vite'
132
- import react, { reactCompilerPreset } from '@vitejs/plugin-react'
133
131
  import babel from '@rolldown/plugin-babel'
132
+ import react, { reactCompilerPreset } from '@vitejs/plugin-react'
133
+ import { defineConfig } from 'vite'
134
134
 
135
135
  export default defineConfig({
136
136
  plugins: [react(), babel({ presets: [reactCompilerPreset()] })],
package/dist/index.d.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import { Plugin } from "vite";
2
2
  import { ReactCompilerBabelPluginOptions, ReactCompilerOptions, RolldownBabelPreset } from "#optionalTypes";
3
- //#region src/reactCompilerPreset.d.ts
4
3
  export declare const reactCompilerPreset: (options?: ReactCompilerBabelPluginOptions) => RolldownBabelPreset;
5
- //#endregion
6
- //#region src/index.d.ts
7
4
  interface ReactCompilerPluginOptions extends ReactCompilerOptions {
8
5
  /**
9
6
  * Log recoverable React Compiler diagnostics through Vite.
@@ -27,6 +24,16 @@ export interface Options {
27
24
  * @default /\/node_modules\//
28
25
  */
29
26
  exclude?: string | RegExp | Array<string | RegExp>;
27
+ /**
28
+ * Can be used to skip fast refresh for certain files that won't run in the browser.
29
+ * @example options.include
30
+ */
31
+ refreshInclude?: string | RegExp | Array<string | RegExp>;
32
+ /**
33
+ * Can be used to skip fast refresh for certain files that won't run in the browser.
34
+ * @default options.exclude
35
+ */
36
+ refreshExclude?: string | RegExp | Array<string | RegExp>;
30
37
  /**
31
38
  * Control where the JSX factory is imported from.
32
39
  * https://oxc.rs/docs/guide/usage/transformer/jsx.html#import-source
@@ -37,7 +44,7 @@ export interface Options {
37
44
  * Note: Skipping React import with classic runtime is not supported from v4
38
45
  * @default "automatic"
39
46
  */
40
- jsxRuntime?: 'classic' | 'automatic';
47
+ jsxRuntime?: "classic" | "automatic";
41
48
  /**
42
49
  * React Fast Refresh runtime URL prefix.
43
50
  * Useful in a module federation context to enable HMR by specifying
@@ -55,9 +62,8 @@ export interface Options {
55
62
  compiler?: boolean | ReactCompilerPluginOptions;
56
63
  }
57
64
  declare function viteReact(opts?: Options): Plugin[];
58
- declare namespace viteReact {
59
- var preambleCode: string;
60
- }
65
+ declare const viteReactWithPreamble: typeof viteReact & {
66
+ preambleCode: string;
67
+ };
61
68
  declare function viteReactForCjs(this: unknown, options: Options): Plugin[];
62
- //#endregion
63
- export { type ReactCompilerPluginOptions as ReactCompilerOptions, viteReact as default, viteReactForCjs as "module.exports" };
69
+ export { type ReactCompilerPluginOptions as ReactCompilerOptions, viteReactWithPreamble as default, viteReactForCjs as "module.exports" };
package/dist/index.js CHANGED
@@ -2,8 +2,9 @@ import { readFileSync } from "node:fs";
2
2
  import { dirname, join } from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import { exactRegex, makeIdFiltersToMatchWithQuery } from "@rolldown/pluginutils";
5
+ import { createFilter } from "vite";
5
6
  import { reactRefreshWrapperPlugin } from "vite/internal";
6
- //#region ../common/refresh-utils.ts
7
+ // ../common/refresh-utils.ts
7
8
  const runtimePublicPath = "/@react-refresh";
8
9
  const preambleCode = `import { injectIntoGlobalHook } from "__BASE__${runtimePublicPath.slice(1)}";
9
10
  injectIntoGlobalHook(window);
@@ -31,16 +32,14 @@ function virtualPreamblePlugin({ name, isEnabled }) {
31
32
  }
32
33
  };
33
34
  }
34
- //#endregion
35
- //#region ../common/warning.ts
35
+ // ../common/warning.ts
36
36
  const silenceUseClientWarning = (userConfig) => ({ rollupOptions: { onwarn(warning, defaultHandler) {
37
37
  if (warning.code === "MODULE_LEVEL_DIRECTIVE" && (warning.message.includes("use client") || warning.message.includes("use server"))) return;
38
38
  if (warning.code === "SOURCEMAP_ERROR" && warning.message.includes("resolve original location") && warning.pos === 0) return;
39
39
  if (userConfig.build?.rollupOptions?.onwarn) userConfig.build.rollupOptions.onwarn(warning, defaultHandler);
40
40
  else defaultHandler(warning);
41
41
  } } });
42
- //#endregion
43
- //#region src/reactCompilerPreset.ts
42
+ // src/reactCompilerPreset.ts
44
43
  const defaultCodeFilter = /forwardRef|memo|\b(?:[A-Z]|use[A-Z0-9])/;
45
44
  const reactCompilerPreset = (options = {}) => ({
46
45
  preset: () => ({ plugins: [[fileURLToPath(import.meta.resolve("babel-plugin-react-compiler")), options]] }),
@@ -50,8 +49,7 @@ const reactCompilerPreset = (options = {}) => ({
50
49
  optimizeDeps: { include: options.target === "17" || options.target === "18" ? ["react-compiler-runtime"] : ["react/compiler-runtime"] }
51
50
  }
52
51
  });
53
- //#endregion
54
- //#region src/index.ts
52
+ // src/index.ts
55
53
  const _dirname = dirname(fileURLToPath(import.meta.url));
56
54
  const refreshRuntimePath = join(_dirname, "refresh-runtime.js");
57
55
  const defaultIncludeRE = /\.[tj]sx?$/;
@@ -59,6 +57,8 @@ const defaultExcludeRE = /\/node_modules\//;
59
57
  function viteReact(opts = {}) {
60
58
  const include = opts.include ?? defaultIncludeRE;
61
59
  const exclude = opts.exclude ?? defaultExcludeRE;
60
+ const jsxRefreshInclude = makeIdFiltersToMatchWithQuery(opts.refreshInclude ?? include);
61
+ const jsxRefreshExclude = makeIdFiltersToMatchWithQuery(opts.refreshExclude ?? exclude);
62
62
  const jsxImportSource = opts.jsxImportSource ?? "react";
63
63
  const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
64
64
  const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
@@ -79,8 +79,8 @@ function viteReact(opts = {}) {
79
79
  runtime: "classic",
80
80
  refresh
81
81
  },
82
- jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
83
- jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude)
82
+ jsxRefreshInclude,
83
+ jsxRefreshExclude
84
84
  } };
85
85
  else return {
86
86
  oxc: {
@@ -89,8 +89,8 @@ function viteReact(opts = {}) {
89
89
  importSource: opts.jsxImportSource,
90
90
  refresh
91
91
  },
92
- jsxRefreshInclude: makeIdFiltersToMatchWithQuery(include),
93
- jsxRefreshExclude: makeIdFiltersToMatchWithQuery(exclude)
92
+ jsxRefreshInclude,
93
+ jsxRefreshExclude
94
94
  },
95
95
  optimizeDeps: { rolldownOptions: { transform: { jsx: { runtime: "automatic" } } } }
96
96
  };
@@ -119,8 +119,8 @@ function viteReact(opts = {}) {
119
119
  if (env.config.consumer !== "client" || skipFastRefresh) return false;
120
120
  return reactRefreshWrapperPlugin({
121
121
  cwd: process.cwd(),
122
- include: makeIdFiltersToMatchWithQuery(include),
123
- exclude: makeIdFiltersToMatchWithQuery(exclude),
122
+ include: jsxRefreshInclude,
123
+ exclude: jsxRefreshExclude,
124
124
  jsxImportSource,
125
125
  reactRefreshHost: opts.reactRefreshHost ?? ""
126
126
  });
@@ -201,6 +201,7 @@ function createReactCompilerPlugin({ logDiagnostics, ...reactCompilerOptions },
201
201
  const jsxImportSource = reactOptions.jsxImportSource ?? "react";
202
202
  const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`;
203
203
  const jsxImportDevRuntime = `${jsxImportSource}/jsx-dev-runtime`;
204
+ const jsxRefreshFilter = reactOptions.refreshInclude || reactOptions.refreshExclude ? createFilter(reactOptions.refreshInclude, reactOptions.refreshExclude) : void 0;
204
205
  const loadCompiler = async (onError) => {
205
206
  if (compiler) return compiler;
206
207
  try {
@@ -228,7 +229,7 @@ function createReactCompilerPlugin({ logDiagnostics, ...reactCompilerOptions },
228
229
  const [filepath] = id.split("?");
229
230
  const isJSX = filepath.endsWith("x");
230
231
  const isClient = this.environment?.config.consumer !== "server";
231
- const refreshEnabled = isClient && isFastRefreshEnabled() && (isJSX || code.includes(jsxImportRuntime) || code.includes(jsxImportDevRuntime));
232
+ const refreshEnabled = isClient && isFastRefreshEnabled() && (jsxRefreshFilter && !jsxRefreshFilter(id) || !(isJSX || code.includes(jsxImportRuntime) || code.includes(jsxImportDevRuntime)));
232
233
  const shouldCompile = isClient && (reactCompilerOptions.compilationMode === "annotation" ? /['"]use memo['"]/.test(code) : defaultCodeFilter.test(code));
233
234
  const { transform } = compiler ?? await loadCompiler((message) => this.error(message));
234
235
  const result = await transform(id.split("?")[0], code, {
@@ -255,7 +256,7 @@ function createReactCompilerPlugin({ logDiagnostics, ...reactCompilerOptions },
255
256
  }
256
257
  };
257
258
  }
258
- viteReact.preambleCode = preambleCode;
259
+ const viteReactWithPreamble = Object.assign(viteReact, { preambleCode });
259
260
  function viteReactForCjs(options) {
260
261
  return viteReact.call(this, options);
261
262
  }
@@ -263,5 +264,4 @@ Object.assign(viteReactForCjs, {
263
264
  default: viteReactForCjs,
264
265
  reactCompilerPreset
265
266
  });
266
- //#endregion
267
- export { viteReact as default, viteReactForCjs as "module.exports", reactCompilerPreset };
267
+ export { viteReactWithPreamble as default, viteReactForCjs as "module.exports", reactCompilerPreset };
@@ -1,5 +1,5 @@
1
1
  /* global window */
2
- /* eslint-disable eqeqeq, prefer-const, @typescript-eslint/no-empty-function */
2
+ /* oxlint-disable eqeqeq, prefer-const, no-empty-function, no-unused-vars, typescript/no-implied-eval */
3
3
 
4
4
  /*! Copyright (c) Meta Platforms, Inc. and affiliates. **/
5
5
  /**
@@ -372,6 +372,7 @@ export function injectIntoGlobalHook(globalObject) {
372
372
  // Here, we just want to get a reference to scheduleRefresh.
373
373
  const oldInject = hook.inject
374
374
  hook.inject = function (injected) {
375
+ // oxlint-disable-next-line prefer-rest-params
375
376
  const id = oldInject.apply(this, arguments)
376
377
  if (
377
378
  typeof injected.scheduleRefresh === 'function' &&
@@ -408,6 +409,7 @@ export function injectIntoGlobalHook(globalObject) {
408
409
  rootElements.set(root, children)
409
410
  }
410
411
  }
412
+ // oxlint-disable-next-line prefer-rest-params
411
413
  return oldOnScheduleFiberRoot.apply(this, arguments)
412
414
  }
413
415
  hook.onCommitFiberRoot = function (id, root, maybePriorityLevel, didError) {
@@ -460,6 +462,7 @@ export function injectIntoGlobalHook(globalObject) {
460
462
  }
461
463
 
462
464
  // Always call the decorated DevTools hook.
465
+ // oxlint-disable-next-line prefer-rest-params
463
466
  return oldOnCommitFiberRoot.apply(this, arguments)
464
467
  }
465
468
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitejs/plugin-react",
3
- "version": "6.1.2",
3
+ "version": "6.2.0",
4
4
  "description": "The default Vite plugin for React projects",
5
5
  "keywords": [
6
6
  "fast refresh",
@@ -46,20 +46,20 @@
46
46
  "@rolldown/pluginutils": "^1.0.1"
47
47
  },
48
48
  "devDependencies": {
49
- "@rolldown/plugin-babel": "^0.2.4",
49
+ "@rolldown/plugin-babel": "^0.3.0",
50
50
  "@vitejs/react-common": "workspace:*",
51
51
  "babel-plugin-react-compiler": "^1.0.0",
52
- "oxc-transform-react": "^0.152.0",
53
- "react": "^19.3.0",
54
- "react-dom": "^19.3.0",
55
- "rolldown": "^1.2.12",
56
- "tsdown": "^0.23.0",
57
- "vite": "^8.3.2"
52
+ "oxc-transform-react": "^0.156.0",
53
+ "react": "^19.3.1",
54
+ "react-dom": "^19.3.1",
55
+ "rolldown": "^1.2.16",
56
+ "tsdown": "^0.23.4",
57
+ "vite": "^8.3.5"
58
58
  },
59
59
  "peerDependencies": {
60
- "@rolldown/plugin-babel": "^0.1.7 || ^0.2.0",
60
+ "@rolldown/plugin-babel": "^0.1.7 || ^0.2.0 || ^0.3.0",
61
61
  "babel-plugin-react-compiler": "^1.0.0",
62
- "oxc-transform-react": "^0.152.0",
62
+ "oxc-transform-react": ">=0.152.0 <0.157.0",
63
63
  "vite": "^8.0.0"
64
64
  },
65
65
  "peerDependenciesMeta": {
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/ban-ts-comment */
1
+ /* oxlint-disable @typescript-eslint/ban-ts-comment */
2
2
 
3
3
  // @ts-ignore --- `@rolldown/plugin-babel` is an optional peer dependency, so this may cause an error
4
4
  import type * as pluginBabel from '@rolldown/plugin-babel'