Movatterモバイル変換


[0]ホーム

URL:


Skip to navigationSkip to main content
Blog
Eleventy, 2024 in Review
Versions
Stable
3.0.0
Canary
3.0.1-alpha.5
Toggle Menu
Eleventy5.81s
Gatsby43.36s

Programmatic API

Contents

You can run Eleventy in any arbitrary Node script.

Write to the file system

Don’t forget toinstall Eleventy into your local project first!

Now create a file calledmy-node-script.js with the following contents:

my-node-script.js
import Eleventyfrom"@11ty/eleventy";

let elev=newEleventy();
await elev.write();
(asyncfunction(){
const{ Eleventy}=awaitimport("@11ty/eleventy");

let elev=newEleventy();
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

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

my-node-script.js
import Eleventyfrom"@11ty/eleventy";

let elev=newEleventy();
let json=await elev.toJSON();

// All results
console.log(json);
(asyncfunction(){
const{ Eleventy}=awaitimport("@11ty/eleventy");

let elev=newEleventy();
let json=await elev.toJSON();

// All results
console.log(json);
})();

ndjson Output

my-node-script.js
import Eleventyfrom"@11ty/eleventy";

let elev=newEleventy();
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);
});
(asyncfunction(){
const{ Eleventy}=awaitimport("@11ty/eleventy");

let elev=newEleventy();
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

The first argument is the input directory. The second argument is the output directory.

my-node-script.js
import Eleventyfrom"@11ty/eleventy";

let elev=newEleventy(".","_site");

// Use `write` or `toJSON` or `toNDJSON`
(asyncfunction(){
const{ Eleventy}=awaitimport("@11ty/eleventy");

let elev=newEleventy(".","_site");

// Use `write` or `toJSON` or `toNDJSON`
})();

Full Options List

The third argument to Eleventy is an options object.

(This documentation section is a work in progress butyou’re welcome to dig into theEleventy class source code inv3.0.0 to learn more)

my-node-script.js
import Eleventyfrom"@11ty/eleventy";

let elev=newEleventy(".","_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`
(asyncfunction(){
const{ Eleventy}=awaitimport("@11ty/eleventy");

let elev=newEleventy(".","_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`
})();

Other pages in Eleventy Projects:


[8]ページ先頭

©2009-2025 Movatter.jp