Emit files from ejs with rollup

Downloads in past


3.1.04 years ago4 years agoMinified + gzip package size for rollup-plugin-emit-ejs in KB


test build version
This plugin allows you to emit files from ejs templates to rollup bundle.
It is primarily meant to emit html files since it helps you to link bundled javascripts/stylesheets and includes a basic layout system, but it can deal with any file type.
Unlike rollup-plugin-bundle-html, this plugin uses the emitFile() plugin context method which allow other plugins like rollup-plugin-html-minifier to process the emitted files.


yarn add rollup-plugin-emit-ejs --dev

npm install rollup-plugin-emit-ejs -D


// rollup.config.js

import emitEJS from 'rollup-plugin-emit-ejs'

export default {
  // ...
  plugins: [
      src: 'src',
      layout: 'src/layout.html.ejs',
      data: {
        title: 'Hey',
        body: 'Hey Hey Hey'

<!-- src/layout.html.ejs !-->

<!doctype html>
    <meta charset="utf-8">
    <title><%= title %></title>
    <% stylesheets.forEach(stylesheet => { %>
      <link rel="stylesheet" href="<%= stylesheet %>">
    <% }) %>
    <%- content %>
    <% javascripts.forEach(javascript => { %>
      <script src="<%= javascript %>"></script>
    <% }) %>

<!-- src/index.html.ejs !-->

  <p><%= body %></p>

This will emit a file named index.html next to the javascript bundle file.


  src: string
  dest?: string
  include?: string | string[]
  exclude?: string | string[]
  layout?: string
  extension?: string
  data?: Data
  options?: Options


Source directory to find ejs templates from.


Relative path from bundle location where to output files from ejs templates.
Default: '.'


Glob or array of globs defining which templates to include.
Default: '**/*.ejs'


Glob or array of globs defining which templates to exclude.
Default: []
Note that the template provided in the layout option is automatically excluded.


Path to an ejs template to use as layout. Skip this option if you don't need layout.
Default: undefined


Extension to append to emitted files (the leading . can be omitted).
Default: undefined
Note that the trailing .ejs extension is automatically removed from template filenames. So this option is useful if you only want to use the .ejs extension in your template filenames, but need to replace it with another extension like .html. Otherwise, you can just stack the output extension directly in the filename (index.html.ejs) and skip this option.


Data to pass to ejs.
Default: {}
The following helper variables are forced by the plugin and available in all templates:
  • javascripts: array of relative paths to javascripts
  • stylesheets: array of relative paths to stylesheets

In the layout, an extra content variable is passed containing the content to wrap into the layout. This variable needs to be printed unescaped if you want to use it as html, use the corresponding ejs tag: <%- (See ejs tags)


Options to pass to ejs. (See ejs options)
Default: {}
Note that the filename options is forced by the plugin.