@cyberalien/svg-utils 0.1.5 → 0.1.7

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.
@@ -3,6 +3,7 @@ type Types = string | Record<string, string>;
3
3
  interface Options {
4
4
  ext?: string;
5
5
  data?: Record<string, Types>;
6
+ defaultProp?: string;
6
7
  }
7
8
  /**
8
9
  * Add exports for main files to object
@@ -4,9 +4,10 @@
4
4
  function createExportsForMainFiles(data, options = {}) {
5
5
  const result = options?.data || Object.create(null);
6
6
  const ext = options.ext || "";
7
+ const defaultProp = options.defaultProp || "default";
7
8
  for (const { icon, filename, types } of data) result[`./${icon}${ext}`] = types ? {
8
9
  types: `./${types}`,
9
- default: `./${filename}`
10
+ [defaultProp]: `./${filename}`
10
11
  } : `./${filename}`;
11
12
  return result;
12
13
  }
@@ -2,10 +2,13 @@ import { GeneratedAssetFile } from "../../types/component.js";
2
2
  import { ComponentFactorySource } from "../../types/source.js";
3
3
  import { ComponentFactoryOptions } from "../../types/options.js";
4
4
  import { FactoryComponentImports } from "../imports/types.js";
5
+ interface Options extends Pick<ComponentFactoryOptions, 'cssMode' | 'cssPath' | 'doubleDirsForCSS' | 'mergeCSS'> {
6
+ styleInComponent?: boolean;
7
+ }
5
8
  /**
6
9
  * Generate CSS files for component
7
10
  *
8
11
  * Adds imports to imports object, adds assets
9
12
  */
10
- declare function generateCSSFilesForComponent(content: ComponentFactorySource, imports: FactoryComponentImports, assets: GeneratedAssetFile[], options: Pick<ComponentFactoryOptions, 'cssMode' | 'cssPath' | 'doubleDirsForCSS' | 'mergeCSS'>): void;
13
+ declare function generateCSSFilesForComponent(content: ComponentFactorySource, imports: FactoryComponentImports, assets: GeneratedAssetFile[], options: Options): string | undefined;
11
14
  export { generateCSSFilesForComponent };
