rollup-plugin-preact

raw JSON →
0.5.2 verified Mon Apr 27 auth: no javascript

Rollup plugin that automatically resolves and aliases React imports to Preact/Preact Compat in Rollup bundles. Current stable version 0.5.2. Configurable options include usePreactX for Preact X, noPropTypes, noReactIs, noEnv flags to remove unnecessary compat code, and custom aliasModules. It simplifies migrating from React to Preact by handling module resolution and compat aliasing.

error Error: Cannot find module 'rollup-plugin-preact'
cause Package not installed or not in node_modules.
fix
Run 'npm install rollup-plugin-preact' or 'yarn add rollup-plugin-preact'.
error TypeError: preact is not a function
cause Importing incorrectly (e.g., preact.default()).
fix
Use 'import preact from "rollup-plugin-preact"' or 'const preact = require("rollup-plugin-preact")'.
error Error: No version of preact/compat found in node_modules
cause Preact Compat not installed or alias not configured correctly.
fix
Install preact-compat or set alias properly, e.g., 'aliasModules: { "react": "preact/compat" }'.
breaking Version 0.5.x drops support for Node < 10; requires Rollup >= 1.0
fix Upgrade Node to >=10 and Rollup to >=1.0.
deprecated Option 'usePreactX' is deprecated in favor of using 'resolvePreactCompat: true' with Preact X+.
fix Replace 'usePreactX: true' with 'resolvePreactCompat: true'.
gotcha Plugin does not automatically install Preact; you must install 'preact' and optionally 'preact/compat'.
fix Run 'npm install preact' and use 'aliasModules: { "react": "preact/compat" }'.
gotcha When using 'noPropTypes', ensure your code does not rely on PropTypes (runtime removal may cause errors).
fix Remove PropTypes imports or set 'noPropTypes: false' if needed.
npm install rollup-plugin-preact
yarn add rollup-plugin-preact
pnpm add rollup-plugin-preact

Configure Rollup to replace React imports with Preact/Preact Compat and remove unnecessary compat code.

// rollup.config.js
import preact from 'rollup-plugin-preact';

export default {
  input: 'src/main.js',
  output: {
    file: 'bundle.js',
    format: 'iife',
  },
  plugins: [
    preact({
      usePreactX: true,
      noPropTypes: true,
      noReactIs: true,
      noEnv: true,
      resolvePreactCompat: true,
      aliasModules: {
        'react-dom': 'preact/compat',
      },
    }),
  ],
};