-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheleventy.config.js
More file actions
206 lines (172 loc) · 6.48 KB
/
Copy patheleventy.config.js
File metadata and controls
206 lines (172 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
IdAttributePlugin,
InputPathToUrlTransformPlugin,
HtmlBasePlugin,
} from '@11ty/eleventy'
import pluginSyntaxHighlight from '@11ty/eleventy-plugin-syntaxhighlight'
import pluginNavigation from '@11ty/eleventy-navigation'
import { EleventyRenderPlugin } from '@11ty/eleventy'
import pluginFilters from './_config/filters.js'
import fontAwesomePlugin from "@11ty/font-awesome";
import { PurgeCSS } from 'purgecss'
import * as fs from 'fs';
import CleanCSS from "clean-css";
import htmlmin from "html-minifier-terser";
import Image from "@11ty/eleventy-img";
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default async function (eleventyConfig) {
eleventyConfig.addPlugin(fontAwesomePlugin);
// Drafts, see also _data/eleventyDataSchema.js
eleventyConfig.addPreprocessor('drafts', '*', (data, content) => {
if (data.draft && process.env.ELEVENTY_RUN_MODE === 'build') {
return false
}
})
// Copy the contents of the `public` folder to the output folder
// For example, `./public/css/` ends up in `_site/css/`
eleventyConfig
.addPassthroughCopy({
'./public/': '/',
CNAME: 'CNAME',
})
// Run Eleventy when these files change:
// https://www.11ty.dev/docs/watch-serve/#add-your-own-watch-targets
// Watch content images for the image pipeline.
eleventyConfig.addWatchTarget('content/**/*.{svg,webp,png,jpeg}')
// Per-page bundles, see https://github.com/11ty/eleventy-plugin-bundle
// Adds the {% css %} paired shortcode
eleventyConfig.addBundle('css', {
toFileDirectory: 'dist',
})
// Adds the {% js %} paired shortcode
eleventyConfig.addBundle('js', {
toFileDirectory: 'dist',
})
// Official plugins
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
preAttributes: { tabindex: 0 },
})
eleventyConfig.addPlugin(pluginNavigation)
eleventyConfig.addPlugin(HtmlBasePlugin)
eleventyConfig.addPlugin(InputPathToUrlTransformPlugin)
// Filters
eleventyConfig.addPlugin(pluginFilters)
eleventyConfig.addPlugin(IdAttributePlugin, {
// by default we use Eleventy's built-in `slugify` filter:
// slugify: eleventyConfig.getFilter("slugify"),
// selector: "h1,h2,h3,h4,h5,h6", // default
})
// Responsive, optimized <picture> output (webp + original-format
// fallback, multiple widths, explicit width/height to avoid layout
// shift) for images referenced via the imgOg frontmatter field —
// see _includes/image.html. Source images live in public/assets/img/
// posts/ (passthrough-copied as-is too, for OG/social meta tags,
// which need one fixed image, not a responsive set).
eleventyConfig.addAsyncShortcode('optimizedImage', async function (src, alt, cssClass) {
let metadata = await Image(src, {
widths: [400, 800, 1200, null],
formats: ['webp', null],
outputDir: './_site/assets/img/optimized/',
urlPath: '/assets/img/optimized/',
sharpPngOptions: { compressionLevel: 9 },
});
return Image.generateHTML(metadata, {
alt,
loading: 'lazy',
decoding: 'async',
class: cssClass,
sizes: '(min-width: 992px) 800px, 100vw',
});
})
eleventyConfig.addShortcode('currentBuildDate', () => {
return new Date().toISOString()
})
eleventyConfig.addShortcode('thisYear', () => {
return new Date().getFullYear();
})
eleventyConfig.addFilter('standardFormatDate', (time) => {
return new Date(time).toLocaleDateString('en-US', {
timeZone: 'America/Los_Angeles',
})
})
eleventyConfig.addFilter('dateFormat', (time) => {
return new Date(time).toLocaleString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric'
});
});
eleventyConfig.addFilter('logger', (obj) => {
console.log('logger output:', obj);
return '';
})
eleventyConfig.addFilter('timeSort', (logs) => {
if (logs) {
return logs.sort((a, b) => b.time - a.time)
}
return []
})
eleventyConfig.addPlugin(EleventyRenderPlugin)
eleventyConfig.addFilter('encodeParameter', (param) => {
return encodeURIComponent(param.trim());
})
eleventyConfig.addFilter("cssmin", function (code) {
return new CleanCSS({}).minify(code).styles;
});
eleventyConfig.addFilter("includes", function (str, substr) {
if (typeof str === 'string') {
return str.includes(substr);
}
return false;
});
eleventyConfig.addTransform("cssinliner", async function (content) {
if ((this.page.outputPath || "").endsWith(".html")) {
if (!fs.existsSync('scripts/bundle.css')) {
return content;
}
const purgeCSSResults = await new PurgeCSS().purge({
content: [
{
raw: content,
extension: 'html' // Indicate the content type
}
],
css: ['scripts/bundle.css'],
})
if (purgeCSSResults && purgeCSSResults[0]) {
let minifiedCSS = new CleanCSS({}).minify(purgeCSSResults[0].css).styles;
return content.replace('<!--put inlined css here-->',`<style>${minifiedCSS}</style>`);
}
}
// If not an HTML output, return content as-is
return content;
});
eleventyConfig.addTransform("htmlmin", async function (content) {
if ((this.page.outputPath || "").endsWith(".html")) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
});
return minified;
}
// If not an HTML output, return content as-is
return content;
});
}
export const config = {
// Control which files Eleventy will process
// e.g.: *.md, *.njk, *.html, *.liquid
templateFormats: ['md', 'njk', 'html', 'liquid', '11ty.js'],
// Pre-process *.md files with: (default: `liquid`)
markdownTemplateEngine: 'njk',
// Pre-process *.html files with: (default: `liquid`)
htmlTemplateEngine: 'njk',
// These are all optional:
dir: {
input: 'content', // default: "."
includes: '../_includes', // default: "_includes" (`input` relative)
data: '../_data', // default: "_data" (`input` relative)
output: '_site',
},
}