1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getSvgoConfig = void 0;
|
7 |
|
8 | var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
|
9 |
|
10 | var _svgo = require("svgo");
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 |
|
15 |
|
16 | const cache = {};
|
17 |
|
18 | const loadConfigFromCache = async (configFile, cwd) => {
|
19 | if (configFile !== null) {
|
20 |
|
21 |
|
22 | return (0, _svgo.loadConfig)(configFile, cwd);
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 | if (!(cwd in cache)) {
|
28 |
|
29 |
|
30 | cache[cwd] = await (0, _svgo.loadConfig)(null, cwd);
|
31 | }
|
32 |
|
33 | return cache[cwd];
|
34 | };
|
35 |
|
36 | const getSvgoConfig = async function (options = null, doDeepClone = false) {
|
37 |
|
38 | let config = { ...options
|
39 | };
|
40 |
|
41 | const pluginOptions = {
|
42 | full: Boolean(config.full),
|
43 | configFile: config.configFile || null,
|
44 | cwd: config.cwd || process.cwd()
|
45 | };
|
46 | delete config.full;
|
47 | delete config.configFile;
|
48 | delete config.cwd;
|
49 |
|
50 |
|
51 | if (pluginOptions.full) {
|
52 | return config;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 | const plugins = config.plugins || [];
|
58 | delete config.plugins;
|
59 | const loadedConfig = await loadConfigFromCache(pluginOptions.configFile, pluginOptions.cwd);
|
60 |
|
61 |
|
62 | if (loadedConfig) {
|
63 | config = {
|
64 |
|
65 |
|
66 |
|
67 | ...(doDeepClone ? (0, _lodash.default)(loadedConfig) : loadedConfig),
|
68 | ...config
|
69 | };
|
70 | }
|
71 |
|
72 |
|
73 | if (config.plugins) {
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | config.plugins = extendLoadedPlugins(config.plugins, plugins);
|
80 | } else {
|
81 | const pluginConfig = {
|
82 |
|
83 | name: 'preset-default',
|
84 | params: {
|
85 | overrides: {}
|
86 | }
|
87 | };
|
88 |
|
89 | for (const plugin of plugins) {
|
90 | for (const [key, value] of Object.entries(plugin)) {
|
91 | pluginConfig.params.overrides[key] = value;
|
92 | }
|
93 | }
|
94 |
|
95 | config.plugins = [pluginConfig];
|
96 | }
|
97 |
|
98 | return config;
|
99 | };
|
100 |
|
101 |
|
102 | exports.getSvgoConfig = getSvgoConfig;
|
103 |
|
104 | const extendLoadedPlugins = (loadedPlugins, plugins) => {
|
105 | const pluginsOrder = [];
|
106 | const extendedPlugins = loadedPlugins.map(plugin => {
|
107 | pluginsOrder.push(typeof plugin === 'string' ? plugin : plugin.name);
|
108 | return plugin;
|
109 | });
|
110 |
|
111 | for (const plugin of plugins) {
|
112 | const index = pluginsOrder.indexOf(typeof plugin === 'string' ? plugin : plugin.name);
|
113 |
|
114 | if (index === -1) {
|
115 | extendedPlugins.push(plugin);
|
116 | } else {
|
117 | extendedPlugins[index] = plugin;
|
118 | }
|
119 | }
|
120 |
|
121 | return extendedPlugins;
|
122 | }; |
\ | No newline at end of file |