@@ -11,7 +11,8 @@ function generateCSSFilesForComponent(content, imports, assets, options) {
11
11
  const { classes, keyframes } = content;
12
12
  if (!classes) return;
13
13
  const isModule = options.cssMode === "module";
14
- const mergeCSS = options.mergeCSS ?? false;
14
+ const styleInComponent = (!isModule && options.styleInComponent) ?? false;
15
+ const mergeCSS = (styleInComponent || options.mergeCSS) ?? false;
15
16
  const embedAnimations = isModule && !mergeCSS;
16
17
  const mergedContent = [];
17
18
  for (const className in classes) {
@@ -51,12 +52,16 @@ function generateCSSFilesForComponent(content, imports, assets, options) {
51
52
  else imports.css.add(filename.import);
52
53
  }
53
54
  if (mergeCSS && mergedContent.length) {
54
- assets.push({
55
- ...mergeCSS,
56
- content: mergedContent.join("\n\n")
57
- });
58
- if (isModule) imports.modules[mergeCSS.import] = "css";
59
- else imports.css.add(mergeCSS.import);
55
+ const content$1 = mergedContent.join("\n");
56
+ if (typeof mergeCSS == "object") {
57
+ assets.push({
58
+ ...mergeCSS,
59
+ content: content$1
60
+ });
61
+ if (isModule) imports.modules[mergeCSS.import] = "css";
62
+ else imports.css.add(mergeCSS.import);
63
+ }
64
+ return styleInComponent ? content$1 : void 0;
60
65
  }
61
66
  }
62
67
 
@@ -0,0 +1,9 @@
1
+ import { GeneratedAssetFile } from "../../types/component.js";
2
+ import { FactoryIconData } from "../../types/data.js";
3
+ import { ComponentFactoryOptions } from "../../types/options.js";
4
+ import { FactoryComponentProps } from "../props/types.js";
5
+ /**
6
+ * Add Svelte component types
7
+ */
8
+ declare const addSvelteComponentTypes: (data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps) => string;
9
+ export { addSvelteComponentTypes };
@@ -0,0 +1,19 @@
1
+ import { addComponentTypes } from "./wrapper.js";
2
+
3
+ /**
4
+ * Add Svelte component types
5
+ */
6
+ const addSvelteComponentTypes = addComponentTypes.bind(null, `import { SvelteComponent } from "svelte";
7
+ import { SvelteHTMLElements } from "svelte/elements";
8
+
9
+ interface IconProps {
10
+ /* PROPS */
11
+ }
12
+
13
+ declare class Component extends SvelteComponent<Omit<SvelteHTMLElements['svg'], 'viewBox' | 'width' | 'height' | 'xmlns'> & IconProps & Record<\`data-\${string}\`, string>> {}
14
+
15
+ export { type IconProps };
16
+ export default Component;
17
+ `);
18
+
19
+ export { addSvelteComponentTypes };
@@ -5,5 +5,5 @@ import { FactoryComponentProps } from "../props/types.js";
5
5
  /**
6
6
  * Add Vue component types
7
7
  */
8
- declare function addVueComponentTypes(data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps): string;
8
+ declare const addVueComponentTypes: (data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps) => string;
9
9
  export { addVueComponentTypes };
@@ -0,0 +1,18 @@
1
+ import { addComponentTypes } from "./wrapper.js";
2
+
3
+ /**
4
+ * Add Vue component types
5
+ */
6
+ const addVueComponentTypes = addComponentTypes.bind(null, `import { DefineSetupFnComponent, PublicProps } from 'vue';
7
+
8
+ interface IconProps {
9
+ /* PROPS */
10
+ }
11
+
12
+ declare const Component: DefineSetupFnComponent<IconProps, {}, {}, IconProps & {}, PublicProps>;
13
+
14
+ export { type IconProps };
15
+ export default Component;
16
+ `);
17
+
18
+ export { addVueComponentTypes };
@@ -0,0 +1,9 @@
1
+ import { GeneratedAssetFile } from "../../types/component.js";
2
+ import { FactoryIconData } from "../../types/data.js";
3
+ import { ComponentFactoryOptions } from "../../types/options.js";
4
+ import { FactoryComponentProps } from "../props/types.js";
5
+ /**
6
+ * Add component types
7
+ */
8
+ declare function addComponentTypes(template: string, data: FactoryIconData, options: ComponentFactoryOptions, assets: GeneratedAssetFile[], props: FactoryComponentProps): string;
9
+ export { addComponentTypes };
@@ -0,0 +1,18 @@
1
+ import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
2
+ import { stringifyFactoryPropTypes } from "../props/ts.js";
3
+
4
+ /**
5
+ * Add component types
6
+ */
7
+ function addComponentTypes(template, data, options, assets, props) {
8
+ const propTypes = stringifyFactoryPropTypes(props);
9
+ const content = template.replace("/* PROPS */", propTypes);
10
+ const filename = getGeneratedComponentTypesFilename(data, content, options);
11
+ assets.push({
12
+ ...filename,
13
+ content
14
+ });
15
+ return filename.filename;
16
+ }
17
+
18
+ export { addComponentTypes };
@@ -0,0 +1,12 @@
1
+ import { FactoryGeneratedComponent } from "./types/component.js";
2
+ import { FactoryIconData } from "./types/data.js";
3
+ import { ComponentFactoryOptions } from "./types/options.js";
4
+ interface SvelteOptions extends ComponentFactoryOptions {
5
+ ts?: boolean;
6
+ styles?: boolean;
7
+ }
8
+ /**
9
+ * Create Svelte component code
10
+ */
11
+ declare function createSvelteComponent(data: FactoryIconData, options: SvelteOptions): FactoryGeneratedComponent;
12
+ export { createSvelteComponent };
@@ -0,0 +1,97 @@
1
+ import { getComponentSizeValues } from "./helpers/content/size.js";
2
+ import { stringifyFactoryIconContent } from "./helpers/content/stringify.js";
3
+ import { getIconViewBox } from "../svg/viewbox/value.js";
4
+ import { createFactoryImports } from "./helpers/imports/create.js";
5
+ import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
+ import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
+ import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
+ import { addSizeFunctionAsset } from "./helpers/functions/size.js";
9
+ import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
+ import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
+ import { addSvelteComponentTypes } from "./helpers/ts/svelte.js";
12
+
13
+ /**
14
+ * Create Svelte component code
15
+ */
16
+ function createSvelteComponent(data, options) {
17
+ const useTS = options.ts ?? false;
18
+ const styleInComponent = options.styles ?? false;
19
+ const assets = [];
20
+ const imports = createFactoryImports();
21
+ const hasFallback = !!data.fallback;
22
+ if (hasFallback) imports.default["@iconify/css-svelte"] = "Icon";
23
+ const styleContent = generateCSSFilesForComponent(data.icon, imports, assets, {
24
+ ...options,
25
+ styleInComponent
26
+ });
27
+ const componentCode = [];
28
+ const sizeProps = getComponentSizeValues(options, data.viewBox);
29
+ const props = {};
30
+ if (!hasFallback) props.xmlns = "http://www.w3.org/2000/svg";
31
+ const viewBox = data.viewBox;
32
+ const isDynanicViewBox = !sizeProps && options.square && viewBox.width !== viewBox.height;
33
+ const viewBoxPropValue = `viewBox${isDynanicViewBox ? "Computed" : ""}`;
34
+ if (sizeProps) {
35
+ props.width = sizeProps.width;
36
+ props.height = sizeProps.height;
37
+ } else if (hasFallback) {
38
+ props.width = {
39
+ type: "string",
40
+ value: "width",
41
+ template: "width={width}"
42
+ };
43
+ props.height = {
44
+ type: "string",
45
+ value: "height",
46
+ template: "height={height}"
47
+ };
48
+ } else {
49
+ const getSizeProps = addSizeFunctionAsset(imports, assets, options);
50
+ componentCode.push(`let size = $derived(${getSizeProps}(width, height, ${viewBoxPropValue}));`);
51
+ props.width = {
52
+ type: "string",
53
+ value: "width",
54
+ template: "{...size}"
55
+ };
56
+ props.height = {
57
+ type: "string",
58
+ value: "height",
59
+ template: ""
60
+ };
61
+ }
62
+ if (options.square) props.square = { type: "boolean" };
63
+ const getViewBox = (viewBox$1) => hasFallback ? JSON.stringify(viewBox$1) : `'${getIconViewBox(viewBox$1)}'`;
64
+ const viewBoxValue = getViewBox(viewBox);
65
+ if (isDynanicViewBox) componentCode.unshift(`const baseViewBox = ${viewBoxValue};`, `const squareViewBox = ${getViewBox(makeSquareViewBox(viewBox))};`, `let ${viewBoxPropValue} = $derived(square ? squareViewBox : baseViewBox);`);
66
+ else componentCode.unshift(`const viewBox = ${viewBoxValue};`);
67
+ props.viewBox = {
68
+ value: "viewBox",
69
+ template: `viewBox={${viewBoxPropValue}}`
70
+ };
71
+ componentCode.push(`const content = ${stringifyFactoryIconContent(data.icon, options)};`);
72
+ const innerHTML = hasFallback ? "" : "{@html content}";
73
+ props.content = {
74
+ value: "content",
75
+ template: hasFallback ? `content={content} fallback="${data.fallback}"` : ""
76
+ };
77
+ const usedProps = getUsedFactoryProps(props);
78
+ componentCode.unshift(`let {${[...usedProps, "...props"].join(", ")}}${useTS ? ": Props" : ""} = $props();\n`);
79
+ if (useTS) componentCode.unshift(`interface Props {\n${stringifyFactoryPropTypes(props)}\n};\n`);
80
+ const tag = hasFallback ? "Icon" : "svg";
81
+ const template = `<${tag} ${stringifyFactoryProps(props, "{prop}={{value}}")} {...props}>${innerHTML}</${tag}>`;
82
+ let content = `<script${useTS ? " lang=\"ts\"" : ""}>
83
+ ${stringifyFactoryImports(imports)}
84
+ ${componentCode.join("\n")}
85
+ <\/script>
86
+ ${template}
87
+ `;
88
+ if (styleContent) content += `<style>\n:global {\n${styleContent}\n}\n</style>\n`;
89
+ const types = addSvelteComponentTypes(data, options, assets, props);
90
+ return {
91
+ assets,
92
+ content,
93
+ types
94
+ };
95
+ }
96
+
97
+ export { createSvelteComponent };
@@ -4,11 +4,11 @@ import { getIconViewBox } from "../svg/viewbox/value.js";
4
4
  import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
6
  import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
- import { getUsedFactoryProps } from "./helpers/props/ts.js";
8
- import { addVueComponentTypes } from "./helpers/vue/types.js";
9
7
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
10
- import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
11
8
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
9
+ import { getUsedFactoryProps } from "./helpers/props/ts.js";
10
+ import { addVueComponentTypes } from "./helpers/ts/vue.js";
11
+ import { stringifyFactoryPropsAsJSON } from "./helpers/props/object.js";
12
12
 
13
13
  /**
14
14
  * Create functional Vue component code
@@ -3,6 +3,7 @@ import { FactoryIconData } from "./types/data.js";
3
3
  import { ComponentFactoryOptions } from "./types/options.js";
4
4
  interface VueOptions extends ComponentFactoryOptions {
5
5
  ts?: boolean;
6
+ styles?: boolean;
6
7
  }
7
8
  /**
8
9
  * Create Vue component code
@@ -5,23 +5,27 @@ import { createFactoryImports } from "./helpers/imports/create.js";
5
5
  import { generateCSSFilesForComponent } from "./helpers/css/generate.js";
6
6
  import { stringifyFactoryImports } from "./helpers/imports/stringify.js";
7
7
  import { stringifyFactoryProps } from "./helpers/props/stringify.js";
8
- import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
9
- import { addVueComponentTypes } from "./helpers/vue/types.js";
10
8
  import { addSizeFunctionAsset } from "./helpers/functions/size.js";
11
9
  import { makeSquareViewBox } from "../svg/viewbox/square.js";
10
+ import { getUsedFactoryProps, stringifyFactoryPropTypes } from "./helpers/props/ts.js";
11
+ import { addVueComponentTypes } from "./helpers/ts/vue.js";
12
12
 
13
13
  /**
14
14
  * Create Vue component code
15
15
  */
16
16
  function createVueComponent(data, options) {
17
17
  const useTS = options.ts ?? false;
18
+ const styleInComponent = options.styles ?? false;
18
19
  const assets = [];
19
20
  const imports = createFactoryImports();
20
21
  const hasFallback = !!data.fallback;
21
22
  if (hasFallback) imports.named["@iconify/css-vue"] = new Set(["Icon"]);
22
23
  const vueNamedImports = /* @__PURE__ */ new Set();
23
24
  imports.named["vue"] = vueNamedImports;
24
- generateCSSFilesForComponent(data.icon, imports, assets, options);
25
+ const styleContent = generateCSSFilesForComponent(data.icon, imports, assets, {
26
+ ...options,
27
+ styleInComponent
28
+ });
25
29
  const componentCode = [];
26
30
  const sizeProps = getComponentSizeValues(options, data.viewBox);
27
31
  const props = {};
@@ -73,18 +77,17 @@ function createVueComponent(data, options) {
73
77
  };
74
78
  const usedProps = getUsedFactoryProps(props);
75
79
  if (usedProps.length) {
76
- const tsCode = useTS ? `<{
77
- ${stringifyFactoryPropTypes(props)}
78
- }>` : "";
80
+ const tsCode = useTS ? `<{\n${stringifyFactoryPropTypes(props)}\n}>` : "";
79
81
  componentCode.unshift(`const props = defineProps${tsCode}(${JSON.stringify(usedProps)});\n`);
80
82
  }
81
83
  const template = `<template><${hasFallback ? "Icon" : "svg"} ${stringifyFactoryProps(props, ":{prop}=\"{value}\"")} /></template>`;
82
- const content = `<script setup${useTS ? " lang=\"ts\"" : ""}>
84
+ let content = `<script setup${useTS ? " lang=\"ts\"" : ""}>
83
85
  ${stringifyFactoryImports(imports)}
84
86
  ${componentCode.join("\n")}
85
87
  <\/script>
86
88
  ${template}
87
89
  `;
90
+ if (styleContent) content += `<style>\n${styleContent}\n</style>\n`;
88
91
  const types = addVueComponentTypes(data, options, assets, props);
89
92
  return {
90
93
  assets,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "type": "module",
4
4
  "description": "Common functions for working with SVG used by various packages.",
5
5
  "author": "Vjacheslav Trushkin",
6
- "version": "0.1.5",
6
+ "version": "0.1.7",
7
7
  "license": "MIT",
8
8
  "bugs": "https://github.com/cyberalien/svg-utils/issues",
9
9
  "homepage": "https://cyberalien.dev/",
@@ -1,27 +0,0 @@
1
- import { getGeneratedComponentTypesFilename } from "../filenames/types.js";
2
- import { stringifyFactoryPropTypes } from "../props/ts.js";
3
-
4
- /**
5
- * Add Vue component types
6
- */
7
- function addVueComponentTypes(data, options, assets, props) {
8
- const content = `import { DefineSetupFnComponent, PublicProps } from 'vue';
9
-
10
- interface IconProps {
11
- ${stringifyFactoryPropTypes(props)}
12
- }
13
-
14
- declare const Component: DefineSetupFnComponent<IconProps, {}, {}, IconProps & {}, PublicProps>;
15
-
16
- export { type IconProps };
17
- export default Component;
18
- `;
19
- const filename = getGeneratedComponentTypesFilename(data, content, options);
20
- assets.push({
21
- ...filename,
22
- content
23
- });
24
- return filename.filename;
25
- }
26
-
27
- export { addVueComponentTypes };