UNPKG

4.04 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getSvgoConfig = void 0;
7
8var _lodash = _interopRequireDefault(require("lodash.clonedeep"));
9
10var _svgo = require("svgo");
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14// To prevent multiple scans of the disk for a svgo.config.js file, keep its
15// data in module scope.
16const cache = {}; // Load the config from svgo.config.js.
17
18const loadConfigFromCache = async (configFile, cwd) => {
19 if (configFile !== null) {
20 // Look for the config in the specified file. loadConfig() will
21 // require() the file, which caches it for us.
22 return (0, _svgo.loadConfig)(configFile, cwd);
23 } // Since no configFile was given, let loadConfig() find a file for us.
24 // If the config file is not in our module cache, look for it on disk.
25
26
27 if (!(cwd in cache)) {
28 // Any usage of loadConfig() with the same cwd will return the same
29 // file's config. Store the resulting config in our module cache.
30 cache[cwd] = await (0, _svgo.loadConfig)(null, cwd);
31 }
32
33 return cache[cwd];
34};
35
36const getSvgoConfig = async function (options = null, doDeepClone = false) {
37 // Construct the svgo config from the given options.
38 let config = { ...options
39 }; // Get the options that are for this gulp plugin and not for svgo.
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; // If the options.full flag is specified, then the given options are
49 // considered to be the full svgo config.
50
51 if (pluginOptions.full) {
52 return config;
53 } // Extract the svgo plugins list from the config as we will need to handle
54 // them specially later.
55
56
57 const plugins = config.plugins || [];
58 delete config.plugins;
59 const loadedConfig = await loadConfigFromCache(pluginOptions.configFile, pluginOptions.cwd); // Merge the given config with the config loaded from file. (If no
60 // config file was found, svgo's loadConfig() returns null.)
61
62 if (loadedConfig) {
63 config = { // Since gulp-svgmin allows a function to modify config per-file, we
64 // want to prevent that function from making modifications to the
65 // returned config object that would bleed into subsequent usages of
66 // the config object.
67 ...(doDeepClone ? (0, _lodash.default)(loadedConfig) : loadedConfig),
68 ...config
69 };
70 } // Merge any plugins given in options.plugins.
71
72
73 if (config.plugins) {
74 // If plugins are provided in a config file, they are assumed to be
75 // a final list of plugins; according to svgo version 2 docs, the
76 // config file is responsible for merging the default plugins list.
77 // So we just need to merge the options.plugins into the list loaded
78 // from the config file.
79 config.plugins = extendLoadedPlugins(config.plugins, plugins);
80 } else {
81 const pluginConfig = {
82 // Default provided per svgo docs in v2.4.0+
83 name: 'preset-default',
84 params: {
85 overrides: {}
86 }
87 }; // Following format assuming plugins settings are for built ins
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}; // Based on svgo's extendDefaultPlugins().
100
101
102exports.getSvgoConfig = getSvgoConfig;
103
104const 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