summaryrefslogtreecommitdiff
path: root/node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js
diff options
context:
space:
mode:
authorShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
committerShipwreckt <me@shipwreckt.co.uk>2025-10-31 20:02:14 +0000
commit7a52ddeba2a68388b544f529d2d92104420f77b0 (patch)
tree15ddd47457a2cb4a96060747437d36474e4f6b4e /node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js
parent53d6ae2b5568437afa5e4995580a3fb679b7b91b (diff)
Changed from static to 11ty!
Diffstat (limited to 'node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js')
-rw-r--r--node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js105
1 files changed, 105 insertions, 0 deletions
diff --git a/node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js b/node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js
new file mode 100644
index 0000000..3bcaa72
--- /dev/null
+++ b/node_modules/@11ty/eleventy-plugin-bundle/src/eleventy.pruneEmptyBundles.js
@@ -0,0 +1,105 @@
+import matchHelper from "posthtml-match-helper";
+import debugUtil from "debug";
+
+const debug = debugUtil("Eleventy:Bundle");
+
+const ATTRS = {
+ keep: "eleventy:keep"
+};
+
+const POSTHTML_PLUGIN_NAME = "11ty/eleventy-bundle/prune-empty";
+
+function getTextNodeContent(node) {
+ if (!node.content) {
+ return "";
+ }
+
+ return node.content
+ .map((entry) => {
+ if (typeof entry === "string") {
+ return entry;
+ }
+ if (Array.isArray(entry.content)) {
+ return getTextNodeContent(entry);
+ }
+ return "";
+ })
+ .join("");
+}
+
+function eleventyPruneEmptyBundles(eleventyConfig, options = {}) {
+ // Right now script[src],link[rel="stylesheet"] nodes are removed if the final bundles are empty.
+ // `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) {
+ return;
+ }
+
+ if(!eleventyConfig.htmlTransformer || !eleventyConfig.htmlTransformer?.constructor?.SUPPORTS_PLUGINS_ENABLED_CALLBACK) {
+ debug("You will need to upgrade your version of Eleventy core to remove empty bundle tags automatically (v3 or newer).");
+ return;
+ }
+
+ eleventyConfig.htmlTransformer.addPosthtmlPlugin(
+ "html",
+ function bundlePruneEmptyPosthtmlPlugin(pluginOptions = {}) {
+ return function (tree) {
+ tree.match(matchHelper(options.pruneEmptySelector), function (node) {
+ if(node.attrs && node.attrs[ATTRS.keep] !== undefined) {
+ delete node.attrs[ATTRS.keep];
+ return node;
+ }
+
+ // <link rel="stylesheet" href="">
+ if(node.tag === "link") {
+ if(node.attrs?.rel === "stylesheet" && (node.attrs?.href || "").trim().length === 0) {
+ return false;
+ }
+ } else {
+ let content = getTextNodeContent(node);
+
+ if(!content) {
+ // <script></script> or <script src=""></script>
+ if(node.tag === "script" && (node.attrs?.src || "").trim().length === 0) {
+ return false;
+ }
+
+ // <style></style>
+ if(node.tag === "style") {
+ return false;
+ }
+ }
+ }
+
+
+ return node;
+ });
+ };
+ },
+ {
+ name: POSTHTML_PLUGIN_NAME,
+ // the `enabled` callback for plugins is available on v3.0.0-alpha.20+ and v3.0.0-beta.2+
+ enabled: () => {
+ return Object.keys(eleventyConfig.getBundleManagers()).length > 0;
+ }
+ }
+ );
+}
+
+export default eleventyPruneEmptyBundles;