Skip to content

Commit

Permalink
Only addPlugin once
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 2, 2023
1 parent ed3c90c commit a0b0ccb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
21 changes: 17 additions & 4 deletions eleventy.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const pkg = require("./package.json");
const shortcodesPlugin = require("./eleventy.shortcodes.js");
// const debug = require("debug")("Eleventy:Bundle");
const debug = require("debug")("Eleventy:Bundle");

function normalizeOptions(options) {
let shortcodes = Object.assign({
Expand All @@ -23,7 +23,9 @@ function normalizeOptions(options) {
return options;
}

module.exports = function(eleventyConfig, options = {}) {
let hasExecuted = false;

function eleventyBundlePlugin(eleventyConfig, options = {}) {
try {
eleventyConfig.versionCheck(pkg["11ty"].compatibility);
} catch(e) {
Expand All @@ -32,5 +34,16 @@ module.exports = function(eleventyConfig, options = {}) {

options = normalizeOptions(options);

eleventyConfig.addPlugin(shortcodesPlugin, options);
};
eleventyConfig.on("eleventy.before", () => {
hasExecuted = false;
});

if(hasExecuted) {
debug("Warning: You can only addPlugin @11ty/eleventy-plugin-bundle once per project. Subsequent adds are ignored.");
} else {
hasExecuted = true;
shortcodesPlugin(eleventyConfig, options);
}
};

module.exports = eleventyBundlePlugin;
4 changes: 2 additions & 2 deletions eleventy.shortcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function(eleventyConfig, options = {}) {
}

if(addShortcodeName) {
eleventyConfig.addPairedShortcode(addShortcodeName, function addContent(content, bucket, urlOverride) {
eleventyConfig.addPairedShortcode(addShortcodeName, function addContent(content, bucket, urlOverride) {
let url = urlOverride || this.page.url;
managers[name].addToPage(url, content, bucket);
return "";
Expand Down Expand Up @@ -77,4 +77,4 @@ module.exports = function(eleventyConfig, options = {}) {
}
});
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Little bundles of code, little bundles of joy.",
"main": "eleventy.config.js",
"scripts": {
"sample": "npx @11ty/eleventy --config=sample/sample-config.js --input=sample",
"sample": "DEBUG=Eleventy:Bundle npx @11ty/eleventy --config=sample/sample-config.js --input=sample",
"test": "npx ava"
},
"publishConfig": {
Expand Down
7 changes: 5 additions & 2 deletions sample/sample-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const bundlePlugin = require("../eleventy.config.js");

module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(require("../eleventy.config.js"));
};
eleventyConfig.addPlugin(bundlePlugin);
eleventyConfig.addPlugin(bundlePlugin);
};

0 comments on commit a0b0ccb

Please sign in to comment.