iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐸

Injecting Data into EJS

に公開

Environment

  • ejs-easy-loader: "^0.1.4",
  • html-webpack-plugin: "^4.5.0",
  • webpack: "^4.44.2",

Injection Methods

Individual Files

// src/config.js
module.exports = {
  hoge: 'ほげ',
};
// webpack.config.js
const config = require('./src/config');

new HtmlWebpackPlugin({
  data: config.data,  // ←
});
<!-- ejs -->
<%= htmlWebpackPlugin.options.data.hoge %>

By passing data with an arbitrary key/value pair to the HtmlWebpackPlugin instance, you can access it from EJS via htmlWebpackPlugin.options.xxx.

Global

<% 
  const page = require('./page.config.js'); 
%>

As an aside

With ejs-html-loader, data can be injected via options, but it cannot be passed as data for individual pages like with ejs-easy-loader.

https://www.npmjs.com/package/ejs-html-loader#usage

Discussion