-
Notifications
You must be signed in to change notification settings - Fork 25
/
webpack.config.js
120 lines (112 loc) · 3.26 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
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 simpleIconsData = require('simple-icons/_data/simple-icons.json');
const sortColors = require('color-sorter').sortFn;
const { normalizeSearchTerm } = require('./public/scripts/utils.js');
const icons = Object.values(simpleIcons);
const sortedHexes = icons
.map((icon) => icon.hex)
.filter((hex, index, array) => array.indexOf(hex) === index)
.sort(sortColors);
const NODE_MODULES = path.resolve(__dirname, 'node_modules');
const OUT_DIR = path.resolve(__dirname, '_site');
const ROOT_DIR = path.resolve(__dirname);
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;
}
function getGuidelinesFor(title) {
let result;
simpleIconsData.icons.forEach((icon) => {
if (icon.title !== title) {
return;
}
if (icon.guidelines) {
result = icon.guidelines;
}
});
return result;
}
module.exports = {
entry: {
app: './public/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: ['pug-loader'],
},
{
test: /\.svg$/i,
use: ['svg-url-loader'],
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
from: path.resolve(NODE_MODULES, 'simple-icons/icons'),
to: path.resolve(OUT_DIR, 'icons'),
filter: (path) => path.endsWith('.svg'),
},
{
from: path.resolve(NODE_MODULES, 'simple-icons-pdf/icons'),
to: path.resolve(OUT_DIR, 'icons'),
filter: (path) => path.endsWith('.pdf'),
},
{
from: path.resolve(ROOT_DIR, 'public/images'),
to: path.resolve(OUT_DIR, 'images'),
},
],
}),
new HtmlWebpackPlugin({
inject: true,
template: path.resolve(ROOT_DIR, 'public/index.pug'),
templateParameters: {
icons: icons.map((icon, iconIndex) => {
const luminance = getRelativeLuminance(`#${icon.hex}`);
return {
guidelines: getGuidelinesFor(icon.title),
hex: icon.hex,
indexByAlpha: iconIndex,
indexByColor: sortedHexes.indexOf(icon.hex),
light: luminance < 0.4,
superLight: luminance > 0.95,
superDark: luminance < 0.02,
normalizedName: normalizeSearchTerm(icon.title),
path: icon.path,
shortHex: simplifyHexIfPossible(icon.hex),
slug: icon.slug,
title: icon.title,
};
}),
iconCount: icons.length,
},
}),
new MiniCssExtractPlugin(),
],
optimization: {
minimizer: [
'...', // <- Load all default minimizers
new CssMinimizerPlugin(),
],
},
};