Skip to content

Commit

Permalink
Fixes #2799
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 17, 2023
1 parent 43fbb6a commit 9ff1ca9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/Engines/Handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class Handlebars extends TemplateEngine {
* @override
*/
async cachePartialFiles() {
let partials = await super.cachePartialFiles();
this.handlebarsLib.registerPartial(partials);
return partials;
let ret = await super.cachePartialFiles();
this.handlebarsLib.registerPartial(ret.partials);
return ret;
}

async compile(str) {
Expand Down
4 changes: 2 additions & 2 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { TemplatePath } = require("@11ty/eleventy-utils");
const TemplateEngine = require("./TemplateEngine");
const EleventyBaseError = require("../EleventyBaseError");
const getJavaScriptData = require("../Util/GetJavaScriptData");
const eventBus = require("../EventBus");
const EventBusUtil = require("../Util/EventBusUtil");

class JavaScriptTemplateNotDefined extends EleventyBaseError {}

Expand All @@ -14,7 +14,7 @@ class JavaScript extends TemplateEngine {

this.cacheable = false;

eventBus.on("eleventy.resourceModified", (inputPath, usedByDependants = []) => {
EventBusUtil.soloOn("eleventy.resourceModified", (inputPath, usedByDependants = []) => {
// Remove from cached instances when modified
let instancesToDelete = [TemplatePath.addLeadingDotSlash(inputPath), ...usedByDependants];
for (let inputPath of instancesToDelete) {
Expand Down
45 changes: 11 additions & 34 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ const { TemplatePath } = require("@11ty/eleventy-utils");
const TemplateEngine = require("./TemplateEngine");
const EleventyErrorUtil = require("../EleventyErrorUtil");
const EleventyShortcodeError = require("../EleventyShortcodeError");
const eventBus = require("../EventBus");
const EventBusUtil = require("../Util/EventBusUtil");

class Nunjucks extends TemplateEngine {
constructor(name, dirs, config) {
super(name, dirs, config);
this.nunjucksEnvironmentOptions =
this.config.nunjucksEnvironmentOptions || {};
this.nunjucksEnvironmentOptions = this.config.nunjucksEnvironmentOptions || {};

this.nunjucksPrecompiledTemplates =
this.config.nunjucksPrecompiledTemplates || {};
this._usingPrecompiled =
Object.keys(this.nunjucksPrecompiledTemplates).length > 0;
this.nunjucksPrecompiledTemplates = this.config.nunjucksPrecompiledTemplates || {};
this._usingPrecompiled = Object.keys(this.nunjucksPrecompiledTemplates).length > 0;

this.setLibrary(this.config.libraryOverrides.njk);

Expand Down Expand Up @@ -51,10 +48,7 @@ class Nunjucks extends TemplateEngine {
TemplatePath.getWorkingDir(),
]);

this.njkEnv = new NunjucksLib.Environment(
fsLoader,
this.nunjucksEnvironmentOptions
);
this.njkEnv = new NunjucksLib.Environment(fsLoader, this.nunjucksEnvironmentOptions);
}

this.config.events.emit("eleventy.engine.njk", {
Expand All @@ -68,7 +62,7 @@ class Nunjucks extends TemplateEngine {

// Correct, but overbroad. Better would be to evict more granularly, but
// resolution from paths isn't straightforward.
eventBus.on("eleventy.resourceModified", (path) => {
EventBusUtil.soloOn("eleventy.resourceModified", (path) => {
this.njkEnv.invalidateCache();
});

Expand All @@ -88,10 +82,7 @@ class Nunjucks extends TemplateEngine {
this.addAllShortcodes(this.config.nunjucksShortcodes);
this.addAllShortcodes(this.config.nunjucksAsyncShortcodes, true);
this.addAllPairedShortcodes(this.config.nunjucksPairedShortcodes);
this.addAllPairedShortcodes(
this.config.nunjucksAsyncPairedShortcodes,
true
);
this.addAllPairedShortcodes(this.config.nunjucksAsyncPairedShortcodes, true);
this.addGlobals(this.config.nunjucksGlobals);
}

Expand Down Expand Up @@ -208,10 +199,7 @@ class Nunjucks extends TemplateEngine {
shortcodeFn
.call(Nunjucks.normalizeContext(context), ...argArray)
.then(function (returnValue) {
resolve(
null,
new NunjucksLib.runtime.SafeString("" + returnValue)
);
resolve(null, new NunjucksLib.runtime.SafeString("" + returnValue));
})
.catch(function (e) {
resolve(
Expand All @@ -225,10 +213,7 @@ class Nunjucks extends TemplateEngine {
});
} else {
try {
let ret = shortcodeFn.call(
Nunjucks.normalizeContext(context),
...argArray
);
let ret = shortcodeFn.call(Nunjucks.normalizeContext(context), ...argArray);
return new NunjucksLib.runtime.SafeString("" + ret);
} catch (e) {
throw new EleventyShortcodeError(
Expand Down Expand Up @@ -276,11 +261,7 @@ class Nunjucks extends TemplateEngine {

if (isAsync) {
shortcodeFn
.call(
Nunjucks.normalizeContext(context),
bodyContent,
...argArray
)
.call(Nunjucks.normalizeContext(context), bodyContent, ...argArray)
.then(function (returnValue) {
resolve(null, new NunjucksLib.runtime.SafeString(returnValue));
})
Expand All @@ -299,11 +280,7 @@ class Nunjucks extends TemplateEngine {
resolve(
null,
new NunjucksLib.runtime.SafeString(
shortcodeFn.call(
Nunjucks.normalizeContext(context),
bodyContent,
...argArray
)
shortcodeFn.call(Nunjucks.normalizeContext(context), bodyContent, ...argArray)
)
);
} catch (e) {
Expand Down
30 changes: 25 additions & 5 deletions src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { TemplatePath } = require("@11ty/eleventy-utils");
const TemplateConfig = require("../TemplateConfig");
const EleventyExtensionMap = require("../EleventyExtensionMap");
const EleventyBaseError = require("../EleventyBaseError");
const EventBusUtil = require("../Util/EventBusUtil");

const debug = require("debug")("Eleventy:TemplateEngine");

class TemplateEngineConfigError extends EleventyBaseError {}
Expand All @@ -20,8 +22,8 @@ class TemplateEngine {
this.inputDir = dirs.input;
this.includesDir = dirs.includes;

this.partialsHaveBeenCached = false;
this.partials = [];
this.resetPartials();

this.engineLib = null;
this.cacheable = false;

Expand Down Expand Up @@ -88,9 +90,23 @@ class TemplateEngine {
return this.includesDir;
}

resetPartials() {
this.partialsHaveBeenCached = false;
this.partials = [];
this.partialsFiles = [];
}

async getPartials() {
if (!this.partialsHaveBeenCached) {
this.partials = await this.cachePartialFiles();
let ret = await this.cachePartialFiles();
this.partials = ret.partials;
this.partialsFiles = ret.files;

EventBusUtil.soloOn("eleventy.resourceModified", (path) => {
if ((this.partialsFiles || []).includes(path)) {
this.resetPartials();
}
});
}

return this.partials;
Expand All @@ -107,6 +123,8 @@ class TemplateEngine {
this.partialsHaveBeenCached = true;

let results = [];
let partialFiles = [];

if (this.includesDir) {
// TODO move this to use FileSystemSearch instead.
const fastglob = require("fast-glob");
Expand All @@ -115,7 +133,6 @@ class TemplateEngine {
bench.before();

let prefix = this.includesDir + "/**/*.";
let partialFiles = [];
await Promise.all(
this.extensions.map(async function (extension) {
partialFiles = partialFiles.concat(
Expand Down Expand Up @@ -162,7 +179,10 @@ class TemplateEngine {
Object.keys(partials)
);

return partials;
return {
files: partialFiles,
partials,
};
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Util/EventBusUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const eventBus = require("../EventBus");

class EventBusUtil {
// Used for non-global subscriptions that will blow away the previous listener
static soloOn(name, callback) {
eventBus.off(name, callback);
eventBus.on(name, callback);
}
}

module.exports = EventBusUtil;

0 comments on commit 9ff1ca9

Please sign in to comment.