From a0b0ccbb19624c6ddc143a7acfc3386b7ffd5240 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Thu, 2 Feb 2023 08:10:43 -0600 Subject: [PATCH] Only addPlugin once --- eleventy.config.js | 21 +++++++++++++++++---- eleventy.shortcodes.js | 4 ++-- package.json | 2 +- sample/sample-config.js | 7 +++++-- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/eleventy.config.js b/eleventy.config.js index 8668abb..2ac8977 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -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({ @@ -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) { @@ -32,5 +34,16 @@ module.exports = function(eleventyConfig, options = {}) { options = normalizeOptions(options); - eleventyConfig.addPlugin(shortcodesPlugin, options); -}; \ No newline at end of file + 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; diff --git a/eleventy.shortcodes.js b/eleventy.shortcodes.js index ca2b81d..8d5142b 100644 --- a/eleventy.shortcodes.js +++ b/eleventy.shortcodes.js @@ -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 ""; @@ -77,4 +77,4 @@ module.exports = function(eleventyConfig, options = {}) { } }); } -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index ef8da16..d4d5ccb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/sample/sample-config.js b/sample/sample-config.js index c265dba..6efdf70 100644 --- a/sample/sample-config.js +++ b/sample/sample-config.js @@ -1,3 +1,6 @@ +const bundlePlugin = require("../eleventy.config.js"); + module.exports = function(eleventyConfig) { - eleventyConfig.addPlugin(require("../eleventy.config.js")); -}; \ No newline at end of file + eleventyConfig.addPlugin(bundlePlugin); + eleventyConfig.addPlugin(bundlePlugin); +};