svg-icon-baker 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,52 +2,55 @@
2
2
 
3
3
  > Bake the `svg` icon into `symbol` 🍪
4
4
 
5
- The core library for transforming SVG icons into optimized SVG symbol sprite.
5
+ The core library for transforming SVG icons into optimized SVG symbol sprites.
6
6
 
7
7
  If you like this project, please give it a [Star](https://github.com/yangxu52/svg-icon-baker).
8
8
 
9
9
  ## Usage
10
10
 
11
- ```js
12
- import { bakeIcon } from 'svg-icon-baker'
11
+ ```ts
12
+ import { bakeIcon, bakeIcons } from 'svg-icon-baker'
13
+
14
+ const source = { name: 'home', content: '<svg viewBox="0 0 16 16">...</svg>' }
15
+ const result = bakeIcon(source)
16
+ // result: { name: 'home', content: '<symbol id="home" viewBox="0 0 16 16">...</symbol>' }
13
17
 
14
- const source = { name: 'home', content: '<svg>...</svg>' }
15
- const result = await bakeIcon(source)
16
- // result: { name: 'home', symbol: '<symbol>...</symbol>' }
18
+ const results = bakeIcons([source])
17
19
  ```
18
20
 
21
+ `bakeIcon` and `bakeIcons` are synchronous.
22
+
19
23
  ## API
20
24
 
21
25
  ### `bakeIcon(source: BakeSource, options?: Options): BakeResult`
22
26
 
23
- convert SVG into symbols.
27
+ Convert one SVG into one symbol result.
24
28
 
25
29
  ### `bakeIcons(sources: BakeSource[], options?: Options): BakeResult[]`
26
30
 
27
- batch convert SVG into symbols.
31
+ Convert multiple SVG inputs with one inferred option set.
28
32
 
29
33
  ## Options
30
34
 
31
- | name | type | default | description |
32
- | ------------------------- | --------- | ------- | -------------------------------------------------- |
33
- | `defaultPreset` | `boolean` | `true` | Enable SVGO `preset-default`. |
34
- | `convertOneStopGradients` | `boolean` | `false` | Convert one-stop gradients. |
35
- | `convertStyleToAttrs` | `boolean` | `false` | Convert style blocks to attributes. |
36
- | `reusePaths` | `boolean` | `false` | Try to reuse identical paths. |
37
- | `removeScripts` | `boolean` | `false` | Drop `<script>` for safety. |
38
- | `removeTitle` | `boolean` | `true` | Remove `<title>` elements from symbols. |
39
- | `removeXMLNS` | `boolean` | `true` | Remove xmlns on root (not needed inside sprite). |
40
- | `removeXlink` | `boolean` | `true` | Remove xlink namespace (modern browsers use href). |
35
+ | name | type | default | description |
36
+ | ------------- | ------------- | ------- | ------------------------------------------------ |
37
+ | `optimize` | `boolean` | `true` | Enable default safe SVGO optimization preset. |
38
+ | `svgoOptions` | `SvgoOptions` | `{}` | Custom SVGO options into the optimization layer. |
41
39
 
42
40
  Notes:
43
41
 
44
- - set `true` to enable default optimizations. set `false` to disable all optimizations.
45
- - The library prefixes internal ids and URL references via SVGO `prefixIds`, using the icon name as prefix (e.g., `name-xxxxx`).
46
- - Width/height on the root `<svg>` are removed; viewBox is required or inferred by SVGO from width/height when possible.
42
+ - Behavior matrix:
43
+ - `optimize: true` + no `svgoOptions`: run default safe optimization.
44
+ - `optimize: true` + `svgoOptions`: run default safe optimization, then merge custom options.
45
+ - `optimize: false` + no `svgoOptions`: skip optimization layer.
46
+ - `optimize: false` + `svgoOptions`: run custom optimization settings only.
47
+ - When `svgoOptions.plugins` is provided, custom plugins run after default safe optimization plugins.
47
48
 
48
49
  ## Type Definitions
49
50
 
50
51
  ```ts
52
+ import type { Config } from 'svgo'
53
+
51
54
  type BakeSource = {
52
55
  name: string
53
56
  content: string
@@ -55,28 +58,22 @@ type BakeSource = {
55
58
 
56
59
  type BakeResult = {
57
60
  name: string
58
- symbol: string
61
+ content: string
59
62
  }
60
63
 
61
- type Options =
62
- | {
63
- defaultPreset?: boolean
64
- convertOneStopGradients?: boolean
65
- convertStyleToAttrs?: boolean
66
- reusePaths?: boolean
67
- removeScripts?: boolean
68
- removeTitle?: boolean
69
- removeXMLNS?: boolean
70
- removeXlink?: boolean
71
- }
72
- | boolean
64
+ type SvgoOptions = Pick<Config, 'multipass' | 'floatPrecision' | 'js2svg' | 'plugins'>
65
+
66
+ type Options = {
67
+ optimize?: boolean
68
+ svgoOptions?: SvgoOptions
69
+ }
73
70
  ```
74
71
 
75
72
  ## Features
76
73
 
77
- - 🎯 SVG Optimization - Remove redundant data and optimize paths
78
- - 🔗 Reference Handling - Properly namespace IDs and class names
79
- - 🎨 ViewBox Management - Ensure consistent sizing
74
+ - 🎯 Optimization: Reduce file size, and improve efficiency through `SVGO`
75
+ - 🔗 Reference Handling: ID and reference prefixing for sprite safety
76
+ - 🎨 Size Unify: `viewBox` preservation or inference from root dimensions
80
77
 
81
78
  ## License
82
79
 
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Config } from 'svgo';
2
+
1
3
  type BakeSource = {
2
4
  name: string;
3
5
  content: string;
@@ -6,52 +8,21 @@ type BakeResult = {
6
8
  name: string;
7
9
  content: string;
8
10
  };
9
- type ManualOptions = {
10
- /**
11
- * default plugin preset
12
- * @default true
13
- */
14
- defaultPreset?: boolean;
15
- /**
16
- * convert one stop gradients to inline styles
17
- * @default false
18
- */
19
- convertOneStopGradients?: boolean;
20
- /**
21
- * convert style to attrs
22
- * @default false
23
- */
24
- convertStyleToAttrs?: boolean;
11
+ type SvgoOptions = Pick<Config, 'multipass' | 'floatPrecision' | 'js2svg' | 'plugins'>;
12
+ type Options = {
25
13
  /**
26
- * reuse paths
27
- * @default false
28
- */
29
- reusePaths?: boolean;
30
- /**
31
- * remove scripts
32
- * @default false
33
- */
34
- removeScripts?: boolean;
35
- /**
36
- * remove title
14
+ * enable/disable default safe optimization preset
37
15
  * @default true
38
16
  */
39
- removeTitle?: boolean;
17
+ optimize?: boolean;
40
18
  /**
41
- * remove xmlns
42
- * @default true
43
- */
44
- removeXMLNS?: boolean;
45
- /**
46
- * remove xlink
47
- * @default true
19
+ * custom svgo options merged into optimizer
48
20
  */
49
- removeXlink?: boolean;
21
+ svgoOptions?: SvgoOptions;
50
22
  };
51
- type Options = boolean | ManualOptions;
52
23
 
53
24
  declare function bakeIcon(source: BakeSource, options?: Options): BakeResult;
54
25
  declare function bakeIcons(sources: BakeSource[], options?: Options): BakeResult[];
55
26
 
56
27
  export { bakeIcon, bakeIcons };
57
- export type { BakeResult, BakeSource, Options };
28
+ export type { BakeResult, BakeSource, Options, SvgoOptions };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Config } from 'svgo';
2
+
1
3
  type BakeSource = {
2
4
  name: string;
3
5
  content: string;
@@ -6,52 +8,21 @@ type BakeResult = {
6
8
  name: string;
7
9
  content: string;
8
10
  };
9
- type ManualOptions = {
10
- /**
11
- * default plugin preset
12
- * @default true
13
- */
14
- defaultPreset?: boolean;
15
- /**
16
- * convert one stop gradients to inline styles
17
- * @default false
18
- */
19
- convertOneStopGradients?: boolean;
20
- /**
21
- * convert style to attrs
22
- * @default false
23
- */
24
- convertStyleToAttrs?: boolean;
11
+ type SvgoOptions = Pick<Config, 'multipass' | 'floatPrecision' | 'js2svg' | 'plugins'>;
12
+ type Options = {
25
13
  /**
26
- * reuse paths
27
- * @default false
28
- */
29
- reusePaths?: boolean;
30
- /**
31
- * remove scripts
32
- * @default false
33
- */
34
- removeScripts?: boolean;
35
- /**
36
- * remove title
14
+ * enable/disable default safe optimization preset
37
15
  * @default true
38
16
  */
39
- removeTitle?: boolean;
17
+ optimize?: boolean;
40
18
  /**
41
- * remove xmlns
42
- * @default true
43
- */
44
- removeXMLNS?: boolean;
45
- /**
46
- * remove xlink
47
- * @default true
19
+ * custom svgo options merged into optimizer
48
20
  */
49
- removeXlink?: boolean;
21
+ svgoOptions?: SvgoOptions;
50
22
  };
51
- type Options = boolean | ManualOptions;
52
23
 
53
24
  declare function bakeIcon(source: BakeSource, options?: Options): BakeResult;
54
25
  declare function bakeIcons(sources: BakeSource[], options?: Options): BakeResult[];
55
26
 
56
27
  export { bakeIcon, bakeIcons };
57
- export type { BakeResult, BakeSource, Options };
28
+ export type { BakeResult, BakeSource, Options, SvgoOptions };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{optimize as m}from"svgo";function l(e,r){const t=a(r),n=i(t);return{name:e.name,content:o(e,n)}}function u(e,r){const t=a(r),n=i(t);return e.map(s=>({name:s.name,content:o(s,n)}))}function o(e,r){if(!e||!e.name||!e.content)throw new TypeError("Property name and content are required.");if(!/^[A-Za-z][A-Za-z0-9_-]*$/.test(e.name))throw new TypeError("Invalid name. Use letters, numbers, dash, or underscore, starting with a letter.");r.push({name:"prefixIds",params:{prefix:`${e.name}-`,delim:""}});let t;try{t=m(e.content,{plugins:r})}catch(s){throw new Error(`Parsing failed. ${String(s)}`)}const n=t.data.match(/viewBox="([^"]+)"/)?.[1];if(!n)throw new Error("Cannot determine viewBox. Provide an SVG with viewBox or width/height attributes.");return t.data.replace(/^\s*<\?xml[^>]*\?>\s*/i,"").replace(/^\s*<svg\b[^>]*>/i,`<symbol id="${e.name}" viewBox="${n}">`).replace(/<\/svg>\s*$/i,"</symbol>").trim()}function a(e){const r={defaultPreset:!0,convertOneStopGradients:!1,convertStyleToAttrs:!1,reusePaths:!1,removeScripts:!1,removeTitle:!0,removeXMLNS:!0,removeXlink:!0};return typeof e=="boolean"?e?r:{defaultPreset:!1,convertOneStopGradients:!1,convertStyleToAttrs:!1,reusePaths:!1,removeScripts:!1,removeTitle:!1,removeXMLNS:!1,removeXlink:!1}:{...r,...e||{}}}function i(e){const r=[];return e.defaultPreset&&r.push({name:"preset-default",params:{overrides:{removeUselessDefs:!1,removeHiddenElems:!1,removeUnknownsAndDefaults:!1,collapseGroups:!1,mergePaths:!1,convertShapeToPath:!1}}}),e.convertOneStopGradients&&r.push({name:"convertOneStopGradients"}),e.convertStyleToAttrs&&r.push({name:"convertStyleToAttrs"}),e.reusePaths&&r.push({name:"reusePaths"}),e.removeScripts&&r.push({name:"removeScripts"}),e.removeTitle&&r.push({name:"removeTitle"}),e.removeXMLNS&&r.push({name:"removeXMLNS"}),e.removeXlink&&r.push({name:"removeXlink"}),r.push({name:"removeDimensions"}),r}export{l as bakeIcon,u as bakeIcons};
1
+ import{optimize as m}from"svgo";const p={name:"preset-default",params:{overrides:{removeUselessDefs:!1,removeHiddenElems:!1,removeUnknownsAndDefaults:!1,collapseGroups:!1,mergePaths:!1,convertShapeToPath:!1}}},l=[{name:"removeTitle"},{name:"removeXMLNS"},{name:"removeXlink"}],c=new Set(["prefixIds"]);function i(e){const n=e??{};return{optimize:n.optimize??!0,svgoOptions:n.svgoOptions??{}}}function u(e,n){const t=[];return n.optimize&&(t.push(p),t.push(...l)),n.svgoOptions.plugins!=null&&t.push(...f(n.svgoOptions.plugins)),t.push(...g(e)),{multipass:n.svgoOptions.multipass,floatPrecision:n.svgoOptions.floatPrecision,js2svg:n.svgoOptions.js2svg,plugins:t}}function f(e){return e.filter(n=>{const t=v(n);return t==null?!0:!c.has(t)})}function v(e){return typeof e=="string"?e:e&&typeof e=="object"&&"name"in e&&typeof e.name=="string"?e.name:null}function g(e){return[{name:"removeDimensions"},{name:"prefixIds",params:{prefix:`${e}-`,delim:""}}]}function h(e,n){const t=i(n);return{name:e.name,content:a(e,t)}}function d(e,n){const t=i(n);return e.map(r=>({name:r.name,content:a(r,t)}))}function a(e,n){if(!e||!e.name||!e.content)throw new TypeError("Property name and content are required.");if(!/^[A-Za-z][A-Za-z0-9_-]*$/.test(e.name))throw new TypeError("Invalid name. Use letters, numbers, dash, or underscore, starting with a letter.");let t;try{t=m(e.content,u(e.name,n))}catch(s){throw new Error(`Parsing failed. ${String(s)}`)}const r=t.data.match(/viewBox="([^"]+)"/)?.[1];if(!r)throw new Error("Cannot determine viewBox. Provide an SVG with viewBox or width/height attributes.");const o=t.data.replace(/^\s*<\?xml[^>]*\?>\s*/i,"");return w(o,e.name,r)}function w(e,n,t){const r=e.match(/^\s*<svg\b[^>]*>/i)[0].replace(/^\s*<svg\b/i,"").replace(/>\s*$/i,"").replace(/\s+id=(['"])[^'"]*\1/gi,"").replace(/\s+viewBox=(['"])[^'"]*\1/gi,"").replace(/\s+width=(['"])[^'"]*\1/gi,"").replace(/\s+height=(['"])[^'"]*\1/gi,"").trim(),o=r?` ${r}`:"",s=`<symbol id="${n}" viewBox="${t}"${o}>`;return e.replace(/^\s*<svg\b[^>]*>/i,s).replace(/<\/svg>\s*$/i,"</symbol>").trim()}export{h as bakeIcon,d as bakeIcons};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svg-icon-baker",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "A delightful toolkit for baking raw SVG icons into delicious SVG symbol sprites",
5
5
  "keywords": [
6
6
  "svg",