Skip to content

Commit

Permalink
Fix for bundle plugin transform removing empty nodes (need htmlTransf…
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Sep 13, 2024
1 parent 6332de7 commit 26c0fcf
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ import MemoizeUtil from "./Util/MemoizeFunction.js";
export default function (config) {
let templateConfig = this;

// Used for the HTML <base>, InputPathToUrl, Image transform plugins
let ut = new HtmlTransformer();
ut.setUserConfig(config);

// This needs to be assigned before bundlePlugin is added below.
config.htmlTransformer = ut;

config.addPlugin(bundlePlugin, {
bundles: false, // no default bundles included—must be opt-in.
immediate: true,
Expand Down Expand Up @@ -114,11 +121,7 @@ export default function (config) {
},
);

// Used for the HTML <base>, InputPathToUrl, Image transform plugins
let ut = new HtmlTransformer();
ut.setUserConfig(config);

config.htmlTransformer = ut;
// Run the `htmlTransformer` transform
config.addTransform("@11ty/eleventy/html-transformer", async function (content) {
return ut.transformContent(this.outputPath, content, this);
});
Expand Down
122 changes: 122 additions & 0 deletions test/BundlePluginTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import test from "ava";
import Eleventy from "../src/Eleventy.js";

test("addBundle", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css")
});
eleventyConfig.addTemplate("index.njk", "{% css %}/* Hi */{% endcss %}<style>{% getBundle 'css' %}</style>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `<style>/* Hi */</style>`);
});

test("addBundle (empty css)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css");
});

eleventyConfig.addTemplate("index.njk", "Hi<style>{% getBundle 'css' %}</style>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});

test("addBundle (empty js)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("js");
});

eleventyConfig.addTemplate("index.njk", "Hi<script>{% getBundle 'js' %}</script>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});

test("Empty script node is removed (not using bundle)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("js");
});

eleventyConfig.addTemplate("index.njk", "Hi<script></script>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});


test("Empty style node is removed (not using bundle)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css");
});

eleventyConfig.addTemplate("index.njk", "Hi<style></style>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});

test("Empty link node is removed (not using bundle)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css");
});

eleventyConfig.addTemplate("index.njk", "Hi<link rel='stylesheet' href=''>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});

test("Empty link node is removed (no href attribute at all, not using bundle)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css");
});

eleventyConfig.addTemplate("index.njk", "Hi<link rel='stylesheet'>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi`);
});

test("Empty link node is kept (no rel attribute, not using bundle)", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
config: eleventyConfig => {
eleventyConfig.addPlugin(() => {
eleventyConfig.addBundle("css");
});

eleventyConfig.addTemplate("index.njk", "Hi<link>");
}
});

let results = await elev.toJSON();
t.is(results[0].content, `Hi<link>`);
});

0 comments on commit 26c0fcf

Please sign in to comment.