This repository has been archived by the owner on Sep 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
75 lines (71 loc) · 2.01 KB
/
mod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
defaultPlugins,
extendDefaultPlugins,
resolvePluginConfig,
} from "./lib/svgo/config.js";
import svg2js from "./lib/svgo/svg2js.js";
import js2svg from "./lib/svgo/js2svg.js";
import invokePlugins from "./lib/svgo/plugins.js";
import JSAPI from "./lib/svgo/jsAPI.js";
import { encodeSVGDatauri } from "./lib/svgo/tools.js";
export { extendDefaultPlugins };
export function optimize(input, config) {
if (config == null) {
config = {};
}
if (typeof config !== "object") {
throw Error("Config should be an object");
}
const maxPassCount = config.multipass ? 10 : 1;
let prevResultSize = Number.POSITIVE_INFINITY;
let svgjs = null;
const info = {};
if (config.path != null) {
info.path = config.path;
}
for (let i = 0; i < maxPassCount; i += 1) {
info.multipassCount = i;
svgjs = svg2js(input);
if (svgjs.error != null) {
if (config.path != null) {
svgjs.path = config.path;
}
return svgjs;
}
const plugins = config.plugins || defaultPlugins;
if (Array.isArray(plugins) === false) {
throw Error(
"Invalid plugins list. Provided 'plugins' in config should be an array.",
);
}
const resolvedPlugins = plugins.map((plugin) =>
resolvePluginConfig(plugin, config)
);
svgjs = invokePlugins(svgjs, info, resolvedPlugins);
svgjs = js2svg(svgjs, config.js2svg);
if (svgjs.error) {
throw Error(svgjs.error);
}
if (svgjs.data.length < prevResultSize) {
input = svgjs.data;
prevResultSize = svgjs.data.length;
} else {
if (config.datauri) {
svgjs.data = encodeSVGDatauri(svgjs.data, config.datauri);
}
if (config.path != null) {
svgjs.path = config.path;
}
return svgjs;
}
}
return svgjs;
} /**
* The factory that creates a content item with the helper methods.
*
* @param {Object} data which is passed to jsAPI constructor
* @returns {JSAPI} content item
*/
export function createContentItem(data) {
return new JSAPI(data);
}