rollup-plugin-emit-ejs

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

Rollup plugin to emit files from EJS templates into the bundle output. Version 3.1.0 (stable, last updated 2021). This plugin uses Rollup's emitFile() method, allowing other plugins to process emitted files (e.g., minify HTML). It provides a basic layout system, automatic JavaScript and stylesheet linking via helper variables (javascripts, stylesheets, content), and supports any file type, not just HTML. Key differentiator: unlike rollup-plugin-bundle-html, it enables post-processing of emitted assets. Requires rollup ^2.10.0 as peer dependency. Ships TypeScript definitions.

error Error: Could not resolve 'ejs'
cause Missing runtime dependency 'ejs' in project.
fix
Install ejs: npm install ejs
error Error: [plugin:emit-ejs] EJS cannot find module: 'file.ejs'
cause Template path specified in options does not exist or is outside the src directory.
fix
Ensure the template file exists and is relative to the src directory (e.g., if src='templates', use layout='templates/layout.html.ejs').
error TypeError: emitEJS is not a function
cause Wrong import method; using CommonJS require with a default export.
fix
Use ESM import: import emitEJS from 'rollup-plugin-emit-ejs' or use dynamic import: const emitEJS = await import('rollup-plugin-emit-ejs').then(m => m.default).
gotcha Layout template path is automatically excluded from file emission, but duplicate exclusion may cause unexpected behavior.
fix Do not list layout template in exclude option; it is excluded automatically.
gotcha The helper variables (javascripts, stylesheets) are only populated if Rollup outputs chunks (e.g., code-split). In legacy mode or single-bundle output, they may be empty.
fix Ensure output format supports multiple chunks (e.g., 'es', 'cjs') and that code splitting is enabled.
gotcha The extension option can cause conflicts if specified alongside templates that already have a custom extension (e.g., index.html.ejs).
fix Either use .ejs only and rely on extension option, or use explicit extensions in filename and omit extension option.
deprecated No deprecation notices in current version. Plugin is stable but may not receive updates for newer Rollup versions (>3.0).
fix Monitor compatibility with upcoming Rollup releases; consider alternatives if Rollup 4+ is needed.
npm install rollup-plugin-emit-ejs
yarn add rollup-plugin-emit-ejs
pnpm add rollup-plugin-emit-ejs

Basic usage: emit an HTML file from EJS template with layout and data. Shows plugin config in rollup.config.js.

// rollup.config.js
import emitEJS from 'rollup-plugin-emit-ejs';

export default {
  input: 'src/index.js',
  output: { dir: 'dist', format: 'es' },
  plugins: [
    emitEJS({
      src: 'src',
      layout: 'src/layout.html.ejs',
      data: {
        title: 'My App',
        body: 'Content goes here'
      }
    })
  ]
};