Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
simple-nunjucks-loader
Advanced tools
This Webpack loader compiles Nunjucks templates.
html-webpack-plugin
compatible.
npm install --save-dev simple-nunjucks-loader
window.nunjucksPrecompiled
Loader didn't expose window.nunjucksPrecompiled
. If your code relied on
this object, it will definitely break. Use imports of required template
or adopt expose-loader
to your build pipeline.
This loader will precompile Nunjucks templates. It also includes Nunjunks (slim) runtime for browser.
Add loader to your webpack
config as follows:
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.njk$/,
use: [
{
loader: 'simple-nunjucks-loader',
options: {}
}
]
}
]
}
};
html-webpack-plugin
For using with html-webpack-plugin
just add it to plugins
array, all options
from it would be available as htmlWebpackPlugin.options
in Nunjucks template.
webpack.config.js
const HTMLWebpackPlugin = require('html-webpack-plugin');
module.exports = {
module: {
rules: [
{
test: /\.njk$/,
use: [
{
loader: 'simple-nunjucks-loader',
options: {}
}
]
}
]
},
plugins: [
new HTMLWebpackPlugin({
template: 'src/page.njk'
})
]
};
Refer to html-webpack-plugin
page
for all available options.
import
/require
To use it from your modules just import it as any other dependency and pass context for render:
import template from './template.njk';
const page = template({
foo: 'bar'
});
By default Nunjunks bundle all precompiled templates to
window.nunjucksPrecompiled
, then loads them via custom loader from this
global object. If precompiled template reference some other template file,
it is loaded from disk (in NodeJS environment), or fetched via XMLHttpRequest
from internet.
Both are not webpack-way for projects bundling.
This loader workaround this behaviour by precompiling templates and dependant
templates as separate bundle chunks. It also use custom wrapper for precompiled
code to avoid creating window.nunjucksPrecompiled
.
It also adds each found template as dependency for template that need it, so bundle get rebuild in watch mode only when required.
Loader supports limited number of Nunjuncks options.
It's doesn't support watch
(we use webpack
own dependencies watch),
noCache
, web
settings and express
.
All other options get passed to Nunjunks Environment
during files loading.
Name | Type | Default | Description |
---|---|---|---|
searchPaths | {String} or {Array.<string>} | . | One or more paths to resolve templates paths |
globals | Object.<string, string> | {} | Map global function to corresponding module |
extensions | Object.<string, string> | {} | Map extension to corresponding module |
autoescape | {Boolean} | true | See Nunjuncks options for description of options below |
throwOnUndefined | {Boolean} | false | |
trimBlocks | {Boolean} | false | |
lstripBlocks | {Boolean} | false | |
tags | {Object.<string, string>} | {blockStart: '{%', blockEnd: '%}', variableStart: '{{', variableEnd: '}}', commentStart: '{#', commentEnd: '#}'} | Override tags syntax |
Loader is searching for full template relative to given string(s) from
searchPath
option (or project root, if no paths given).
Path to file couldn't be outside of folders above.
Set global function and import, that should return function to use.
{
globals: {
_: 'lodash',
globalEnv: path.join(__dirname, 'app/global-env.js')
}
}
Set map of extensions that would be imported before each template render.
{
extensions: {
CustomExtension: path.join(__dirname, 'lib/extensions/custom-extension.js')
}
}
Module here (lib/extensions/custom-extension.js
) should return extension
instance.
Loader trying to guess which extensions are really used, and keep only required imports.
FAQs
Webpack loader for Nunjucks
The npm package simple-nunjucks-loader receives a total of 2,612 weekly downloads. As such, simple-nunjucks-loader popularity was classified as popular.
We found that simple-nunjucks-loader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.