Skip to content
Merged
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
39 changes: 31 additions & 8 deletions lib/plugins/renderer/nunjucks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { readFileSync } from 'hexo-fs';
import { dirname } from 'path';
import type { StoreFunctionData } from '../../extend/renderer';

interface CacheableEnvironment extends Environment {
invalidateCache(): void;
}

interface CompiledTemplate {
env: CacheableEnvironment;
template: nunjucks.Template;
}

function toArray(value) {
if (Array.isArray(value)) {
// Return if given value is an Array
Expand Down Expand Up @@ -42,27 +51,41 @@ const nunjucksAddFilter = (env: Environment): void => {
env.addFilter('safedump', safeJsonStringify);
};

function njkCompile(data: StoreFunctionData): nunjucks.Template {
let env: Environment;
function njkCompile(data: StoreFunctionData): CompiledTemplate {
let env: CacheableEnvironment;
if (data.path) {
env = nunjucks.configure(dirname(data.path), nunjucksCfg);
env = nunjucks.configure(dirname(data.path), nunjucksCfg) as CacheableEnvironment;
} else {
env = nunjucks.configure(nunjucksCfg);
env = nunjucks.configure(nunjucksCfg) as CacheableEnvironment;
}
nunjucksAddFilter(env);

const text = 'text' in data ? data.text : readFileSync(data.path);

return nunjucks.compile(text, env, data.path);
return {
env,
template: nunjucks.compile(text, env, data.path)
};
}

function njkRenderer(data: StoreFunctionData, locals?: any): string {
return njkCompile(data).render(locals);
return njkCompile(data).template.render(locals);
}

njkRenderer.compile = (data: StoreFunctionData): (locals: any) => string => {
// Need a closure to keep the compiled template.
return locals => njkCompile(data).render(locals);
const { env, template } = njkCompile(data);

return locals => {
// The top-level template is compiled directly and is not stored in the loader cache,
// so invalidating the cache here does not recompile it. This intentionally reloads
// extends/include/import dependencies before every render to preserve `hexo server`
// hot updates because the renderer has no signal indicating that a dependency changed.
// The trade-off is that unchanged dependencies are also recompiled on every render.
// If a reliable theme-template change signal becomes available, invalidate the cache
// in response to that signal instead of removing this call outright.
env.invalidateCache();
return template.render(locals);
};
};

export = njkRenderer;
69 changes: 69 additions & 0 deletions test/scripts/renderers/nunjucks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import r from '../../../lib/plugins/renderer/nunjucks';
import nunjucks from 'nunjucks';
import { dirname, join } from 'path';
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import chai from 'chai';
import { spy } from 'sinon';
const _should = chai.should();


Expand Down Expand Up @@ -47,6 +51,71 @@ describe('nunjucks', () => {
}).should.eql('Hello world!\n');
});

it('compile template only once', () => {
const compile = spy(nunjucks, 'compile');

try {
const render = r.compile({
text: 'Hello {{ name }}!'
});

render({ name: 'world' }).should.eql('Hello world!');
render({ name: 'Hexo' }).should.eql('Hello Hexo!');
compile.calledOnce.should.be.true;
} finally {
compile.restore();
}
});

const dependencyCases = [
{
name: 'include',
source: '{%- include \'dependency.njk\' -%}',
firstDependency: 'one',
secondDependency: 'two',
firstResult: 'one',
secondResult: 'two'
},
{
name: 'extends',
source: '{% extends \'dependency.njk\' %}{% block body %}body{% endblock %}',
firstDependency: 'one:{% block body %}{% endblock %}',
secondDependency: 'two:{% block body %}{% endblock %}',
firstResult: 'one:body',
secondResult: 'two:body'
},
{
name: 'import',
source: '{%- import \'dependency.njk\' as dependency -%}{{ dependency.value() }}',
firstDependency: '{% macro value() %}one{% endmacro %}',
secondDependency: '{% macro value() %}two{% endmacro %}',
firstResult: 'one',
secondResult: 'two'
}
];

for (const dependencyCase of dependencyCases) {
it(`invalidate ${dependencyCase.name} cache before rendering`, () => {
const fixtureDir = mkdtempSync(join(tmpdir(), 'hexo-nunjucks-'));
const templatePath = join(fixtureDir, 'template.njk');
const dependencyPath = join(fixtureDir, 'dependency.njk');

try {
writeFileSync(dependencyPath, dependencyCase.firstDependency);
const render = r.compile({
path: templatePath,
text: dependencyCase.source
});

render({}).should.eql(dependencyCase.firstResult);
writeFileSync(dependencyPath, dependencyCase.secondDependency);
render({}).should.eql(dependencyCase.secondResult);
} finally {
rmSync(fixtureDir, { recursive: true });
}
});
}

describe('nunjucks filters', () => {
const forLoop = [
'{% for x in arr | toarray %}',
Expand Down
Loading