-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
eleventy.config.js
127 lines (105 loc) · 3.9 KB
/
eleventy.config.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
// This file is the entry point for all 11ty configuration.
// It configures the core 11ty behavior and registers
// plugins and customization that live in `/src/_11ty`.
import { registerFilters } from './src/_11ty/filters.js';
import { registerShortcodes } from './src/_11ty/shortcodes.js';
import { markdown } from './src/_11ty/plugins/markdown.js';
import { configureHighlighting } from './src/_11ty/plugins/highlight.js';
import minifier from 'html-minifier-terser';
import yaml from 'js-yaml';
import { EleventyRenderPlugin } from '@11ty/eleventy';
import * as path from 'node:path';
import * as sass from 'sass';
// noinspection JSUnusedGlobalSymbols
/**
* @typedef {import('11ty/eleventy/UserConfig')} EleventyConfig
* @param {EleventyConfig} eleventyConfig
*/
export default function (eleventyConfig) {
const isProduction = process.env.PRODUCTION === 'true';
const shouldOptimize = process.env.OPTIMIZE === 'true';
eleventyConfig.on('eleventy.before', async () => {
await configureHighlighting(markdown);
});
eleventyConfig.addGlobalData('isProduction', isProduction);
eleventyConfig.setLibrary('md', markdown);
eleventyConfig.addDataExtension('yml,yaml', (contents) =>
yaml.load(contents),
);
eleventyConfig.setLiquidOptions({
cache: true,
strictFilters: true,
lenientIf: true,
jekyllInclude: true,
});
eleventyConfig.addPlugin(EleventyRenderPlugin);
registerFilters(eleventyConfig);
registerShortcodes(eleventyConfig);
eleventyConfig.addTemplateFormats('scss');
eleventyConfig.addWatchTarget('src/_sass');
eleventyConfig.addExtension('scss', {
outputFileExtension: 'css',
compile: function (inputContent, inputPath) {
const parsedPath = path.parse(inputPath);
if (parsedPath.name.startsWith('_')) {
return;
}
const result = sass.compileString(inputContent, {
style: shouldOptimize ? 'compressed' : 'expanded',
quietDeps: true,
loadPaths: [parsedPath.dir, 'src/_sass'],
});
const dependencies = result.loadedUrls
.filter(
(loadedUrl) =>
loadedUrl.protocol === 'file:' && loadedUrl.pathname !== '',
)
.map((url) => path.relative('.', url.pathname));
this.addDependencies(inputPath, dependencies);
return () => result.css;
},
});
eleventyConfig.addPassthroughCopy('src/content/assets/js');
eleventyConfig.addPassthroughCopy({'site-shared/pkgs/inject_dartpad/lib/inject_dartpad.js': 'assets/js/inject_dartpad.js'});
eleventyConfig.addPassthroughCopy('src/content/assets/images', { expand: true });
eleventyConfig.addPassthroughCopy('src/content/cookbook/img-files', { expand: true });
eleventyConfig.addPassthroughCopy('src/content/f', {
expand: true,
filter: /^(?!_).+/,
});
eleventyConfig.addPassthroughCopy('src/content/tools/devtools/release-notes', {
filter: (path) => path.includes('src') || path.includes('images'),
});
if (shouldOptimize) {
// If building for production, minify/optimize the HTML output.
// Doing so during serving isn't worth the extra build time.
eleventyConfig.addTransform('minify-html', async function (content) {
if (this.page.outputPath && this.page.outputPath.endsWith('.html')) {
// Minify the page's content if it's an HTML file.
// Other options can be enabled, but each should be tested.
return await minifier.minify(content, {
useShortDoctype: true,
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
});
}
return content;
});
}
eleventyConfig.setQuietMode(true);
eleventyConfig.setServerOptions({
port: 4000,
watch: ['src/_sass'],
});
return {
htmlTemplateEngine: 'liquid',
dir: {
input: 'src/content',
output: '_site',
layouts: '../_layouts',
includes: '../_includes',
data: '../_data',
},
};
}