Programmatic API Added in v1.0.0 Jump to heading
Contents
Starting in Eleventy 1.0, you can run Eleventy in your own Node script. (This is how the Eleventy Serverless plugin works, behind the scenes)
Examples Jump to heading
Write to the file system Jump to heading
Don’t forget to install Eleventy into your local project first!
Now create a file called my-node-script.js
with the following contents:
Filename my-node-script.js
const Eleventy = require("@11ty/eleventy");
(async function() {
let elev = new Eleventy();
await elev.write();
})();
Then run your new script from the command line. Don’t include ~ $
when you run this command.
node my-node-script.js
Don’t write to the file system Jump to heading
Using .write()
will write your output to the file system. If, instead, you want to retrieve the content programmatically without writing, use .toJSON()
or .toNDJSON()
.
JSON Output Jump to heading
const Eleventy = require("@11ty/eleventy");
(async function() {
let elev = new Eleventy();
let json = await elev.toJSON();
// All results
console.log( json );
})();
ndjson Output Jump to heading
const Eleventy = require("@11ty/eleventy");
(async function() {
let elev = new Eleventy();
let stream = await elev.toNDJSON();
stream.on("data", (entry) => {
// Stream one output result at a time
let json = JSON.parse(entry.toString());
console.log( json );
});
})();
Changing the Input and Output Directories Jump to heading
The first argument is the input directory. The second argument is the output directory.
const Eleventy = require("@11ty/eleventy");
(async function() {
let elev = new Eleventy( ".", "_site" );
// Use `write` or `toJSON` or `toNDJSON`
})();
Full Options List Jump to heading
The third argument to Eleventy is an options object.
const Eleventy = require("@11ty/eleventy");
(async function() {
let elev = new Eleventy( ".", "_site", {
// --quiet
quietMode: true,
// --config
configPath: ".eleventy.js",
config: function(eleventyConfig) {
// Do some custom Configuration API stuff
// Works great with eleventyConfig.addGlobalData
},
});
// Use `write` or `toJSON` or `toNDJSON`
})();
(More to come)