1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.validateAndFix = validateAndFix;
|
7 |
|
8 | var _lodash = _interopRequireDefault(require("lodash.isplainobject"));
|
9 |
|
10 | var _lodash2 = _interopRequireDefault(require("lodash.clonedeep"));
|
11 |
|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const essentialPlugins = ["removeDoctype", "removeComments", "removeStyleElement"];
|
18 |
|
19 | function 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();
|
32 |
|
33 | for (const p of essentialPlugins) {
|
34 | state.set(p, false);
|
35 | }
|
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 |
|
43 |
|
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 | }
|
54 |
|
55 |
|
56 | for (const p of essentialPlugins) {
|
57 | if (!state.get(p)) {
|
58 | cleanOpts.plugins.push(p);
|
59 | }
|
60 | }
|
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 |