This repository has been archived by the owner on May 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-bundle.js
269 lines (243 loc) · 5.94 KB
/
build-bundle.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
const fs = require('fs');
const { blankIconSet, IconSet, SVG } = require('@iconify/tools');
const { getIcons, minifyIconSet } = require('@iconify/utils');
const { paths, mkdir } = require('./lib/files');
const jsSourceDir = paths.sourceAssets + '/js';
const iconSourceDir = paths.sourceAssets + '/svg';
const assetsPath = paths.html + '/assets';
const outputFile = assetsPath + '/bundle.js';
mkdir(assetsPath);
// Custom icons data
const customPrefix = 'assets';
// List of icons to preload
const preloadIcons = {
'line-md': [
// Footer
'home-twotone-alt',
'github-loop',
'document-list',
'document-code',
'image-twotone',
'mastodon',
'discord',
// Breadcrumbs
'chevron-small-double-left',
'chevron-small-double-right',
// Documentation
'hash-small', // Heading link, see replacements.json
'clipboard-arrow', // Copy code, see copy-code.js
'confirm', // Copy success, see copy-code.js
'alert', // Notice, see partials/notices/*.html
'alert-circle', // Notice
'construction-twotone', // Notice in page.html
// Navigation
'menu-unfold-left',
'menu-fold-right',
// Navigation, see src/navigation/render.ts
'chevron-small-down',
'chevron-small-right',
// Code
'alert',
'github-twotone',
'image-twotone',
// partials/process/*, partials/sources/api/namespaces/animation.html
// 'cloud',
'cloud-twotone',
'cloud-down-twotone',
'computer-twotone',
'laptop-twotone',
// partials/sources/api/namespaces/name.html
'navigation-left-up',
// partials/visual-blocks/bundle.html (re-using footer)
'home-twotone-alt',
'github',
'document-list',
'document-code',
'image-twotone',
'chevron-small-double-left',
'alert',
'hash-small',
'cloud-twotone',
'cloud-down-twotone',
'computer-twotone',
'laptop-twotone',
// partials/
],
};
[
// Common
'mdi:alert',
'mdi:home',
// Size demo
'jam:info',
'cil:locomotive',
'cil:paper-plane',
'cil:truck',
'fa-regular:id-badge',
'fa-regular:handshake',
// Color demo
'ion:umbrella-sharp',
'noto:paintbrush',
'bx:bx-home',
'bx:bx-hourglass',
'bx:bx-home',
'entypo:attachment',
// Rotation and flip
'bi:check2-circle',
// Icons page
'mdi:material-design',
'akar-icons:bootstrap-fill',
'carbon:carbon',
'tabler:brand-tabler',
'twemoji:dizzy', // also articles
'openmoji:face-with-tongue',
].forEach((icon) => {
const parts = icon.split(':');
if (parts.length !== 2) {
throw new Error('Use full syntax!');
}
const prefix = parts.shift();
const name = parts.shift();
if (preloadIcons[prefix] === void 0) {
preloadIcons[prefix] = [];
} else if (preloadIcons[prefix].indexOf(name) !== -1) {
return;
}
preloadIcons[prefix].push(name);
});
// Content
const preload = [];
const collections = Object.create(null);
// Get all icons and icon sets
const customCollection = blankIconSet(customPrefix);
// Check for quiet mode
let quiet = false;
process.argv.slice(2).forEach((arg) => {
switch (arg) {
case '--quiet':
quiet = true;
return;
}
});
/**
* Bundle scripts
*/
function loadScripts() {
let content = '';
const webComponent = require.resolve('iconify-icon/dist/iconify-icon.min.js');
content += fs.readFileSync(webComponent, 'utf8') + '\n';
fs.readdirSync(jsSourceDir).forEach((file) => {
const parts = file.split('.');
const ext = parts.pop();
if (ext !== 'js' || parts.length !== 1 || parts[0].slice(0, 1) === '_') {
return;
}
content += fs.readFileSync(jsSourceDir + '/' + file, 'utf8') + '\n';
});
return content;
}
/**
* Load assets and custom icon sets
*/
function loadIconSets() {
fs.readdirSync(iconSourceDir).forEach((file) => {
const parts = file.split('.');
const ext = parts.pop();
if (parts.length !== 1 || parts[0].slice(0, 1) === '_') {
return;
}
const name = parts.shift().replace(/_/g, '-');
switch (ext) {
case 'svg': {
// Custom icon
const content = fs
.readFileSync(iconSourceDir + '/' + file, 'utf8')
.replace(/\s*\n\s*/g, ' ')
.replace(/\s([<>])/g, '$1');
// Add icon
try {
const svg = new SVG(content);
if (!(svg instanceof SVG)) {
throw new Error(`Bad icon: ${file}`);
}
customCollection.fromSVG(name, svg);
if (preloadIcons[customPrefix] === void 0) {
preloadIcons[customPrefix] = [];
}
preloadIcons[customPrefix].push(name);
} catch (err) {
reject(err);
throw new Error(err);
}
break;
}
case 'json': {
// Custom icon set
const content = JSON.parse(
fs.readFileSync(iconSourceDir + '/' + file, 'utf8')
);
const iconSet = new IconSet(content);
if (iconSet.prefix !== name) {
const err = `Bad prefix "${iconSet.prefix}" in JSON file: ${file}`;
reject(err);
throw new Error(err);
}
collections[name] = iconSet;
break;
}
}
});
// Export custom icons
collections[customPrefix] = customCollection;
}
/**
* Load default icon set
*/
function loadDefaultSet(prefix) {
const content = JSON.parse(
fs.readFileSync(
require.resolve('@iconify/json/json/' + prefix + '.json'),
'utf8'
)
);
return new IconSet(content);
}
// Do stuff
loadIconSets();
// Filter icons
Object.keys(preloadIcons).forEach((prefix) => {
let collection;
if (collections[prefix] === void 0) {
collection = loadDefaultSet(prefix);
} else {
collection = collections[prefix];
}
const fullJSON = collection.export();
const json = getIcons(fullJSON, preloadIcons[prefix], true);
if (json.not_found !== void 0) {
throw new Error(
`Could not find icons in "${prefix}": ${json.not_found.join(', ')}`
);
}
minifyIconSet(json);
preload.push(JSON.stringify(json));
});
// Export file
const content =
loadScripts() +
`(function() {
function add(data) {
try {
window.customElements
.get('iconify-icon')
.addCollection(data);
return;
} catch (err) {}
}\n\t` +
preload.map((data) => 'add(' + data + ');').join('\n\t') +
`\n})();
`;
fs.writeFileSync(outputFile, content, 'utf8');
if (!quiet) {
console.log(`Saved bundle.js (${content.length} bytes)`);
}