-
Notifications
You must be signed in to change notification settings - Fork 1
/
.eleventy.js
131 lines (110 loc) · 4.11 KB
/
.eleventy.js
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
// .env file to be used with environment variables
require('dotenv').config();
// HTML minifier
const htmlmin = require("html-minifier");
// Markdown
const markdownIt = require('markdown-it');
const markdownItAttrs = require('markdown-it-attrs');
const markdownItOptions = {
html: true,
breaks: true,
linkify: true
}
const markdownLib = markdownIt(markdownItOptions).use(markdownItAttrs)
// Feather icon set
const feather = require('feather-icons');
const iconShortcode = (icon) => feather.icons[icon].toSvg({ class: 'inline-block'});
const iconShortcodeSmall = (icon) => feather.icons[icon].toSvg({ class: 'inline-block h-4'});
const now = String(Date.now());
// Configs
module.exports = function(eleventyConfig) {
eleventyConfig.setLibrary('md', markdownLib);
// Make .env variables to templates
eleventyConfig.addGlobalData('env', process.env);
// Add NJK template tag to access feathericons
eleventyConfig.addShortcode('icon', iconShortcode);
eleventyConfig.addShortcode('icon_small', iconShortcodeSmall);
// Get Axios and place in assets dir to use client-side
eleventyConfig.addPassthroughCopy({
"./node_modules/axios/dist/axios.min.js": "/js/axios.min.js"
});
// Get Sal.js (scroll animation library) and place in assets dir to use client-side
eleventyConfig.addPassthroughCopy({
"./node_modules/sal.js/dist/sal.css": "sal.css",
"./node_modules/sal.js/dist/sal.js": "/js/sal.js",
});
// Watch tailwindCSS
eleventyConfig.addWatchTarget('./styles/tailwind.config.js');
eleventyConfig.addWatchTarget('./styles/input.css');
eleventyConfig.addShortcode('version', function () {
return now;
});
// Minify HTML output
eleventyConfig.addTransform("htmlmin", function(content) {
if( this.page.outputPath && this.page.outputPath.endsWith(".html") ) {
let minified = htmlmin.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true
});
return minified;
}
return content;
});
// Enable CORS
eleventyConfig.setBrowserSyncConfig({
middleware: function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
}
});
// Set directories to pass through to the dist folder
eleventyConfig.addPassthroughCopy('./src/js/');
eleventyConfig.addPassthroughCopy('./src/css/');
eleventyConfig.addPassthroughCopy('./src/img/');
eleventyConfig.addPassthroughCopy('./src/favicons/');
eleventyConfig.addPassthroughCopy('./src/temp/');
eleventyConfig.addPassthroughCopy("./src/*.xml");
eleventyConfig.addPassthroughCopy("./src/*.txt");
// Add NJK template tag for WebP images (and fallback)
// Image is included this way: {% webpImg "images/image.jpg", "Alt text here", "Optional custom class here" %}
eleventyConfig.addShortcode("webpImg", function(filename, altText, width, height, classes = '', loading = false) {
const webpFilename = filename.replace(/\.[^.]+$/, ".webp");
// Call checkWebPSupport() here to determine WebP support
let hasWebPSupport = false;
checkWebPSupport(function(supported) {
hasWebPSupport = true;
});
const imgClasses = classes ? `class="${classes}"` : '';
const loadingAttr = loading ? 'loading="lazy"' : '';
if (hasWebPSupport) {
return `<picture>
<source srcset="${webpFilename}" type="image/webp">
<img src="${filename}" alt="${altText}" width="${width}" height="${height}" ${imgClasses} ${loadingAttr}>
</picture>`;
} else {
return `<img src="${filename}" alt="${altText}" ${imgClasses} ${loadingAttr}>`;
}
});
// Define checkWebPSupport() function
function checkWebPSupport(callback) {
const img = new (require('canvas').Image)();
img.onerror = function() {
callback(false);
};
img.onload = function() {
callback(true);
};
img.src = 'data:image/webp;base64,UklGRjoAAABXRUJQVlA4IBgAAAAwAQCdASoBAAEAAwA0JaQAA3AA/vuUAAA=';
}
eleventyConfig.setLiquidOptions({
dynamicPartials: true
});
// Define output directory when compiling assets
return {
dir: {
input: 'src',
output: 'dist'
}
};
};