UNPKG

2.38 kBJavaScriptView Raw
1'use strict';
2
3var svgo = require('svgo');
4var cosmiconfig = require('cosmiconfig');
5
6const explorer = cosmiconfig.cosmiconfigSync("svgo", {
7 searchPlaces: [
8 "package.json",
9 ".svgorc",
10 ".svgorc.js",
11 ".svgorc.json",
12 ".svgorc.yaml",
13 ".svgorc.yml",
14 "svgo.config.js",
15 "svgo.config.cjs",
16 ".svgo.yml"
17 ],
18 transform: (result) => result && result.config,
19 cache: true
20});
21const getSvgoConfigFromSvgrConfig = (config) => {
22 const params = { overrides: {} };
23 if (config.icon || config.dimensions === false) {
24 params.overrides.removeViewBox = false;
25 }
26 if (config.native) {
27 params.overrides.inlineStyles = {
28 onlyMatchedOnce: false
29 };
30 }
31 return {
32 plugins: [
33 {
34 name: "preset-default",
35 params
36 },
37 "prefixIds"
38 ]
39 };
40};
41const getSvgoConfig = (config, state) => {
42 const cwd = state.filePath || process.cwd();
43 if (config.svgoConfig)
44 return config.svgoConfig;
45 if (config.runtimeConfig) {
46 const userConfig = explorer.search(cwd);
47 if (userConfig)
48 return userConfig;
49 }
50 return getSvgoConfigFromSvgrConfig(config);
51};
52
53var __defProp = Object.defineProperty;
54var __defProps = Object.defineProperties;
55var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
56var __getOwnPropSymbols = Object.getOwnPropertySymbols;
57var __hasOwnProp = Object.prototype.hasOwnProperty;
58var __propIsEnum = Object.prototype.propertyIsEnumerable;
59var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
60var __spreadValues = (a, b) => {
61 for (var prop in b || (b = {}))
62 if (__hasOwnProp.call(b, prop))
63 __defNormalProp(a, prop, b[prop]);
64 if (__getOwnPropSymbols)
65 for (var prop of __getOwnPropSymbols(b)) {
66 if (__propIsEnum.call(b, prop))
67 __defNormalProp(a, prop, b[prop]);
68 }
69 return a;
70};
71var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
72const svgoPlugin = (code, config, state) => {
73 if (!config.svgo)
74 return code;
75 const svgoConfig = getSvgoConfig(config, state);
76 const result = svgo.optimize(code, __spreadProps(__spreadValues({}, svgoConfig), { path: state.filePath }));
77 if (result.modernError) {
78 throw result.modernError;
79 }
80 return result.data;
81};
82
83module.exports = svgoPlugin;
84//# sourceMappingURL=index.js.map