Eleventy is a static site generator that takes input files (such as Markdown, HTML, or JavaScript) and processes them into output files (usually HTML). It uses a concept of “templates” to define how the content should be rendered. Eleventy supports multiple templating languages out of the box, including Liquid, Nunjucks, and Handlebars.
TypeScript is a programming language that builds on JavaScript by adding static types. It allows developers to catch errors early in the development process and provides better code documentation. TypeScript code needs to be transpiled into JavaScript before it can be run in a browser or on a server.
When using Eleventy with TypeScript, we can use TypeScript to write custom filters, shortcodes, and other Eleventy plugins. This allows us to take advantage of TypeScript’s type checking and code organization features while building our static sites.
mkdir eleventy-typescript-project
cd eleventy-typescript-project
npm init -y
npm install @11ty/eleventy typescript --save-dev
tsconfig.json
file in the root of your project with the following content:{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
eleventy.config.ts
file in the root of your project. Here’s a basic example:import { EleventyConfig } from "@11ty/eleventy";
export default (eleventyConfig: EleventyConfig) => {
// Add a custom filter
eleventyConfig.addFilter("uppercase", (str: string) => {
return str.toUpperCase();
});
return {
dir: {
input: "src",
output: "dist"
}
};
};
package.json
:{
"scripts": {
"build": "npx tsc && npx eleventy",
"serve": "npx tsc && npx eleventy --serve"
}
}
You can use TypeScript functions in your Eleventy templates by registering them as filters or shortcodes. For example, in a Nunjucks template, you can use the uppercase
filter we defined earlier:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Eleventy with TypeScript</title>
</head>
<body>
<p>{{ "hello world" | uppercase }}</p>
</body>
</html>
filters.ts
file to store all your custom filters:export const uppercase = (str: string) => {
return str.toUpperCase();
};
Then import and register them in your eleventy.config.ts
file:
import { EleventyConfig } from "@11ty/eleventy";
import { uppercase } from "./filters";
export default (eleventyConfig: EleventyConfig) => {
eleventyConfig.addFilter("uppercase", uppercase);
return {
dir: {
input: "src",
output: "dist"
}
};
};
Combining Eleventy with TypeScript can bring significant benefits to your static site development workflow. By using TypeScript’s type checking and code organization features, you can write more reliable and maintainable code. In this blog post, we’ve covered the fundamental concepts, usage methods, common practices, and best practices of using Eleventy with TypeScript. We hope this guide helps you get started and make the most out of this powerful combination.