Skip to content

Commit 26c0fcf

Browse files
committed
Fix for bundle plugin transform removing empty nodes (need htmlTransformer earlier) 11ty/eleventy-plugin-bundle#8
1 parent 6332de7 commit 26c0fcf

File tree

2 files changed

+130
-5
lines changed

2 files changed

+130
-5
lines changed

src/defaultConfig.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ import MemoizeUtil from "./Util/MemoizeFunction.js";
5858
export default function (config) {
5959
let templateConfig = this;
6060

61+
// Used for the HTML <base>, InputPathToUrl, Image transform plugins
62+
let ut = new HtmlTransformer();
63+
ut.setUserConfig(config);
64+
65+
// This needs to be assigned before bundlePlugin is added below.
66+
config.htmlTransformer = ut;
67+
6168
config.addPlugin(bundlePlugin, {
6269
bundles: false, // no default bundles included—must be opt-in.
6370
immediate: true,
@@ -114,11 +121,7 @@ export default function (config) {
114121
},
115122
);
116123

117-
// Used for the HTML <base>, InputPathToUrl, Image transform plugins
118-
let ut = new HtmlTransformer();
119-
ut.setUserConfig(config);
120-
121-
config.htmlTransformer = ut;
124+
// Run the `htmlTransformer` transform
122125
config.addTransform("@11ty/eleventy/html-transformer", async function (content) {
123126
return ut.transformContent(this.outputPath, content, this);
124127
});

test/BundlePluginTest.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import test from "ava";
2+
import Eleventy from "../src/Eleventy.js";
3+
4+
test("addBundle", async (t) => {
5+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
6+
config: eleventyConfig => {
7+
eleventyConfig.addPlugin(() => {
8+
eleventyConfig.addBundle("css")
9+
});
10+
eleventyConfig.addTemplate("index.njk", "{% css %}/* Hi */{% endcss %}<style>{% getBundle 'css' %}</style>");
11+
}
12+
});
13+
14+
let results = await elev.toJSON();
15+
t.is(results[0].content, `<style>/* Hi */</style>`);
16+
});
17+
18+
test("addBundle (empty css)", async (t) => {
19+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
20+
config: eleventyConfig => {
21+
eleventyConfig.addPlugin(() => {
22+
eleventyConfig.addBundle("css");
23+
});
24+
25+
eleventyConfig.addTemplate("index.njk", "Hi<style>{% getBundle 'css' %}</style>");
26+
}
27+
});
28+
29+
let results = await elev.toJSON();
30+
t.is(results[0].content, `Hi`);
31+
});
32+
33+
test("addBundle (empty js)", async (t) => {
34+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
35+
config: eleventyConfig => {
36+
eleventyConfig.addPlugin(() => {
37+
eleventyConfig.addBundle("js");
38+
});
39+
40+
eleventyConfig.addTemplate("index.njk", "Hi<script>{% getBundle 'js' %}</script>");
41+
}
42+
});
43+
44+
let results = await elev.toJSON();
45+
t.is(results[0].content, `Hi`);
46+
});
47+
48+
test("Empty script node is removed (not using bundle)", async (t) => {
49+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
50+
config: eleventyConfig => {
51+
eleventyConfig.addPlugin(() => {
52+
eleventyConfig.addBundle("js");
53+
});
54+
55+
eleventyConfig.addTemplate("index.njk", "Hi<script></script>");
56+
}
57+
});
58+
59+
let results = await elev.toJSON();
60+
t.is(results[0].content, `Hi`);
61+
});
62+
63+
64+
test("Empty style node is removed (not using bundle)", async (t) => {
65+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
66+
config: eleventyConfig => {
67+
eleventyConfig.addPlugin(() => {
68+
eleventyConfig.addBundle("css");
69+
});
70+
71+
eleventyConfig.addTemplate("index.njk", "Hi<style></style>");
72+
}
73+
});
74+
75+
let results = await elev.toJSON();
76+
t.is(results[0].content, `Hi`);
77+
});
78+
79+
test("Empty link node is removed (not using bundle)", async (t) => {
80+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
81+
config: eleventyConfig => {
82+
eleventyConfig.addPlugin(() => {
83+
eleventyConfig.addBundle("css");
84+
});
85+
86+
eleventyConfig.addTemplate("index.njk", "Hi<link rel='stylesheet' href=''>");
87+
}
88+
});
89+
90+
let results = await elev.toJSON();
91+
t.is(results[0].content, `Hi`);
92+
});
93+
94+
test("Empty link node is removed (no href attribute at all, not using bundle)", async (t) => {
95+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
96+
config: eleventyConfig => {
97+
eleventyConfig.addPlugin(() => {
98+
eleventyConfig.addBundle("css");
99+
});
100+
101+
eleventyConfig.addTemplate("index.njk", "Hi<link rel='stylesheet'>");
102+
}
103+
});
104+
105+
let results = await elev.toJSON();
106+
t.is(results[0].content, `Hi`);
107+
});
108+
109+
test("Empty link node is kept (no rel attribute, not using bundle)", async (t) => {
110+
let elev = new Eleventy("./test/stubs-virtual/", undefined, {
111+
config: eleventyConfig => {
112+
eleventyConfig.addPlugin(() => {
113+
eleventyConfig.addBundle("css");
114+
});
115+
116+
eleventyConfig.addTemplate("index.njk", "Hi<link>");
117+
}
118+
});
119+
120+
let results = await elev.toJSON();
121+
t.is(results[0].content, `Hi<link>`);
122+
});

0 commit comments

Comments
 (0)