UNPKG

2.28 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validateAndFix = validateAndFix;
7
8var _lodash = _interopRequireDefault(require("lodash.isplainobject"));
9
10var _lodash2 = _interopRequireDefault(require("lodash.clonedeep"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14// validates svgo opts
15// to contain minimal set of plugins that will strip some stuff
16// for the babylon JSX parser to work
17const essentialPlugins = ["removeDoctype", "removeComments", "removeStyleElement"];
18
19function validateAndFix(opts = {}) {
20 if (!(0, _lodash.default)(opts)) throw new Error("Expected options.svgo to be Object.");
21 let cleanOpts = (0, _lodash2.default)(opts);
22 if (cleanOpts.plugins === void 0) cleanOpts.plugins = [];
23 if (!Array.isArray(cleanOpts.plugins)) throw new Error("Expected options.svgo.plugins to be an array");
24
25 if (cleanOpts.plugins.length === 0) {
26 cleanOpts.plugins = [...essentialPlugins].map(p => ({
27 [p]: true
28 }));
29 }
30
31 const state = new Map(); // mark all essential plugins as disabled
32
33 for (const p of essentialPlugins) {
34 state.set(p, false);
35 } // parse through input plugins and mark enabled ones
36
37
38 for (const plugin of cleanOpts.plugins) {
39 if ((0, _lodash.default)(plugin)) {
40 for (const pluginName of Object.keys(plugin)) {
41 if (essentialPlugins.indexOf(pluginName) > -1) {
42 // enable the plugin in-place if it's an essential plugin
43 // $FlowFixMe: suppressing until refactor (`plugin` is a sealed obj)
44 plugin[pluginName] = true;
45 state.set(pluginName, true);
46 }
47 }
48 } else if (typeof plugin === "string") {
49 state.set(plugin, true);
50 } else {
51 throw new TypeError("Expected SVGO plugin to be of type String or Object. Got " + typeof plugin);
52 }
53 } // add missing plugins
54
55
56 for (const p of essentialPlugins) {
57 if (!state.get(p)) {
58 cleanOpts.plugins.push(p);
59 }
60 } // convert strings to objects to match the form svgo accepts
61
62
63 for (let i = 0; i < cleanOpts.plugins.length; i++) {
64 if (typeof cleanOpts.plugins[i] === "string") {
65 cleanOpts.plugins[i] = {
66 [cleanOpts.plugins[i]]: true
67 };
68 }
69 }
70
71 return cleanOpts;
72}
\No newline at end of file