Skip to content

Commit

Permalink
Merge 310fa68 into d7e3f76
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov committed Dec 11, 2019
2 parents d7e3f76 + 310fa68 commit fd8ffa3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 36 deletions.
14 changes: 10 additions & 4 deletions src/precompile/with-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ export function withDependencies(resourcePath, source, options) {
extensionsInstances.map(([,, ext]) => ext)
);

const extensionCalls = nodes.findAll(nunjucks.nodes.CallExtension)
.map(({extName}) => {
return extensionsInstances.find(([name,, instance]) => {
const extensionCalls = nodes.findAll(nunjucks.nodes.CallExtension).map(
({extName}) => (
extensionsInstances.find(([name,, instance]) => {
// Sometime `extName` is instance of custom tag
return name === extName || instance === extName
})
}).filter(Boolean);
)
).filter(Boolean).filter(([extensionName], i, extensions) => {
const extension = extensions.find(([name]) => name === extensionName);
const extensionIndex = extensions.indexOf(extension);

return i === extensionIndex;
});

// For proper precompilation of parent templates
extensionsInstances.forEach(function([name,, extensionInstance]) {
Expand Down
61 changes: 41 additions & 20 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Advanced compilation extensions should compile custom tags 1`] = `
"<div id=\\"el100500\\">
This content will be replaced with the content from /stuff
</div>
"
`;
exports[`Advanced compilation extensions should compile custom tags from parent template 1`] = `
"<div id=\\"el100500\\">
This content will be replaced with the content from /stuff
</div>
<p>Child template for custom tag check</p>
"
`;
exports[`Advanced compilation extensions should compile multiple instances of same tag 1`] = `
"<div id=\\"el100500\\">
This content will be replaced with the content from /stuff
</div>
<div>
<div id=\\"el100500\\">
Content 1
</div>
</div>
<div>
<div id=\\"el100500\\">
Content 2
</div>
</div>
"
`;
exports[`Advanced compilation filters should compile async filters 1`] = `
"<!DOCTYPE html>
<html lang=\\"en\\">
Expand Down Expand Up @@ -43,26 +84,6 @@ exports[`Advanced compilation filters should compile single filter instance 1`]
"
`;
exports[`Advanced compilation should compile custom tags 1`] = `
"<div id=\\"el100500\\">
This content will be replaced with the content from /stuff
</div>
"
`;
exports[`Advanced compilation should compile custom tags from parent template 1`] = `
"<div id=\\"el100500\\">
This content will be replaced with the content from /stuff
</div>
<p>Child template for custom tag check</p>
"
`;
exports[`Advanced compilation should compile globals in parent templates 1`] = `
"<div>
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'test/fixtures/custom-extension.njk' %}
{% extends 'test/fixtures/extensions/base.njk' %}

{% block child %}
<p>Child template for custom tag check</p>
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/extensions/multiple.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'test/fixtures/extensions/base.njk' %}

{% block child %}
<div>
{% remote "/stuff" %}
Content 1
{% error %}
There was an error fetching /stuff
{% endremote %}
</div>

<div>
{% remote "/stuff" %}
Content 2
{% error %}
There was an error fetching /stuff
{% endremote %}
</div>
{% endblock %}
28 changes: 17 additions & 11 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,30 @@ describe('Advanced compilation', function() {
expect(output()).toMatchSnapshot();
});

test('should compile custom tags', async function() {
const output = await compiler('fixtures/custom-extension.njk', {
describe('extensions', function() {
const loaderOptions = {
extensions: {
RemoteExtension: path.join(__dirname, './fixtures/RemoteExtension.js')
RemoteExtension: path.join(__dirname, './fixtures/extensions/RemoteExtension.js')
}
};

test('should compile custom tags', async function() {
const output = await compiler('fixtures/extensions/base.njk', loaderOptions);

expect(output()).toMatchSnapshot();
});

expect(output()).toMatchSnapshot();
});
test('should compile custom tags from parent template', async function() {
const output = await compiler('fixtures/extensions/child.njk', loaderOptions);

test('should compile custom tags from parent template', async function() {
const output = await compiler('fixtures/custom-extension-child-template.njk', {
extensions: {
RemoteExtension: path.join(__dirname, './fixtures/RemoteExtension.js')
}
expect(output()).toMatchSnapshot();
});

expect(output()).toMatchSnapshot();
test('should compile multiple instances of same tag', async function() {
const output = await compiler('fixtures/extensions/multiple.njk', loaderOptions);

expect(output()).toMatchSnapshot();
});
});

describe('filters', function() {
Expand Down

0 comments on commit fd8ffa3

Please sign in to comment.