Skip to content

ejs-loader with webpack5 support. Chain it to html-loader and use it with html-webpack-plugin.

License

Notifications You must be signed in to change notification settings

dc7290/template-ejs-loader

Repository files navigation

template-ejs-loader

npm License: MIT

日本語

EJS (Embeded JavaScript) loader for Webpack. It converts EJS templates to plain HTML using the EJS npm package.

Features

  • webpack5 support
  • Import .js,.json and node modules using require
  • All files can be passed values.

Instalation

npm i -D template-ejs-loader

Usage

NOTE: You need to chain the template-ejs-loader with an html loader such as the html-loader and use a template plugin such as the html-webpack-plugin. To install these run npm i -D html-loader html-webpack-plugin.

Inside your webpack config file add the fallowing rules

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.ejs$/i,
        use: ['html-loader', 'template-ejs-loader'],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/ejs/index.ejs',
    }),
  ],
  // ...
}

Options

You can set the values supported by ejs. See here for the values.

The following are your own configuration options.

Name Type Default Description
data {Object} {}

data

Type: Object Default: {}

Use this if you want to pass the same value to all ejs files. If you want to pass individual values, see here.

Importing partials

<!-- plain import -->
<%- include('components/footer.ejs') %>

<!-- appending data -->
<%- include('components/header.ejs', { title: 'TOP' }) %>

Example:

index.ejs

<%- include('/components/header.ejs', { title: 'TOP' }) %>

<%- include('/components/footer.ejs') %>

header.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%= title %></title>
  </head>

  <body>

footer.ejs

</body>
</html>

Note: Include preprocessor directives (<% include user/show %>) are not supported in ejs v3.0+.

Importing JavaScript or JSON files

index.ejs

<!DOCTYPE html>
<html lang="en">
  <head>
    <% const meta = require('../data/index-meta.js') %>
    <%- include('components/header.ejs', meta) %>
  </head>
  <!-- ... -->
</html>

index-meta.js

module.exports = {
  title: 'Webpack Starter App',
  author: 'John Doe',
  keywords: ['lorem', 'ipsum', 'dolor', 'sit', 'amet'],
  description: 'Lorem ipsum dolor sit, amet consectetur adipisicing elit.',
  customFunction: function () {
    // ...
  },
}

Importing node modules

index.ejs

<!DOCTYPE html>
<html lang="en">
  <!-- ... -->

  <div>
    <% const _ = require('lodash') %>
    <%= _.join(['a','b','c'], '~') %>
  </div>

  <!-- ... -->
</html>

Passing individual values

If you are getting all your htmlWebpackPlugin instances generated within a loop, and you want to get indivisual passing values for each .ejs template as variables, you can try this. (This method is using webapck loader inline mechanic to load every ejs file instead, you can also set html-loader/template-ejs-Loader options for each .ejs file.)

Unfortunaly, because webapck loader inline does not support loader option in which type is function, so basicly the option preprocessor of html-loader is NOT supported here, better try another way if you need to do interpolate things, for example: using templateEjsLoaderOption.data to set individual inject value.

webpack.config.js

const { htmlWebpackPluginTemplateCustomizer }  = require('template-ejs-loader')
...
module.exports = {
  ...

  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: htmlWebpackPluginTemplateCustomizer({

        templatePath:'./src/index.ejs' // ejs template path 

        htmlLoaderOption:{
          // you can set individual html-loader option here.
          // but preprocessor option is not supported.
        },
        templateEjsLoaderOption:{ // set individual template-ejs-loader option here
          root:'', // this is for example, if not needed, just feel free to delete.
          data:{ // example, too.
            foo:'test' // btw, you can have indivisual data injection for each .ejs file using data option
          }
        }
      }),
    }),
  ]

  ...
}

More info

For more info on how to use EJS visit their npm package page or their official website

About

ejs-loader with webpack5 support. Chain it to html-loader and use it with html-webpack-plugin.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •