Created
March 5, 2020 02:45
-
-
Save samthor/b406117023fb8c88bd90a203756970be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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