Skip to content

Commit

Permalink
Revert making shortcodes and transforms unique (re-add necessary when…
Browse files Browse the repository at this point in the history
… config resets)
  • Loading branch information
zachleat committed Jan 28, 2025
1 parent 7dd35b1 commit 1e86bca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
10 changes: 5 additions & 5 deletions eleventy.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ function eleventyBundlePlugin(eleventyConfig, pluginOptions = {}) {

let alreadyAdded = "getBundleManagers" in eleventyConfig || "addBundle" in eleventyConfig;
if(!alreadyAdded || pluginOptions.force) {
if(alreadyAdded) {
if(alreadyAdded && pluginOptions.force) {
debug("Bundle plugin already added via `addPlugin`, add was forced via `force: true`");
}

bundleManagersPlugin(eleventyConfig, pluginOptions);

pruneEmptyBundlesPlugin(eleventyConfig, pluginOptions);

globalShortcodesAndTransforms(eleventyConfig, pluginOptions);
}

// These can’t be unique (don’t skip re-add above), when the configuration file resets they need to be added again
pruneEmptyBundlesPlugin(eleventyConfig, pluginOptions);
globalShortcodesAndTransforms(eleventyConfig, pluginOptions);

// Support subsequent calls like addPlugin(BundlePlugin, { bundles: [] });
if(Array.isArray(pluginOptions.bundles)) {
debug("Adding bundles via `addPlugin`: %o", pluginOptions.bundles)
Expand Down
28 changes: 14 additions & 14 deletions src/eleventy.pruneEmptyBundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ function eleventyPruneEmptyBundles(eleventyConfig, options = {}) {
// `false` to disable
options.pruneEmptySelector = options.pruneEmptySelector ?? `style,script,link[rel="stylesheet"]`;

// Subsequent call can remove a previously added `addPosthtmlPlugin` entry
// htmlTransformer.remove is v3.0.1-alpha.4+
if(typeof eleventyConfig.htmlTransformer.remove === "function") {
eleventyConfig.htmlTransformer.remove("html", entry => {
if(entry.name === POSTHTML_PLUGIN_NAME) {
return true;
}

// Temporary workaround for missing `name` property.
let fnStr = entry.fn.toString();
return !entry.name && fnStr.startsWith("function (pluginOptions = {}) {") && fnStr.includes(`tree.match(matchHelper(options.pruneEmptySelector), function (node)`);
});
}

// `false` disables this plugin
if(options.pruneEmptySelector === false) {
// Subsequent call can remove a previously added `addPosthtmlPlugin` entry
// htmlTransformer.remove is v3.0.1-alpha.4+
if(typeof eleventyConfig.htmlTransformer.remove === "function") {
eleventyConfig.htmlTransformer.remove("html", entry => {
if(entry.name === POSTHTML_PLUGIN_NAME) {
return true;
}

// Temporary workaround for missing `name` property.
let fnStr = entry.fn.toString();
return !entry.name && fnStr.startsWith("function (pluginOptions = {}) {") && fnStr.includes(`tree.match(matchHelper(options.pruneEmptySelector), function (node)`);
});
}

return;
}

Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ test.skip("Empty CSS bundle (trimmed) does *not* remove empty <style> tag (unski
t.deepEqual(normalize(results[0].content), `<div></div><style></style>`)
});

test("Empty CSS bundle (trimmed) does *not* remove empty <style eleventy:keep> tag", async t => {
// TODO this requires `htmlTransformer.remove` which is core v3.0.1-alpha.4+
test.skip("Empty CSS bundle (trimmed) does *not* remove empty <style eleventy:keep> tag (unskip after Eleventy v3.0.1+)", async t => {
let elev = new Eleventy("test/stubs-virtual/", "_site", {
config: function(eleventyConfig) {
eleventyConfig.addPlugin(bundlePlugin);
Expand Down

0 comments on commit 1e86bca

Please sign in to comment.