Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative paths not being resolved #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ module.exports = function (source) {
hasRun = true;
}

var name = slash(path.relative(root || this.rootContext || this.options.context, this.resourcePath));
var resourcePathRel = path.relative(root || this.rootContext || this.options.context, this.resourcePath);
var name = slash(resourcePathRel);
var resourceDirRel = path.parse(resourcePathRel).dir;

var nunjucksCompiledStr = nunjucks.precompileString(source, {
env: env,
Expand Down Expand Up @@ -117,22 +119,27 @@ module.exports = function (source) {
// when this loader compiles multiple templates.
compiledTemplate += 'var dependencies = nunjucks.webpackDependencies || (nunjucks.webpackDependencies = {});\n';

var templateReg = /env\.getTemplate\(\"(.*?)\"/g;
var match;
var templateReg = /(?<=env\.getTemplate\(")(.+?)(?=")/g; // Node >= 8
var isRelativeReg = /^\.\.?\//;

// Create an object to store references to the dependencies that have been included - this ensures that a template
// dependency is only written once per file, even if it is used multiple times.
var required = {};

// Iterate over the template dependencies
while (match = templateReg.exec(nunjucksCompiledStr)) {
var templateRef = match[1];
if (!required[templateRef]) {
// Require the dependency by name, so it gets bundled by webpack
compiledTemplate += 'dependencies["' + templateRef + '"] = require( "' + templateRef + '" );\n';
required[templateRef] = true;
nunjucksCompiledStr = nunjucksCompiledStr.replace(templateReg, function(templateRef) {
eventlistener marked this conversation as resolved.
Show resolved Hide resolved
var templateRefRel = templateRef;
if (isRelativeReg.test(templateRef)) {
templateRefRel = slash(path.join(resourceDirRel, templateRef));
};
if (!required[templateRefRel]) {
// Require the dependency by path, so it gets bundled by webpack
compiledTemplate += 'dependencies["' + templateRefRel + '"] = require( "' + templateRef + '" );\n';
required[templateRefRel] = true;
}
}

return templateRefRel;
});



Expand All @@ -148,7 +155,7 @@ module.exports = function (source) {
compiledTemplate += '\n\n';

// export the shimmed module
compiledTemplate += 'module.exports = shim(nunjucks, env, nunjucks.nunjucksPrecompiled["' + name + '"] , dependencies)';
compiledTemplate += 'module.exports = shim(nunjucks, env, nunjucks.nunjucksPrecompiled["' + name + '"] , dependencies);\n';

return compiledTemplate;
};
1 change: 1 addition & 0 deletions test/fixtures/templates/hero/include.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include './default.njk' %}
1 change: 1 addition & 0 deletions test/fixtures/templates/villain/include.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include './default.njk' %}
6 changes: 6 additions & 0 deletions test/spec/includes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var includeBasic = require('include-basic.njk');
var includeWithinBlock = require('include-within-block.njk');
var includeWithinMacro = require('include-within-macro.njk');
var includeRelative1 = require('hero/include.njk');
var includeRelative2 = require('villain/include.njk');

describe('includes', function () {

Expand All @@ -21,4 +23,8 @@ describe('includes', function () {
result.should.include('Hello');
})

it('should correctly resolve relative paths', function() {
includeRelative1.render().trim().should.equal('I am a hero.');
includeRelative2.render().trim().should.equal('I am a villain.');
});
});
11 changes: 9 additions & 2 deletions test/spec/nunjucks-loader.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
var nunjucks = require("nunjucks/browser/nunjucks-slim");

var templateOne = require('hero/default.njk');
var templateTwo = require('villain/default.njk');
var templateOne = require('hero/include.njk');
var templateTwo = require('villain/include.njk');

describe('nunjucks-loader', function () {
describe('when requiring templates with the same base name', function() {
it('should have unique entries in nunjucksPrecompiled', function() {
nunjucks.nunjucksPrecompiled.should.include.keys([
'fixtures/templates/hero/include.njk',
'fixtures/templates/villain/include.njk'
]);
});

it('should have unique entries in `webpackDependencies`', function() {
nunjucks.webpackDependencies.should.include.keys([
'fixtures/templates/hero/default.njk',
'fixtures/templates/villain/default.njk'
]);
Expand Down