diff --git a/src/defaultConfig.js b/src/defaultConfig.js index 2e099032a..8cd985f74 100644 --- a/src/defaultConfig.js +++ b/src/defaultConfig.js @@ -58,6 +58,13 @@ import MemoizeUtil from "./Util/MemoizeFunction.js"; export default function (config) { let templateConfig = this; + // Used for the HTML , 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, @@ -114,11 +121,7 @@ export default function (config) { }, ); - // Used for the HTML , 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); }); diff --git a/test/BundlePluginTest.js b/test/BundlePluginTest.js new file mode 100644 index 000000000..ca891a329 --- /dev/null +++ b/test/BundlePluginTest.js @@ -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 %}"); + } + }); + + let results = await elev.toJSON(); + t.is(results[0].content, ``); +}); + +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"); + } + }); + + 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"); + } + }); + + 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"); + } + }); + + 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"); + } + }); + + 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"); + } + }); + + 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"); + } + }); + + 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"); + } + }); + + let results = await elev.toJSON(); + t.is(results[0].content, `Hi`); +});