-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack.config.js
132 lines (125 loc) · 4 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
const CopyPlugin = require('copy-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const getRelativeLuminance = require('get-relative-luminance').default;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const simpleIcons = require('simple-icons');
const { normalizeSearchTerm } = require('./public/scripts/utils.js');
const sortByColors = require('./scripts/color-sorting.js');
const icons = Object.values(simpleIcons);
const sortedHexes = sortByColors(icons.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');
function simplifyHexIfPossible(hex) {
if (hex[0] === hex[1] && hex[2] === hex[3] && hex[4] == hex[5]) {
return `${hex[0]}${hex[2]}${hex[4]}`;
}
return hex;
}
let displayIcons = icons;
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 = icons.slice(0, 255);
}
module.exports = (env, argv) => {
return {
entry: {
app: path.resolve(ROOT_DIR, 'scripts/index.js'),
},
output: {
path: OUT_DIR,
filename: 'script.js',
},
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(ROOT_DIR, 'images'),
to: path.resolve(OUT_DIR, 'images'),
},
],
}),
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(ROOT_DIR, 'index.pug'),
templateParameters: {
icons: displayIcons.map((icon, iconIndex) => {
const luminance = getRelativeLuminance(`#${icon.hex}`);
return {
base64Svg: Buffer.from(icon.svg).toString('base64'),
guidelines: icon.guidelines,
hex: icon.hex,
indexByAlpha: iconIndex,
indexByColor: sortedHexes.indexOf(icon.hex),
license: icon.license,
light: luminance < 0.4,
superLight: luminance > 0.55,
superDark: luminance < 0.02,
normalizedName: normalizeSearchTerm(icon.title),
path: icon.path,
shortHex: simplifyHexIfPossible(icon.hex),
slug: icon.slug,
title: icon.title,
badgeEncodedTitle: encodeURIComponent(
icon.title.replaceAll('-', '--').replaceAll('_', '__'),
),
};
}),
iconCount: icons.length,
twitterIcon: icons.find((icon) => icon.title === 'X'),
pageTitle: 'Simple Badges',
pageDescription: `${icons.length} Awesome Simple Icons on your favorite Shields.io Badges.`,
pageUrl: 'https://badges.pages.dev',
},
}),
new MiniCssExtractPlugin(),
],
optimization: {
minimizer:
argv.mode === 'development'
? []
: [
'...', // <- Load all default minimizers
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',
},
};
};