Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including rollup-plugin-emit-ejs with all npm packages installed. Try it out:

require("rollup/package.json"); // rollup is a peer dependency. var rollupPluginEmitEjs = require("rollup-plugin-emit-ejs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

rollup-plugin-emit-ejs v3.1.0

Emit files from ejs with rollup

rollup-plugin-emit-ejs

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.

Install

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

or

npm install rollup-plugin-emit-ejs -D

Usage

// rollup.config.js

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

export default {
  // ...
  plugins: [
    emitEJS({
      src: 'src',
      layout: 'src/layout.html.ejs',
      data: {
        title: 'Hey',
        body: 'Hey Hey Hey'
      }
    })
  ]
}
<!-- src/layout.html.ejs !-->

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

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

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

Options

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

src

Source directory to find ejs templates from.

Required

dest

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

Default: '.'

include

Glob or array of globs defining which templates to include.

Default: '**/*.ejs'

exclude

Glob or array of globs defining which templates to exclude.

Default: []

Note that the template provided in the layout option is automatically excluded.

layout

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

Default: undefined

extension

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

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

Options to pass to ejs. (See ejs options)

Default: {}

Note that the filename options is forced by the plugin.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free