This repository has been archived on 2026-01-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
babel-plugin-nunjucks/README.md
Valentin Silytuin 1f153076fe v3.0.0
2022-12-15 16:35:15 +06:00

71 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# babel-plugin-nunjucks
Плагин для [babel-loader](https://github.com/babel/babel-loader), позволяющий использовать шаблонизатор [Nunjucks](https://mozilla.github.io/nunjucks/) внутри JS-файлов.
## Установка
```bash
yarn add -D @advdominion/babel-plugin-nunjucks
```
## Использование
Используются [Tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) с тегом `njk`:
```js
njk`
{% from "./item.njk" import item as item %}
<div class="items">
{% for n in range(0, 10) %}
{{item()}}
{% endfor %}
</div>
`;
```
Пример конфигурации Webpack:
```js
[
{
test: /\.js$/,
exclude: [/mocks\.js$/],
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
},
},
],
},
{
test: /mocks\.js$/,
use: [
{
loader: 'babel-loader',
options: {
compact: false,
plugins: [
[
'@advdominion/babel-plugin-nunjucks',
{
templatesFolder: 'src/templates/',
},
],
],
},
},
],
},
];
```
## Опции
- `templatesFolder` - строка, обязательный параметр. Путь до папки с файлами шаблонов
- `globals` - массив, необязательный параметр. Глобальные переменные для Nunjucks (например, `[{name: "message", value: "Hello, world!"}]`)
**Важно:** Параметр `cacheDirectory` в опциях babel-loader должен быть отключён.