-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.js
110 lines (102 loc) · 2.82 KB
/
optimize.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
'use strict';
const fs = require('fs');
const path = require('path');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const { optimize } = require('svgo');
const input_folder = path.resolve(__dirname, 'svg_raw');
const output_folder = path.resolve(__dirname, 'public/svg');
const config = {
plugins: [
'cleanupAttrs',
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeMetadata',
'removeTitle',
'removeDesc',
'removeUselessDefs',
'removeEditorsNSData',
'removeEmptyAttrs',
'removeHiddenElems',
'removeEmptyText',
'removeEmptyContainers',
'removeViewBox',
'cleanupEnableBackground',
'convertStyleToAttrs',
'convertColors',
'convertPathData',
'convertTransform',
'removeUnknownsAndDefaults',
'removeNonInheritableGroupAttrs',
'removeUselessStrokeAndFill',
'removeUnusedNS',
'cleanupIDs',
'cleanupNumericValues',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'collapseGroups',
// 'removeRasterImages',
// 'mergePaths',
'convertShapeToPath',
'sortAttrs',
'removeDimensions',
{
name: 'removeAttrs',
params: { attrs: '(stroke|fill-rule|stroke-width)' },
},
],
};
const readFiles = async (dirname) => {
try {
const filenames = await (await readdir(dirname)).filter(
(f) => f.indexOf('.svg') > -1
);
console.log({ filenames });
const files_promise = filenames.map((filename) => {
return readFile(`${dirname}/${filename}`, 'utf-8');
});
const response = await Promise.all(files_promise);
return filenames.map((filename, currentIndex) => {
const content = response[currentIndex];
return {
filename,
content,
};
});
} catch (error) {
console.error(error);
}
};
const main = async () => {
const response = await readFiles(input_folder);
// console.log(response);
const opt_results = response.map((item) => {
const { filename, content } = item;
const source = `${input_folder}/${filename}`;
// return writeFile(
// dest,
// optimize(content, { path: dest, ...config }),
// 'utf-8'
// );
return optimize(content, { path: source, ...config });
});
console.log(opt_results.length);
const promises = response.map((item, index) => {
const { filename } = item;
const dest = `${output_folder}/${filename}`;
const optimized = opt_results[index];
return writeFile(dest, optimized.data, 'utf-8');
});
await Promise.all(promises);
};
// FS.readFile(filepath, 'utf8', function (err, data) {
// if (err) {
// throw err;
// }
// const result = optimize(data, { path: filepath, ...config });
// console.log(result);
// });
(() => main())();