Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vite-plugin-swc-react-refresh

ArnaudBarre1.4kMIT2.2.1TypeScript support: included

Use the versatility of SWC for development and the maturity of esbuild for production

vite, vite-plugin, react, swc, react-refresh, fast refresh

readme

vite-plugin-swc-react-refresh npm

Use the versatility of SWC for development and the maturity of esbuild for production.

Installation

npm i -D vite-plugin-swc-react-refresh

Usage

With the automatic JSX runtime (requires esbuild => 0.14.51):

import { defineConfig } from "vite";
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";

export default defineConfig({
  plugins: [swcReactRefresh()],
  esbuild: { jsx: "automatic" },
});

Without the automatic JSX runtime, but still omitting the React default import:

import { defineConfig } from "vite";
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";

export default defineConfig({
  plugins: [swcReactRefresh()],
  esbuild: { jsxInject: `import React from "react"` },
});

Consistent components exports

For React refresh to work, your file should only export React components. The best explanation I've read is the one from the Gatsby docs.

However, having some components being hot updated and not others is a very frustrating experience as a developer, and I think this rule should be enforced by a linter, so I made an eslint rule to go along with this plugin.

This plugin expects this rule to be respected and will always fast refresh components. If a file export something that is not a React component (TS types are ok), update to this export would not propagate and require a manual reload.

changelog

Changelog

2.2.1

Skip react-refresh on SSR (Fixes #2)

2.2.0

  • Always provide parser options to fix issue with .jsx imports. Relying on file extension for this is more buggy than I though
  • Extract line and column in SWC errors to make overlay filename clickable
  • Fix plugin name (react-refresh -> swc-react-refresh)

2.1.0

Add source maps support

2.0.3

Include react/jsx-dev-runtime for dependencies optimisation when using automatic runtime.

2.0.2

Unpinned @swc/core to get new features (like TS instantiation expression) despite a 30mb bump of bundle size

2.0.1

Fix esbuild property in documentation.

2.0.0

Breaking: Use named export instead of default export for better esm/cjs interop.

To migrate, replace your import by import { swcReactRefresh } from "vite-plugin-swc-react-refresh";

The JSX automatic runtime is also now supported if you bump esbuild to at least 0.14.51.

To use it, update your config from esbuild: { jsxInject: 'import React from "react"' }, to esbuild: { jsx: "automatic" },

0.1.2

  • Add vite as peer dependency
  • Pin @swc/core version to 1.2.141 to avoid a 30mb bump of bundle size

0.1.1

Add LICENSE

0.1.0

Initial release