This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.cjs
274 lines (245 loc) · 8.94 KB
/
gulpfile.cjs
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
//
// ================================================================================
// @ SETTINGS
// Turn different build features on/off
// ================================================================================
const settings = {
clean: true, // Turn on/off clean tasks
svgs: true, // Turn on/off SVG tasks
build: true, // Turn on/off build tasks
exportIcons: true, // Turn on/off export icons tasks
iconList: true, // Turn on/off icon list tasks
};
// ================================================================================
// @ PACKAGES
// What makes everything go
// ================================================================================
// @@ GENERAL
const { src, dest, series } = require('gulp');
const del = require('del');
const rename = require('gulp-rename');
const through2 = require('through2');
const fs = require("fs");
const jsonFormat = require('gulp-json-format');
// @@ SVGS
const path = settings.svgs ? require('path') : null;
const svgmin = settings.svgs ? require('gulp-svgmin') : null;
const replace = settings.svgs ? require('gulp-replace') : null;
// ================================================================================
// @ PATHS
// Where everything is in this project
// ================================================================================
const paths = {
clean: {
svgs: './dist/svg/**/*.svg',
vueIcons: './src/icons/*.vue',
},
icons: {
input: './src/svg/**/*.svg',
outputSvg: './dist/svg/',
outputVue: './src/icons/',
},
exports: {
vueIcons: './src/icons/',
index: './src/icons.js',
iconsList: './src/icons.json',
},
templates: {
baseIcon: './src/baseIconTemplate.vue',
}
};
// ====
// Util Functions
// ====
const _getAllFiles = (dirPath, arrayOfFiles = []) => {
const files = fs.readdirSync(dirPath)
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = _getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))
}
})
return arrayOfFiles
}
// ================================================================================
// @ TASKS
// Where everything happens
// ================================================================================
// @@ CLEAN UP
// ================================================================================
// -- Function to clean out folders / files
const cleanUp = (items) => {
// Make sure the feature is active before running
if (!settings.clean) return;
// Clean dist folders
return Promise.all([
del.sync(items),
]);
};
// -- Clean out SVGs
const cleanSVGs = () => {
return cleanUp([paths.clean.svgs]);
};
// -- Clean out Vue icons
const cleanVueIcons = () => {
return cleanUp([paths.clean.vueIcons]);
};
// ================================================================================
// @@ COMPILE CSS
// Lint, minify, and concatenate style files
// ================================================================================
const moveStyleTagsToEOF = function (file, enc, cb) {
if (!file.isBuffer()) return cb(null, file);
const styleTagsRegex = /<style[\s\S]*<\/style>/gmi;
let content = file.contents.toString();
const result = styleTagsRegex.exec(content);
if (!result) return cb(null, file);
const matchedText = result[0];
content = content.replace(styleTagsRegex, '');
content = content + matchedText;
file.contents = Buffer.from(content);
return cb(null, file);
};
const replaceTemplateContent = function (file, enc, cb) {
if (!file.isBuffer()) return cb(null, file);
const template = fs.readFileSync(paths.templates.baseIcon).toString();
let content = file.contents.toString();
content = template.replace('/*SVG-CONTENT*/', content);
file.contents = Buffer.from(content);
return cb(null, file);
};
// ================================================================================
// @@ COMPILE SVGS
// Lint and optimize SVG files
// ================================================================================
const buildIcons = function (done) {
// Make sure this feature is activated before running
if (!settings.svgs) return done();
// Compile icons
return src(paths.icons.input)
.pipe(replace(' fill="none"', ''))
.pipe(replace(' fill="#000"', ' fill="currentColor"'))
.pipe(replace(' fill="#000000"', ' fill="currentColor"'))
.pipe(replace(' fill="black"', ' fill="currentColor"'))
.pipe(replace(' fill="#0D0C0F"', ' fill="currentColor"'))
.pipe(replace(' fill="#222"', ' fill="currentColor"'))
.pipe(replace(' fill="#222222"', ' fill="currentColor"'))
.pipe(replace(/<svg.*(width="[0-9]+|height="[0-9]+)"/g, (match) => {
return match
.replace(/width="[0-9]+"/, '')
.replace(/height="[0-9]+"/, '');
}))
.pipe(replace('<svg', function (match) {
const name = path.parse(this.file.path).name;
const title = name
.replace(/\b\S/g, t => t.toUpperCase())
.replace(/-+/g, ' ');
return `${match}
aria-hidden="true"
focusable="false"
role="img"
data-name="${title}"
class="d-icon d-icon--${name}"`;
}))
.pipe(svgmin(function getOptions () {
return {
multipass: true,
plugins: [
{
removeUnknownsAndDefaults: {
keepRoleAttr: true,
},
},
{
cleanupIDs: {
minify: true,
},
}
],
}
}))
.pipe(rename({ dirname: '' }))
.pipe(dest(paths.icons.outputSvg))
.pipe(replace(/(clip-path|fill|mask|filter)="url\(#[a-z]\)"/g, (match) => {
return ':'+ match
.replace(/url\(#[a-z]\)/, (match) => `\`${match}\``)
.replace(/#[a-z]/, (id) => '#${uniqueID}__' + id.charAt(1))
}))
.pipe(replace(/id="[a-z]"/g, (match) => {
const oldID = /"[a-z]"/.exec(match)[0].charAt(1);
return `:id="\`\${uniqueID}__${oldID}\`"`;
}))
// replace the SVG content into the vue template
.pipe(through2.obj(replaceTemplateContent))
// move any style tags within the svg into style tags of the vue component
.pipe(through2.obj(moveStyleTagsToEOF))
.pipe(replace('<style>', '<style scoped>'))
.pipe(rename(function (file) {
file.basename = file.basename
.replace(/\b\S/g, t => t.toUpperCase())
.replace(/-+/g, '');
file.extname = '.vue';
}))
.pipe(dest(paths.icons.outputVue));
};
// ================================================================================
// @@ Build the main.js file
// Read the vue icon files and export them all
// ================================================================================
const exportIcons = async function (done) {
if (!settings.exportIcons) return done();
const iconsList = fs
.readdirSync(paths.exports.vueIcons)
.map(icon => {
const iconName = icon.split('.')[0];
return `export { default as ${iconName} } from './icons/${icon}'`;
});
await fs.writeFileSync('./src/main.js', iconsList.join('\n'));
return done();
};
// ================================================================================
// @@ Copy icons.json to dist
// ================================================================================
const copyFiles = function (done) {
src(paths.exports.iconsList)
.pipe(dest('./dist'));
return done();
};
// ================================================================================
// @@ Updates icons.json file with any newly added icons
// Reads previous icons.json file to extract keywords and add any new icon
// into the respective category.
// ================================================================================
const updateIconsJSON = function (done) {
const rawData = fs.readFileSync(paths.exports.iconsList)
const iconsJSON = JSON.parse(rawData).categories;
const result = _getAllFiles('./src/svg')
.filter(file => file.endsWith('.svg'))
.reduce((acc, file) => {
const [category, fileName] = file.split('/').slice(-2);
const iconName = fileName.replace('.svg', '');
const keywords = iconsJSON[category][iconName] || [];
acc[category] = {...acc[category], [iconName]: keywords}
return acc;
}, {});
fs.writeFileSync(paths.exports.iconsList, JSON.stringify({ categories: {...result}}));
// Prettifies the JSON to improve readability and easier keyword adding.
src(paths.exports.iconsList)
.pipe(jsonFormat(2))
.pipe(dest('./src/'));
return done();
};
// ================================================================================
// @ EXPORT TASKS
// ================================================================================
// -- CLEAN, BUILD, EXPORT THE ICONS AND COPY THE FILES
// default build task
exports.default = series(
cleanSVGs,
cleanVueIcons,
buildIcons,
exportIcons,
updateIconsJSON,
copyFiles,
);