-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathloader.svgo.js
More file actions
35 lines (27 loc) · 1.25 KB
/
loader.svgo.js
File metadata and controls
35 lines (27 loc) · 1.25 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
const path = require('node:path');
const weblog = require('webpack-log');
const SVGO = require('svgo');
const { SvgoCreateConfig } = require('./svgo.config');
const UTILS = require('./webpack.utils');
const logger = weblog({ name: 'loader-svgo' });
module.exports = function SvgLoader(content) {
const loaderContext = this;
if (loaderContext.cacheable) loaderContext.cacheable();
const loaderCallback = loaderContext.async();
const name = path.basename(loaderContext.resourcePath, '.svg');
const options = SvgoCreateConfig({ prefix: `svgo-${name.toLowerCase()}`, pretty: true });
const relativePath = UTILS.slash(path.relative(__dirname, loaderContext.resourcePath));
options.path = relativePath;
if (options.verbose) {
logger.info(`optimize(${JSON.stringify(relativePath)})`);
}
const result = SVGO.optimize(content, options);
if (result.error) {
loaderCallback(`In ${JSON.stringify(relativePath)} -- ${result.error}`);
} else {
const prefix = `<!-- ${JSON.stringify(relativePath)} -->\n`;
const suffix = `\n<!-- /${JSON.stringify(relativePath)} -->\n`;
loaderCallback(null, `module.exports = ${JSON.stringify(prefix + result.data.trim() + suffix)}`);
}
};
module.exports.raw = true;