11ty and Nunjuck

Warning

This is not a tutorial to create an 11ty website, this’s a note! You can find some very new and useful techniques on this note alongside the official documentation.

Installation

Setting & Deploy with Netlify

Sometimes, 11ty takes to much time to build (especially on the task of optimizing images). You shouldn’t use branch main to build your site because every time you make a push to main branch, Netlify will rebuild & deploy it. You should create a new branch base on main branch. It so-called netlify

Templating

SCSS to CSS

Terminal window
# folder structure
|- css
| |- main.scss
| |- components
| | |- ..
main.scss
@import '../components/navbar'
package.json
{
"scripts": {
"css-build": "node-sass css/main.scss - o css",
}
}
index.html
<head>
...
<link rel="stylesheet" href="css/main.css" />
...
</head>
# folder structure
|- css
| |- main.scss
| |- components
| | |- ..
main.scss
@import "./components/font"; // without extension
package.json
{
"scripts": {
"js-build": "rollup -c rollup.config.js",
}
}
rollup.config.js
import scss from "rollup-plugin-scss";
export default [
{
// plugin 1
},
{
input: "css/main_input.js", // where the input file containing import of main.scss
output: {
file: "css/main.js", // intermediate file which can be translated to css/main.css
format: "esm", // still not know
},
plugins: [
scss(), // there are other configs
],
},
];
index.html
<!-- in <head> -->
<link rel="stylesheet" href="css/main.css" />

Using postcss

Terminal window
# Install it and its plugin first
npm install autoprefixer postcss postcss-cli
Terminal window
# Create postcss.config.js on the same folder as package.json
module.exports = {
plugins: [require("autoprefixer")],
};
// npm command in package.json
"css:prefix": "postcss src/css/main.css --use autoprefixer --replace true"
Terminal window
# Watch (cannot use with `--replace`)
postcss --watch main.css -o main_.css --use autoprefixer

Nunjucks inside css

<style>
.bg {
background-image: url({{ bg-image }});
}
</style>
<!-- or -->
<div style="background-image: url({{ bg-image }});"></div>

Bootstrap + 11ty

👉 Bootstrap’s homepage

👉 How to Isolate Bootstrap CSS to Avoid Conflicts (using LESS)

TailwindCSS

Terminal window
npm install -D tailwindcss postcss autoprefixer
# init tailwind config file
npx tailwindcss init
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
css/tailwind.css
@tailwind base;
@tailwind components;
@tailwind utilities;
tailwind.config.js
content: ["./src/**/*.{njk,md}", "./src/**/*.svg",]
index.html
<head>
<link rel="stylesheet" href="{{ '/css/tailwind.css' | url }}">
</head
package.json
"scripts": {
"dev:css": "tailwindcss -i css/tailwind.css -o _site/css/tailwind.css --watch --postcss",
"build:css": "tailwindcss -i css/tailwind.css -o _site/css/tailwind.css --postcss"
}