Skip to content

Instantly share code, notes, and snippets.

@samthor
Created March 5, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samthor/b406117023fb8c88bd90a203756970be to your computer and use it in GitHub Desktop.
Save samthor/b406117023fb8c88bd90a203756970be to your computer and use it in GitHub Desktop.
let configured = false;
let finished = false;
const methodsToCall = [];
function setup() {
const Eleventy = require("@11ty/eleventy");
const originalWrite = Eleventy.prototype.write;
Eleventy.prototype.write = async function(...args) {
const out = await originalWrite.apply(this, args);
finished = true;
while (methodsToCall.length) {
const method = methodsToCall.shift();
await method();
}
return out;
};
}
/**
* @param {function(): void} method to call once Eleventy is done
*/
module.exports = function postEleventyBuild(method) {
if (!configured) {
configured = true;
setImmediate(setup);
}
if (finished) {
throw new Error(`Eleventy has already finished`);
}
methodsToCall.push(method);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment