Skip to content

Instantly share code, notes, and snippets.

@pauleveritt
Created April 17, 2024 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pauleveritt/ae332543b07443abb765566ae74eb849 to your computer and use it in GitHub Desktop.
Save pauleveritt/ae332543b07443abb765566ae74eb849 to your computer and use it in GitHub Desktop.
11ty constructor, options, dirs
test("should load an Eleventy site", async () => {
const elev = new Eleventy("./", "./_site", {
dir: {
includes: "my_includes",
layouts: "my_layouts",
},
});
await elev.init();
const d = elev.config.dir;
expect(elev.config.dir.includes).toEqual("my_includes");
});
@zachleat
Copy link

zachleat commented Apr 17, 2024

This pieces isn’t currently supported to set includes/layouts dirs:

  const elev = new Eleventy("./", "./_site", {
-    dir: {
-      includes: "my_includes",
-      layouts: "my_layouts",
-    },
  });

Eleventy 3.0+

If you want to programmatically set custom includes/layouts directories without using a configuration file, the most straightforward way is to use the config API to do so.

import Eleventy from "@11ty/eleventy";

const elev = new Eleventy("./", "./_site", {
	config: eleventyConfig => {
		// 3.0.0-alpha.6+
		eleventyConfig.setLayoutsDirectory("my_layouts");
		eleventyConfig.setIncludesDirectory("my_includes");
	},
});

await elev.init();

// 3.0.0-alpha.6+
console.log(
	elev.directories.input,
	elev.directories.output,
	elev.directories.includes,
	elev.directories.layouts
);

Eleventy 2.0+

For previous versions of Eleventy it’s probably easiest to use configPath to point to an external configuration file:

const Eleventy = require("@11ty/eleventy");

(async function() {
	const elev = new Eleventy("./", "./_site", {
		configPath: "./my-custom-config.js"
	});

	await elev.init();

	console.log(
		elev.config.dir.input,
		elev.config.dir.output,
		elev.config.dir.includes,
		elev.config.dir.layouts
	);
})();

where my-custom-config.js contains:

module.exports = function(eleventyConfig) {
	return {
		dir: {
			layouts: "my_layouts",
			includes: "my_includes",
		}
	};
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment