rollup-plugin-invariant

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

Rollup plugin that strips invariant(condition, message) calls in production builds. v0.9.2 (stable, infrequent releases). It transforms invariant calls into empty statements when process.env.NODE_ENV is 'production', removing both the condition evaluation and message string. Unlike general dead-code elimination, this plugin specifically targets the invariant pattern without relying on Terser or manual tree-shaking, ensuring zero runtime overhead in production. Works with rollup >=1.0 and ships TypeScript types.

error Error: Unknown plugin options: include, exclude
cause Using options 'include' and 'exclude' as top-level plugin options instead of nested under 'invariantPlugin'.
fix
Options are passed as object to the plugin function: invariantPlugin({ include: ... })
gotcha Plugin only works with the 'invariant' package from npm; custom invariant functions are not stripped.
fix Ensure you import invariant from 'invariant' (or '@apollo/query-planner' which re-exports it).
gotcha The plugin does not support CommonJS output (or only transforms ES modules?). Check compatibility with your output format.
fix Prefer ES module output or test with CJS. Consider using @rollup/plugin-commonjs to convert dependencies.
npm install rollup-plugin-invariant
yarn add rollup-plugin-invariant
pnpm add rollup-plugin-invariant

Configures rollup-plugin-invariant in a rollup config, showing include/exclude options and production flag override.

// rollup.config.js
import invariantPlugin from 'rollup-plugin-invariant';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'esm'
  },
  plugins: [
    invariantPlugin({
      // Default: include all files
      include: ['**/*.js'],
      // Optionally exclude node_modules
      exclude: ['node_modules/**'],
      // Force production mode (default: process.env.NODE_ENV === 'production')
      production: true
    })
  ]
};