-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
113 lines (95 loc) · 3.39 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
const fs = require('fs');
const dotenv = require('dotenv')
const { DateTime } = require("luxon");
const postCss = require("postcss");
const tailwind = require("tailwindcss");
const autoPrefixer = require("autoprefixer");
const cssNano = require("cssnano");
const htmlmin = require("html-minifier");
const pluginBundle = require("@11ty/eleventy-plugin-bundle");
const pluginRss = require("@11ty/eleventy-plugin-rss");
const pluginNavigation = require("@11ty/eleventy-navigation");
const pluginSyntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");
const { EleventyHtmlBasePlugin } = require("@11ty/eleventy");
module.exports = function (eleventyConfig) {
const result = dotenv.config();
if (result.error) {
throw result.error;
}
const isDev = process.env.ELEVENTY_ENV === 'dev';
const eleventyPort = process.env.ELEVENTY_PORT;
const prodBaseURL = process.env.ELEVENTY_PROD_BASEURL;
const baseUrl = isDev ? `http://localhost:${eleventyPort}` : prodBaseURL;
eleventyConfig.addGlobalData("baseUrl", baseUrl);
eleventyConfig.setServerOptions({
// The starting port number
// Will increment up to (configurable) 10 times if a port is already in use.
port: eleventyPort,
});
eleventyConfig.addPassthroughCopy({
"./public/assets": "assets/",
});
eleventyConfig.addWatchTarget("./public/css/");
// Official plugins
eleventyConfig.addPlugin(pluginRss);
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
preAttributes: { tabindex: 0 }
});
eleventyConfig.addPlugin(pluginNavigation);
eleventyConfig.addPlugin(EleventyHtmlBasePlugin);
eleventyConfig.addPlugin(pluginBundle);
// file published & modified date
eleventyConfig.addShortcode("published", function () {
return this.page?.inputPath ? fs.statSync(this.page.inputPath).birthtime : undefined;
});
eleventyConfig.addShortcode("modified", function () {
return this.page?.inputPath ? fs.statSync(this.page.inputPath).mtime : undefined;
});
eleventyConfig.addNunjucksAsyncFilter("postcss", (code, done) => {
postCss([
tailwind(require("./tailwind.config")),
autoPrefixer(),
cssNano({ preset: "default" }),
])
.process(code, {
from: "./public/css/style.css",
})
.then(
(r) => done(null, r.css),
(e) => done(e, null)
);
});
eleventyConfig.addFilter("fullUrl", function (url = '', base = eleventyConfig.globalData.baseUrl) {
try {
return new URL(url, base).href;
} catch (e) {
console.error(e);
}
return false;
});
eleventyConfig.addFilter("randomItem", (arr) => {
arr.sort(() => {
return 0.5 - Math.random();
});
return arr.slice(0, 1);
});
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;
});
return {
dir: {
input: "src",
includes: "../_includes",
data: "../_data",
output: "_site",
},
};
};