Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
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
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.