nunjucks-loader
nunjucks-loader copied to clipboard
Relative paths are not being resolved
Template paths are stored in the shared nunjucks.webpackDependencies
object as is, without being resolved, which may lead to the corresponding template references being overwritten as next templates are being pre-compiled. As a result, if multiple templates use the same string literal as a relative path in include
, import
, or extend
tags, they all will end up referencing the same template after compilation (which one it will be depends on the order they are imported in the JS file).
Consider the following example:
The same directory structure as in this repo, with include.njk
files added in the hero
and villain
dirs:
test/fixtures/templates/
├── ...
├── hero
| ├── default.njk
│ └── include.njk
├── ...
└── villain
├── default.njk
└── include.njk
Contents of these include.njk
tempates:
{% include './default.njk' %}
(e.g. hero/default.njk
and villain/default.njk
respectively are meant to be included)
The expected results of rendering hero/include.njk
and villain/include.njk
would be I am a hero.
and I am a villain.
respectively, but in fact they are the same (either I am a hero.
or I am a villain.
, depending on which template is imported first).
Nunjucks seems not intend to resolve relative paths mozilla/nunjucks#1214
Weird, there is a way to configure jinja to work with relative paths, as shown in this SO answer. I wonder if there's a nunjucks equivalent.