-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.svgo.js
More file actions
63 lines (52 loc) · 1.93 KB
/
script.svgo.js
File metadata and controls
63 lines (52 loc) · 1.93 KB
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
const fs = require('node:fs');
const path = require('node:path');
const weblog = require('webpack-log');
const SVGO = require('svgo');
const { SvgoCreateConfig } = require('./svgo.config');
const logger = weblog({ name: 'svgo' });
const ENV = require('./app.env');
const UTILS = require('./webpack.utils');
const VERBOSE = ENV.ARGV.verbose;
const imageminIgnore = UTILS.readIgnoreFile('./.imageminignore');
const statMessages = { fixed: 0, skipped: 0, ignored: 0 };
const patterns = [...UTILS.processArgs._];
const files = UTILS.globArraySync(patterns.length > 0 ? patterns : [`${ENV.SOURCE_PATH}/**/*.svg`], {
ignore: [`${ENV.OUTPUT_PATH}/**/*.svg`],
nodir: true,
});
const spritePath = 'img/svg-sprite';
logger.info(`${files.length} files\n`);
files.forEach((resourcePath) => {
const svg = fs.readFileSync(resourcePath, 'utf8').toString();
const relativePath = UTILS.slash(path.relative(ENV.SOURCE_PATH, path.normalize(resourcePath)));
const ignores = imageminIgnore.ignores(relativePath);
if (ignores) {
statMessages.ignored += 1;
if (VERBOSE) {
logger.info(`ignored ${relativePath}`);
}
return;
}
const name = path.basename(resourcePath, path.extname(resourcePath));
const options = SvgoCreateConfig({
sprite: relativePath.startsWith(spritePath),
prefix: `svgo-${name.toLowerCase()}`,
pretty: true,
});
options.path = relativePath;
const result = SVGO.optimize(svg, options);
if (result.error) {
throw new Error(`${JSON.stringify(relativePath)} -- ${result.error}`);
} else if (svg !== result.data) {
fs.writeFileSync(resourcePath, result.data);
statMessages.fixed += 1;
logger.info(`fixed ${relativePath}`);
} else {
statMessages.skipped += 1;
if (VERBOSE) {
logger.info(`skipped ${relativePath}`);
}
}
});
console.log('');
logger.info('stats:', statMessages);