forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
278 lines (254 loc) · 12.8 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const pluginRss = require('@11ty/eleventy-plugin-rss');
const pluginSyntaxHighlight = require('@11ty/eleventy-plugin-syntaxhighlight');
const yaml = require('js-yaml');
const fs = require('fs');
const markdown = require('./src/site/_plugins/markdown');
// Shortcodes used in prose
const Aside = require('./src/site/_includes/components/Aside');
const {Blockquote} = require('webdev-infra/shortcodes/Blockquote');
const {Codepen} = require('webdev-infra/shortcodes/Codepen');
const Compare = require('./src/site/_includes/components/Compare');
const CompareCaption = require('./src/site/_includes/components/CompareCaption');
const {Details} = require('webdev-infra/shortcodes/Details');
const {DetailsSummary} = require('webdev-infra/shortcodes/DetailsSummary');
const Glitch = require('./src/site/_includes/components/Glitch');
const IFrame = require('./src/site/_includes/components/IFrame');
const {Img, generateImgixSrc} = require('./src/site/_includes/components/Img');
const Instruction = require('./src/site/_includes/components/Instruction');
const Label = require('./src/site/_includes/components/Label');
const {Video} = require('./src/site/_includes/components/Video');
const {YouTube} = require('webdev-infra/shortcodes/YouTube');
const BrowserCompat = require('./src/site/_includes/components/BrowserCompat');
const CodePattern = require('./src/site/_includes/components/CodePattern');
const Widget = require('./src/site/_includes/components/Widget');
// Other shortcodes
const Assessment = require('./src/site/_includes/components/Assessment');
const Author = require('./src/site/_includes/components/Author');
const AuthorsDate = require('./src/site/_includes/components/AuthorsDate');
const Banner = require('./src/site/_includes/components/Banner');
const Breadcrumbs = require('./src/site/_includes/components/Breadcrumbs');
const CodelabsCallout = require('./src/site/_includes/components/CodelabsCallout');
const Hero = require('./src/site/_includes/components/Hero');
const includeRaw = require('./src/site/_includes/components/includeRaw');
const LanguageList = require('./src/site/_includes/components/LanguageList');
const Meta = require('./src/site/_includes/components/Meta');
const PathCard = require('./src/site/_includes/components/PathCard');
const SignPosts = require('./src/site/_includes/components/SignPosts');
const StackOverflow = require('./src/site/_includes/components/StackOverflow');
const Tooltip = require('./src/site/_includes/components/Tooltip');
const YouTubePlaylist = require('./src/site/_includes/components/YouTubePlaylist');
// Collections
const authors = require('./src/site/_collections/authors');
const blogPostsDescending = require('./src/site/_collections/blog-posts-descending');
const newsletters = require('./src/site/_collections/newsletters');
const shows = require('./src/site/_collections/shows');
const tags = require('./src/site/_collections/tags');
// Filters
const consoleDump = require('./src/site/_filters/console-dump');
const {i18n} = require('./src/site/_filters/i18n');
const {getDefaultUrl, getRelativePath} = require('./src/site/_filters/urls');
const {memoize, findByUrl} = require('./src/site/_filters/find-by-url');
const pathSlug = require('./src/site/_filters/path-slug');
const algoliaIndexable = require('./src/site/_filters/algolia-indexable');
const algoliaItem = require('./src/site/_filters/algolia-item');
const containsTag = require('./src/site/_filters/contains-tag');
const expandAuthors = require('./src/site/_filters/expand-authors');
const githubLink = require('./src/site/_filters/github-link');
const gitlocalizeLink = require('./src/site/_filters/gitlocalize-link');
const htmlDateString = require('./src/site/_filters/html-date-string');
const isNewContent = require('./src/site/_filters/is-new-content');
const livePosts = require('./src/site/_filters/live-posts');
const md = require('./src/site/_filters/md');
const pagedNavigation = require('./src/site/_filters/paged-navigation');
const postsLighthouseJson = require('./src/site/_filters/posts-lighthouse-json');
const prettyDate = require('./src/site/_filters/pretty-date');
const removeDrafts = require('./src/site/_filters/remove-drafts');
const slugify = require('./src/site/_filters/slugify');
const strip = require('./src/site/_filters/strip');
const stripBlog = require('./src/site/_filters/strip-blog');
const getPaths = require('./src/site/_filters/get-paths');
const navigation = require('./src/site/_filters/navigation');
const {minifyJs} = require('./src/site/_filters/minify-js');
const {cspHash, getHashList} = require('./src/site/_filters/csp-hash');
const {siteRender} = require('./src/site/_filters/site-render');
const disableLazyLoad = require('./src/site/_transforms/disable-lazy-load');
const {purifyCss} = require('./src/site/_transforms/purify-css');
const {minifyHtml} = require('./src/site/_transforms/minify-html');
// Shared dependencies between web.dev and developer.chrome.com
const {updateSvgForInclude} = require('webdev-infra/filters/svg');
const {toc} = require('webdev-infra/filters/toc');
// Creates a global variable for the current __dirname to make including and
// working with files in the component library a little easier
global.__basedir = __dirname;
module.exports = function (config) {
console.log(chalk.black.bgGreen('Eleventy is building, please wait…'));
const isProd = process.env.ELEVENTY_ENV === 'prod';
const isStaging = process.env.ELEVENTY_ENV === 'staging';
// ----------------------------------------------------------------------------
// PLUGINS
// ----------------------------------------------------------------------------
// Syntax highlighting for code snippets
config.addPlugin(pluginSyntaxHighlight);
// RSS feeds
config.addPlugin(pluginRss);
// ----------------------------------------------------------------------------
// MARKDOWN
// ----------------------------------------------------------------------------
config.setLibrary('md', markdown);
// ----------------------------------------------------------------------------
// NON-11TY FILES TO WATCH
// ----------------------------------------------------------------------------
config.addWatchTarget('./src/site/content/en/**/*.yml');
// ----------------------------------------------------------------------------
// COLLECTIONS
// ----------------------------------------------------------------------------
config.addCollection('authors', authors);
config.addCollection('blogPosts', blogPostsDescending);
config.addCollection('newsletters', newsletters);
config.addCollection('shows', shows);
config.addCollection('tags', tags);
// Turn collection.all into a lookup table so we can use findBySlug
// to quickly find collection items without looping.
config.addCollection('memoized', (collection) => {
return memoize(collection.getAll());
});
// Filters through all collection items and finds content that has
// CSS_ORIGIN set to 'next'. This allows shortcodes to determine if we
// are in a design system context or a legacy context
config.addCollection('designSystemGlobals', (collection) => {
global.__designSystemPaths = new Set(
collection
.getAll()
.filter(({data}) => data.CSS_ORIGIN === 'next')
.map(({filePathStem}) => filePathStem),
);
return global.__designSystemPaths;
});
// ----------------------------------------------------------------------------
// FILTERS
// ----------------------------------------------------------------------------
config.addFilter('consoleDump', consoleDump);
config.addFilter('i18n', i18n);
config.addFilter('findByUrl', findByUrl);
config.addFilter('getDefaultUrl', getDefaultUrl);
config.addFilter('getRelativePath', getRelativePath);
config.addFilter('pathSlug', pathSlug);
config.addFilter('algoliaIndexable', algoliaIndexable);
config.addFilter('algoliaItem', algoliaItem);
config.addFilter('containsTag', containsTag);
config.addFilter('expandAuthors', expandAuthors);
config.addFilter('githubLink', githubLink);
config.addFilter('gitlocalizeLink', gitlocalizeLink);
config.addFilter('htmlDateString', htmlDateString);
config.addFilter('imgix', generateImgixSrc);
config.addFilter('isNewContent', isNewContent);
config.addFilter('livePosts', livePosts);
config.addFilter('md', md);
config.addFilter('navigation', navigation);
config.addNunjucksAsyncFilter('siteRender', siteRender);
config.addFilter('pagedNavigation', pagedNavigation);
config.addFilter('postsLighthouseJson', postsLighthouseJson);
config.addFilter('prettyDate', prettyDate);
config.addFilter('removeDrafts', removeDrafts);
config.addFilter('slugify', slugify);
config.addFilter('stripBlog', stripBlog);
config.addFilter('getPaths', getPaths);
config.addFilter('strip', strip);
config.addFilter('toc', toc);
config.addFilter('updateSvgForInclude', updateSvgForInclude);
config.addNunjucksAsyncFilter('minifyJs', minifyJs);
config.addFilter('cspHash', cspHash);
// ----------------------------------------------------------------------------
// SHORTCODES
// ----------------------------------------------------------------------------
config.addPairedShortcode('Aside', Aside);
config.addShortcode('Assessment', Assessment);
config.addShortcode('Author', Author);
config.addShortcode('AuthorsDate', AuthorsDate);
config.addPairedShortcode('Banner', Banner);
config.addPairedShortcode('Blockquote', Blockquote);
config.addShortcode('Breadcrumbs', Breadcrumbs);
config.addNunjucksShortcode('BrowserCompat', BrowserCompat);
config.addShortcode('CodelabsCallout', CodelabsCallout);
config.addShortcode('Codepen', Codepen);
config.addShortcode('CodePattern', CodePattern);
config.addPairedShortcode('Compare', Compare);
config.addPairedShortcode('CompareCaption', CompareCaption);
config.addPairedShortcode('Details', Details);
config.addPairedShortcode('DetailsSummary', DetailsSummary);
config.addShortcode('Glitch', Glitch);
config.addShortcode('Hero', Hero);
config.addShortcode('IFrame', IFrame);
config.addShortcode('Img', Img);
config.addShortcode('Instruction', Instruction);
config.addPairedShortcode('Label', Label);
config.addShortcode('LanguageList', LanguageList);
config.addShortcode('Meta', Meta);
config.addShortcode('PathCard', PathCard);
config.addShortcode('SignPosts', SignPosts);
config.addShortcode('StackOverflow', StackOverflow);
config.addShortcode('Tooltip', Tooltip);
config.addShortcode('Widget', Widget);
config.addShortcode('Video', Video);
config.addShortcode('YouTube', YouTube);
config.addShortcode('YouTubePlaylist', YouTubePlaylist);
config.addShortcode('includeRaw', includeRaw);
// ----------------------------------------------------------------------------
// TRANSFORMS
// ----------------------------------------------------------------------------
if (process.env.PERCY) {
config.addTransform('disable-lazy-load', disableLazyLoad);
}
if (isProd || isStaging) {
config.addTransform('purifyCss', purifyCss);
config.addTransform('minifyHtml', minifyHtml);
}
// ----------------------------------------------------------------------------
// ELEVENTY OPTIONS
// ----------------------------------------------------------------------------
// https://www.11ty.io/docs/config/#data-deep-merge
config.setDataDeepMerge(true);
config.setUseGitIgnore(false);
// Make .yml files work in the _data directory.
config.addDataExtension('yml', (contents) => yaml.safeLoad(contents));
// Make CSP hashes accessible to firebase config.
if (isProd) {
config.on('afterBuild', () => {
fs.writeFileSync(
'dist/script-hash-list.json',
JSON.stringify(getHashList()),
);
});
}
return {
dir: {
input: 'src/site/content/', // we use a string path with the forward slash since windows doesn't like the paths generated from path.join
output: 'dist',
data: '../_data',
includes: '../_includes',
},
templateFormats: ['njk', 'md'],
htmlTemplateEngine: 'njk',
markdownTemplateEngine: 'njk',
// Because eleventy's passthroughFileCopy does not work with permalinks
// we need to manually copy assets ourselves using gulp.
// https://github.com/11ty/eleventy/issues/379
passthroughFileCopy: false,
};
};