npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

template-ejs-loader

v0.9.4

Published

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

Downloads

6,693

Readme

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