Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
ejs-loader
Advanced tools
The ejs-loader npm package is a loader for webpack that allows you to compile EJS (Embedded JavaScript) templates into JavaScript functions. This is particularly useful for integrating EJS templates into a webpack-based build process, enabling you to use EJS templates in your client-side code.
Basic EJS Template Loading
This feature allows you to load and compile an EJS template using webpack. The compiled template can then be used as a function that takes a data object and returns the rendered HTML.
const template = require('ejs-loader!./template.ejs');
const html = template({ name: 'World' });
console.log(html);
Custom EJS Options
This feature allows you to pass custom options to the EJS compiler via query parameters in the require statement. In this example, the 'variable' option is set to 'data'.
const template = require('ejs-loader?variable=data!./template.ejs');
const html = template({ name: 'World' });
console.log(html);
Using EJS with Webpack
This feature demonstrates how to configure webpack to use ejs-loader for all .ejs files. This setup allows you to import EJS templates in your JavaScript files without specifying the loader each time.
module.exports = {
module: {
rules: [
{
test: /\.ejs$/,
use: 'ejs-loader'
}
]
}
};
The html-loader package is used to export HTML as a string. It can also handle loading images, scripts, and styles within the HTML. Unlike ejs-loader, it does not support EJS syntax but is useful for handling plain HTML files.
The pug-loader package is a loader for webpack that compiles Pug templates into JavaScript functions. It is similar to ejs-loader but uses Pug (formerly Jade) syntax instead of EJS. Pug offers a more concise syntax compared to EJS.
The handlebars-loader package is a webpack loader for compiling Handlebars templates. It is similar to ejs-loader but uses Handlebars syntax, which includes features like block helpers and partials. Handlebars is known for its simplicity and logic-less templates.
EJS (Underscore/LoDash Templates) loader for webpack. Uses lodash template function to compile templates.
If you are looking for the loader which uses EJS templating engine, there is ejs-compiled-loader
npm install ejs-loader
var template = require("ejs!./file.ejs");
// => returns the template function compiled with undesrcore (lodash) templating engine.
// And then use it somewhere in your code
template(data) // Pass object with data
You also should provide a global _
variable with the lodash/underscore runtime. You can do it with the following webpack plugin: https://github.com/webpack/docs/wiki/list-of-plugins#provideplugin
plugins: [
new webpack.ProvidePlugin({
_: "underscore"
})
]
Underscore/Lodash options can be passed in using the querystring or adding an esjLoader
options block to your configuration.
Config example with Webpack 4+
module.exports = {
module: {
rules: [
{
test: /\.ejs$/,
loader: 'ejs-loader',
options: {
variable: 'data',
interpolate : '\\{\\{(.+?)\\}\\}',
evaluate : '\\[\\[(.+?)\\]\\]'
}
}
]
}
};
Config example using a querystring (deprecated):
module.exports = {
module: {
loaders: [
{ test: /\.ejs$/, loader: 'ejs-loader?variable=data' },
]
}
};
is equivalent to
var template = _.template('<%= template %>', { variable : 'data' });
module.exports = {
module: {
loaders: [
{
test: /\.ejs$/,
loader: 'ejs-loader',
query: {
variable: 'data',
interpolate : '\\{\\{(.+?)\\}\\}',
evaluate : '\\[\\[(.+?)\\]\\]'
}
},
]
}
};
is equivalent to
var template = _.template('<%= template %>', { variable: 'data', interpolate : '\\{\\{(.+?)\\}\\}', evaluate : '\\[\\[(.+?)\\]\\]' });
Config example using the ejsLoader
config block (deprecated):
module.exports = {
module: {
loaders: [
{ test: /\.ejs$/, loader: 'ejs-loader' }
]
},
ejsLoader : {
variable : 'data',
interpolate : /\{\{(.+?)\}\}/g,
evaluate : /\[\[(.+?)\]\]/g
}
};
By default, ejs-loader
generates JS modules that use the ES modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.
You can enable a CommonJS module syntax using:
Config example with Webpack 4+
module.exports = {
module: {
rules: [
{
test: /\.ejs$/,
loader: 'ejs-loader',
options: {
esModule: false
}
}
]
}
};
The variable option is required
to compile EJS templates into ES compatible modules. If the variable
option is not provided as a loader or query
option, an Error
will be thrown. Please see https://github.com/lodash/lodash/issues/3709#issuecomment-375898111 for additional details.
Lodash template function does not provide include
method of ejs module. To include other templates, passing template functions as parameters does the job. For example:
index.js:
var mainTemplate = require('ejs!./main.ejs');
var hyperlinkTemplate = require('ejs!./hyperlink.ejs');
var renderedHtml = mainTemplate({ hyperlink: hyperlinkTemplate });
main.ejs:
<h1><%= hyperlink({ name: 'Example', url: 'http://example.com' }) %></h1>
hyperlink.ejs:
<a href="<%= url %>"><%= name %></a>
As a result, renderedHtml
becomes a string <h1><a href="http://example.com">Example</a></h1>
.
exportAsESM
flag to esModule
and enabled this behavior by default to be consistent with other webpack loaders.exportAsESM
flagejsLoader
or via loader's query
FAQs
EJS (Underscore/LoDash Templates) loader for webpack
The npm package ejs-loader receives a total of 118,616 weekly downloads. As such, ejs-loader popularity was classified as popular.
We found that ejs-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.