React Refresh Transformer for TypeScript

2.0.12 · maintenance · verified Tue Apr 21

react-refresh-typescript is a TypeScript transformer that integrates React Fast Refresh (Hot Module Replacement) into React applications. It specifically handles the necessary transformations for TypeScript codebases to enable live editing of React components without losing their state. The package is currently under on-demand maintenance, meaning updates typically occur when issues are opened to track upstream changes in `react-refresh`. It currently matches `react-refresh@0.19.*` (React 19). Its primary audience includes bundler plugin developers, such as those building integrations for webpack, Rollup, or esbuild. Key differentiators include its direct integration as a TypeScript custom transformer, which allows it to operate early in the compilation pipeline, and its support for Deno environments. This package is essential for enabling a smooth developer experience with hot reloading in TypeScript-based React projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use react-refresh-typescript with TypeScript's `transpileModule` API to transform React JSX code for Fast Refresh.

import refresh from 'react-refresh-typescript';
import ts from 'typescript';

const sourceCode = `
const App = () => {
    const [count, setCount] = React.useState(0);
    return (
        <div>
            <h1>Hello Fast Refresh!</h1>
            <p>Count: {count}</p>
            <button onClick={() => setCount(c => c + 1)}>Increment</button>
        </div>
    );
};
export default App;
`;

const { outputText } = ts.transpileModule(sourceCode, {
    compilerOptions: {
        target: ts.ScriptTarget.ESNext,
        jsx: ts.JsxEmit.Preserve,
        module: ts.ModuleKind.ESNext, // Required for Fast Refresh
    },
    fileName: 'app.tsx',
    transformers: { before: [refresh()] },
});

console.log(outputText);

view raw JSON →