rollup-plugin-htaccess

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

A Rollup/Rolldown/Vite plugin that generates .htaccess files during the build process. Current stable version is 0.13.1. Supports Apache directives with type-safe configuration via TypeScript types. The plugin integrates with Rollup (v3.29.4+), Rolldown (v1.0.0-rc7+), and Vite (v4.5.14+). Key differentiators: no runtime dependencies, comprehensive directive support, and native ESM/CJS compatibility. Requires Node >= 20. Released on npm with monthly updates.

error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/rollup-plugin-htaccess/dist/index.js from /path/to/project/rollup.config.js not supported.
cause Using CommonJS require() to load an ESM-only package.
fix
Change your rollup config to use ESM (.mjs extension or "type": "module" in package.json) and use import htaccess from 'rollup-plugin-htaccess'.
error TypeError: htaccess is not a function
cause Importing the plugin incorrectly (e.g., using named import instead of default).
fix
Use default import: import htaccess from 'rollup-plugin-htaccess'.
error Error: Cannot find module 'rollup-plugin-htaccess'
cause Plugin not installed or missing from devDependencies.
fix
Run npm install --save-dev rollup-plugin-htaccess to install the package.
breaking Plugin requires Node >= 20; older Node versions will fail to run.
fix Upgrade Node.js to version 20 or higher.
breaking Breaking change in v0.13: option `enableHTTPS` renamed to `forceHTTPS`.
fix Replace `enableHTTPS: true` with `forceHTTPS: true` in your plugin config.
deprecated The `cache` option is deprecated and will be removed in v1.0.
fix Use the `performance` object with `setExpires` and `setCacheControl` instead.
gotcha Plugin is ESM-only; require() will not work without using dynamic import.
fix Use `import htaccess from 'rollup-plugin-htaccess'` or `const htaccess = (await import('rollup-plugin-htaccess')).default`.
gotcha When using with Vite, the plugin must be placed in the `plugins` array of your vitest config, not in the vite config itself.
fix Add plugin to `plugins` in the `test` section of your vitest config.
npm install rollup-plugin-htaccess
yarn add rollup-plugin-htaccess
pnpm add rollup-plugin-htaccess

Example Rollup configuration using htaccess plugin to generate .htaccess with redirects, security, and caching rules.

import htaccess from 'rollup-plugin-htaccess';

export default {
  input: 'src/index.js',
  output: { dir: 'dist', format: 'es' },
  plugins: [
    htaccess({
      redirects: [
        { from: '/old-path', to: '/new-path', type: 301 },
        { from: '/temporary', to: '/elsewhere', type: 302 }
      ],
      security: {
        disableDirectoryBrowsing: true,
        blockAccessToHiddenFiles: true
      },
      performance: {
        enableCompression: true,
        setExpires: { 'image/png': '1 month', 'text/css': '1 week' }
      }
    })
  ]
};