-
Notifications
You must be signed in to change notification settings - Fork 29
/
webpack.config.js
502 lines (456 loc) · 14.5 KB
/
webpack.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import util from 'node:util';
import CopyPlugin from 'copy-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import getRelativeLuminance from 'get-relative-luminance';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import * as simpleIcons from 'simple-icons/icons';
import {siX as xIcon} from 'simple-icons/icons';
import {
getDirnameFromImportMeta,
getIconSlug,
getIconsData,
getThirdPartyExtensions,
} from 'simple-icons/sdk';
import alphaSort from './scripts/alpha-sorting.js';
import colorSort from './scripts/color-sorting.js';
import {githubApi} from './scripts/https.js';
import {
DEFAULT_LANGUAGE,
getLanguages,
getLanguagesFile,
loadTranslations,
updateTranslations,
} from './scripts/i18n.js';
const __dirname = getDirnameFromImportMeta(import.meta.url);
const allIcons = alphaSort(simpleIcons);
const sortedHexes = colorSort(allIcons.map((icon) => icon.hex));
const NODE_MODULES = path.resolve(__dirname, 'node_modules');
const OUT_DIR = path.resolve(__dirname, '_site');
const ROOT_DIR = path.resolve(__dirname, 'public');
const indexPath = path.resolve(ROOT_DIR, 'index.pug');
const currentIsoDateString = new Date().toISOString();
const getIconsDataBySlugs = async () => {
const dataBySlugs = {};
for (const iconData of await getIconsData()) {
dataBySlugs[getIconSlug(iconData)] = iconData;
}
return dataBySlugs;
};
const getIconPlainAliases = (iconData) => {
const aliases = [];
if (iconData.aliases) {
if (iconData.aliases.aka) {
Array.prototype.push.apply(aliases, iconData.aliases.aka);
}
if (iconData.aliases.dup) {
Array.prototype.push.apply(
aliases,
iconData.aliases.dup.map((duplicate) => duplicate.title),
);
}
if (iconData.aliases.loc) {
Array.prototype.push.apply(aliases, Object.values(iconData.aliases.loc));
}
}
return aliases;
};
const getIconLocalizedTitles = (iconData, languages) => {
const localizedTitles = {};
if (iconData.aliases && iconData.aliases.loc) {
for (const locale of Object.keys(iconData.aliases.loc)) {
if (languages.includes(locale)) {
localizedTitles[locale] = iconData.aliases.loc[locale];
}
const universalLocale = locale.slice(
0,
Math.max(0, DEFAULT_LANGUAGE.length),
);
if (
languages.includes(universalLocale) &&
!localizedTitles[universalLocale]
) {
localizedTitles[universalLocale] = iconData.aliases.loc[locale];
}
}
}
return localizedTitles;
};
const simplifyHexIfPossible = (hex) => {
if (hex[0] === hex[1] && hex[2] === hex[3] && hex[4] === hex[5]) {
return `${hex[0]}${hex[2]}${hex[4]}`;
}
return hex;
};
const sitemapUrlForLanguage = (language) => {
const url = `https://simpleicons.org/${language}/`;
return (
`\n <url>\n <loc>${url}</loc>\n` +
` <lastmod>${currentIsoDateString}</lastmod>\n` +
` <changefreq>weekly</changefreq>\n` +
` <xhtml:link rel="alternate" hreflang="${language}" href="${url}"/>\n </url>`
);
};
/**
* Get all the icons that will be removed in the next major versions
* ordered by version.
*/
const getDeprecatedIcons = async () => {
const deprecatedIcons = {};
const siMilestonesCachePath = path.join(
os.tmpdir(),
'simple-icons-milestones.json',
);
let siMilestones;
try {
siMilestones = JSON.parse(await fs.readFile(siMilestonesCachePath, 'utf8'));
} catch {
siMilestones = await githubApi.get(
'/repos/simple-icons/simple-icons/milestones',
);
await fs.writeFile(siMilestonesCachePath, JSON.stringify(siMilestones));
}
for (const milestone of siMilestones) {
const version = milestone.title.replace(/[a-zA-Z]/, '');
let issues;
try {
issues = JSON.parse(
path.join(os.tmpdir(), `simple-icons-milestone-issues-${version}.json`),
);
} catch {
// eslint-disable-next-line no-await-in-loop
issues = await githubApi.get(
`/repos/simple-icons/simple-icons/issues?milestone=${milestone.number}`,
);
// eslint-disable-next-line no-await-in-loop
await fs.writeFile(
path.join(os.tmpdir(), `simple-icons-milestone-issues-${version}.json`),
JSON.stringify(issues),
);
}
for (const issue of issues) {
if (issue.pull_request) {
let changedFiles;
try {
changedFiles = JSON.parse(
path.join(
os.tmpdir(),
`simple-icons-pr-changed-files-${issue.number}.json`,
),
);
} catch {
// eslint-disable-next-line no-await-in-loop
changedFiles = await githubApi.get(`${issue.pull_request.url}/files`);
// eslint-disable-next-line no-await-in-loop
await fs.writeFile(
path.join(
os.tmpdir(),
`simple-icons-pr-changed-files-${issue.number}.json`,
),
JSON.stringify(changedFiles),
);
}
for (const file of changedFiles) {
// eslint-disable-next-line max-depth
if (file.status === 'removed' && file.filename.startsWith('icons/')) {
const slug = file.filename.slice(6, -4);
deprecatedIcons[slug] = {
version,
milestoneNumber: milestone.number,
};
}
}
}
}
}
return deprecatedIcons;
};
let displayIcons = allIcons;
if (process.env.TEST_ENV) {
// Use fewer icons when building for a test run. This significantly speeds up
// page load time and therefor (end-to-end) tests, reducing the chance of
// failed tests due to timeouts.
displayIcons = allIcons.slice(0, 255);
// Ensure that some icons needed by the tests are added
const ensureIconDisplayed = (iconSlug) => {
const iconFound = displayIcons.find((icon) => icon.slug === iconSlug);
if (!iconFound) {
const iconToDisplay = allIcons.find((icon) => icon.slug === iconSlug);
if (!iconToDisplay) {
console.error(`Slug "${iconSlug}" not found in icons`);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
displayIcons.push(iconToDisplay);
}
};
for (const slug of ['adobe', 'aew', 'gotomeeting', 'kinopoisk'])
ensureIconDisplayed(slug);
}
const pageDescription = `${allIcons.length} Free SVG icons for popular brands`;
const pageTitle = 'Simple Icons';
const pageUrl = 'https://simpleicons.org';
const logoUrl = `${pageUrl}/icons/simpleicons.svg`;
const generateStructuredData = async () => {
const getSimpleIconsMembers = async () => {
const siMembersCachePath = path.join(
os.tmpdir(),
'simple-icons-members.json',
);
let siMembersFileContent;
try {
siMembersFileContent = await fs.readFile(siMembersCachePath, 'utf8');
} catch {
const siOrgMembers = await githubApi.get('/orgs/simple-icons/members');
const users = await Promise.all(
siOrgMembers.map(async (member) =>
Object.assign(member, await githubApi.get(`/users/${member.login}`)),
),
);
const structuredDataMembers = users.map((user) => {
return {
'@type': 'Person',
name: user.name,
jobTitle: 'Maintainer',
url: user.html_url,
image: user.avatar_url,
};
});
await fs.writeFile(
siMembersCachePath,
JSON.stringify(structuredDataMembers),
);
return structuredDataMembers;
}
return JSON.parse(siMembersFileContent);
};
return {
'@context': 'http://schema.org',
'@type': 'Organization',
name: pageTitle,
description: pageDescription,
logo: logoUrl,
image: logoUrl,
url: pageUrl,
members: await getSimpleIconsMembers(),
potentialAction: {
'@type': 'SearchAction',
target: `${pageUrl}/?q={search-term}`,
'query-input': 'required name=search-term',
},
};
};
let _translationsUpdated = false;
let i18n;
export default async function webpackConfig(env, argv) {
if (!_translationsUpdated) {
await updateTranslations();
i18n = await loadTranslations();
_translationsUpdated = true;
}
const deprecatedIcons = await getDeprecatedIcons();
const languageNamesObject = await getLanguagesFile();
const languageNamesArray = await getLanguages();
// In test environment only build for a subset of languages
let languages = languageNamesArray.map((lang) => lang[0]);
const languagesFilter = process.env.TEST_ENV
? (lang) => languages.slice(0, 3).includes(lang)
: () => true;
// eslint-disable-next-line unicorn/no-array-callback-reference
languages = languages.filter(languagesFilter);
const nonDefaultLanguages = languages.filter(
(language) => language !== DEFAULT_LANGUAGE,
);
const extensions = await getThirdPartyExtensions();
const structuredData = await generateStructuredData();
const iconsDataBySlugs = await getIconsDataBySlugs();
const icons = displayIcons.map((icon, iconIndex) => {
const luminance = getRelativeLuminance.default(`#${icon.hex}`);
const plainAliases = getIconPlainAliases(iconsDataBySlugs[icon.slug]);
return {
guidelines:
typeof icon.guidelines === 'object'
? icon.guidelines.trademark ?? icon.guidelines.branding
: icon.guidelines,
hex: icon.hex,
indexByAlpha: iconIndex,
indexByColor: sortedHexes.indexOf(icon.hex),
license: icon.license,
source: icon.source,
light: luminance < 0.4,
superLight: luminance > 0.95,
superDark: luminance < 0.02,
path: icon.path,
shortHex: simplifyHexIfPossible(icon.hex),
slug: icon.slug,
title: icon.title,
plainAliases: plainAliases.length > 0 ? plainAliases : false,
localizedTitles: getIconLocalizedTitles(
iconsDataBySlugs[icon.slug],
languages,
),
deprecatedAt:
deprecatedIcons[icon.slug] === undefined
? false
: deprecatedIcons[icon.slug],
};
});
return {
entry: {
app: path.resolve(ROOT_DIR, 'scripts/index.js'),
},
output: {
path: OUT_DIR,
filename: 'script.js',
},
infrastructureLogging: {
// Hide false warning raised by Webpack:
// https://github.com/webpack/webpack/issues/15574
level: 'error',
},
module: {
rules: [
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /\.pug$/i,
use: [
{
loader: 'pug-loader',
options: {
pretty: argv.mode === 'development',
},
},
],
},
{
test: /\.svg$/i,
type: 'asset/inline',
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(
NODE_MODULES,
'simple-icons/_data/simple-icons.json',
),
to: path.resolve(OUT_DIR, 'simple-icons.json'),
},
{
from: path.resolve(NODE_MODULES, 'simple-icons/icons'),
to: path.resolve(OUT_DIR, 'icons'),
filter: (filepath) => filepath.endsWith('.svg'),
},
{
from: path.resolve(ROOT_DIR, 'images'),
to: path.resolve(OUT_DIR, 'images'),
},
{
from: path.resolve(__dirname, 'LICENSE.md'),
to: path.resolve(OUT_DIR, 'license.txt'),
},
{
// Add sitemap.xml
from: path.resolve(ROOT_DIR, 'sitemap.template.xml'),
to: path.resolve(OUT_DIR, 'sitemap.xml'),
transform(content) {
// Inject last modification date in W3C datetime format
return util.format(
content.toString('ascii'),
currentIsoDateString,
nonDefaultLanguages
.map((lang) => sitemapUrlForLanguage(lang))
.join(''),
);
},
},
{
// Add opensearch.xml
from: path.resolve(ROOT_DIR, 'opensearch.xml'),
to: path.resolve(OUT_DIR, 'opensearch.xml'),
},
],
}),
...languages.map((lang) => {
// Add localized title for the icons in the property `localizedTitle`
const currentLangIcons =
lang === DEFAULT_LANGUAGE
? [...icons]
: [...icons].map((icon_) => {
const icon = {...icon_};
if (icon.localizedTitles[lang]) {
icon.localizedTitle = icon.localizedTitles[lang];
}
return icon;
});
return new HtmlWebpackPlugin({
filename:
lang === DEFAULT_LANGUAGE
? 'index.html'
: path.join(lang, 'index.html'),
inject: true,
template: indexPath,
templateParameters: {
extensions,
icons: currentLangIcons,
iconCount: currentLangIcons.length,
pageTitle,
pageUrl,
structuredData,
DEFAULT_LANGUAGE,
t_: i18n(lang),
languageOfTheBuild: lang,
languages,
languageNames: languageNamesObject,
mode: argv.mode,
xIcon,
testing: process.env.TEST_ENV !== undefined,
},
minify:
argv.mode === 'development'
? {}
: {
collapseWhitespace: true,
collapseBooleanAttributes: true,
decodeEntities: true,
removeAttributeQuotes: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
},
});
}),
new MiniCssExtractPlugin(),
],
optimization: {
minimizer:
argv.mode === 'development'
? []
: [
// Load all default minimizers with '...'
'...',
new CssMinimizerPlugin(),
],
},
cache: process.argv.includes('--watch')
? {type: 'memory'}
: {
cacheLocation: path.resolve(
__dirname,
'.cache',
process.argv.includes('development') ? 'webpack-dev' : 'webpack',
),
type: 'filesystem',
version: '1',
},
};
}