@svgt/core 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +358 -2
- package/dist/angular.d.ts +7 -0
- package/dist/angular.js +12 -0
- package/dist/astro.d.ts +4 -0
- package/dist/astro.js +9 -0
- package/dist/case-D05A9Slu.js +1 -0
- package/dist/frameworks.d.ts +2 -0
- package/dist/frameworks.js +1 -0
- package/dist/index-CFHXlo8P.d.ts +27 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1 -0
- package/dist/jsx-CLs13YQ-.js +5 -0
- package/dist/options-CMZc7e_j.d.ts +66 -0
- package/dist/preact.d.ts +4 -0
- package/dist/preact.js +1 -0
- package/dist/react.d.ts +4 -0
- package/dist/react.js +1 -0
- package/dist/solid.d.ts +4 -0
- package/dist/solid.js +1 -0
- package/dist/svelte.d.ts +4 -0
- package/dist/svelte.js +7 -0
- package/dist/template-CSV8K9KX.js +4 -0
- package/dist/vue.d.ts +6 -0
- package/dist/vue.js +1 -0
- package/dist/web-components.d.ts +4 -0
- package/dist/web-components.js +38 -0
- package/package.json +82 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pablo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,359 @@
|
|
|
1
|
-
#
|
|
1
|
+
# svgt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A zero-dependency library to transform SVG into frontend components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @svgt/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Import `transform()` and pass it an SVG string and a framework. It returns the component's source code as a string.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { transform } from "@svgt/core";
|
|
17
|
+
|
|
18
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M15 18l-6-6 6-6"/></svg>`;
|
|
19
|
+
|
|
20
|
+
transform(svg, "react", { output: { name: "ArrowLeft" } });
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Import a single framework
|
|
24
|
+
|
|
25
|
+
The main entry point includes every framework. To bundle only one, import `transform()` from its entry point, which takes no `framework` argument:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { transform } from "@svgt/core/react";
|
|
29
|
+
|
|
30
|
+
transform(svg, { output: { name: "ArrowLeft" } });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
| Framework | `framework` value | Entry point |
|
|
34
|
+
| -------------- | ------------------ | --------------------------- |
|
|
35
|
+
| React | `"react"` | `@svgt/core/react` |
|
|
36
|
+
| Preact | `"preact"` | `@svgt/core/preact` |
|
|
37
|
+
| Solid | `"solid"` | `@svgt/core/solid` |
|
|
38
|
+
| Svelte | `"svelte"` | `@svgt/core/svelte` |
|
|
39
|
+
| Vue | `"vue"` | `@svgt/core/vue` |
|
|
40
|
+
| Angular | `"angular"` | `@svgt/core/angular` |
|
|
41
|
+
| Astro | `"astro"` | `@svgt/core/astro` |
|
|
42
|
+
| Web Components | `"web-components"` | `@svgt/core/web-components` |
|
|
43
|
+
|
|
44
|
+
### Framework details
|
|
45
|
+
|
|
46
|
+
`@svgt/core/frameworks` describes each framework without importing any transformer. Use it to build a framework picker or to name the generated file:
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import {
|
|
50
|
+
FRAMEWORKS,
|
|
51
|
+
FRAMEWORK_IDS,
|
|
52
|
+
fileName,
|
|
53
|
+
isFramework,
|
|
54
|
+
} from "@svgt/core/frameworks";
|
|
55
|
+
|
|
56
|
+
FRAMEWORK_IDS; // ["react", "preact", "solid", ...]
|
|
57
|
+
FRAMEWORKS.vue.label; // "Vue"
|
|
58
|
+
FRAMEWORKS.vue.typescriptOption; // false
|
|
59
|
+
FRAMEWORKS.react.exportTypeOption; // true
|
|
60
|
+
FRAMEWORKS.vue.expandPropsOption; // false
|
|
61
|
+
|
|
62
|
+
fileName("react", "arrow-left"); // "ArrowLeft.tsx (tsx)"
|
|
63
|
+
fileName("react", "arrow-left", false); // "ArrowLeft.jsx (jsx)"
|
|
64
|
+
fileName("angular", "ArrowLeft"); // "arrow-left.component.ts"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
| Export | Description |
|
|
68
|
+
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `FRAMEWORK_IDS` | Every `framework` value, in display order. |
|
|
70
|
+
| `FRAMEWORKS` | Each framework's `label`, whether it accepts the `typescript` option (`typescriptOption`), whether it uses `output.exportType` (`exportTypeOption`) and `output.expandProps` (`expandPropsOption`), and its `fileName()`. |
|
|
71
|
+
| `fileName(framework, name, typescript)` | File name for a component, with `name` in PascalCase first. `typescript` defaults to `true`. The CLI uses the same names. |
|
|
72
|
+
| `isFramework(value)` | Whether a string is a `framework` value. Narrows it to the `Framework` type. |
|
|
73
|
+
|
|
74
|
+
## Framework outputs
|
|
75
|
+
|
|
76
|
+
### React
|
|
77
|
+
|
|
78
|
+
A function component that spreads `props` onto the `<svg>`. Attributes become JSX props (`class` to `className`, `stroke-width` to `strokeWidth`) and inline `style` becomes an object.
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import type { SVGProps } from "react";
|
|
82
|
+
|
|
83
|
+
const SvgIcon = (props: SVGProps<SVGSVGElement>) => (
|
|
84
|
+
/// <svg ...props></svg>
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
export default SvgIcon;
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Preact
|
|
91
|
+
|
|
92
|
+
A function component that spreads `props` onto the `<svg>`. Attributes keep their SVG names (`class`, `stroke-width`), because Preact sets them as written: `strokeWidth` only works with `preact/compat`. Inline `style` stays a string. `xlink:href` becomes `href`, and `tabindex` and numeric `aria-*` values are printed as numbers, as Preact's types expect.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import type { SVGAttributes } from "preact";
|
|
96
|
+
|
|
97
|
+
const SvgIcon = (props: SVGAttributes<SVGSVGElement>) => (
|
|
98
|
+
/// <svg ...props></svg>
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
export default SvgIcon;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Solid
|
|
105
|
+
|
|
106
|
+
A Solid 2 function component that spreads `props` onto the `<svg>`, which keeps them reactive. Attributes keep their SVG names (`class`, `stroke-width`, `viewBox`) and inline `style` stays a string. Solid's types declare no `xlink:*` or `xml:*` attributes, so `xlink:href` becomes `href`, `xml:lang` becomes `lang` and the rest are dropped.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import type { ComponentProps } from "@solidjs/web";
|
|
110
|
+
|
|
111
|
+
const SvgIcon = (props: ComponentProps<"svg">) => (
|
|
112
|
+
/// <svg ...props></svg>
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
export default SvgIcon;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Svelte
|
|
119
|
+
|
|
120
|
+
A Svelte 5 component that reads `$props()` and spreads them onto the `<svg>`.
|
|
121
|
+
|
|
122
|
+
```svelte
|
|
123
|
+
<script lang="ts">
|
|
124
|
+
import type { SVGAttributes } from "svelte/elements";
|
|
125
|
+
|
|
126
|
+
let props: SVGAttributes<SVGSVGElement> = $props();
|
|
127
|
+
</script>
|
|
128
|
+
|
|
129
|
+
<svg {...props}>
|
|
130
|
+
<!-- ... -->
|
|
131
|
+
</svg>
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Vue
|
|
135
|
+
|
|
136
|
+
A single-file component with only a `<template>`, so attributes fall through to the `<svg>`. Inner `<style>` and `<script>` tags are printed as `<component is="style">` and `<component is="script">`, because Vue doesn't allow them in a template.
|
|
137
|
+
|
|
138
|
+
```vue
|
|
139
|
+
<template>
|
|
140
|
+
<svg>
|
|
141
|
+
<!-- ... -->
|
|
142
|
+
</svg>
|
|
143
|
+
</template>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Angular
|
|
147
|
+
|
|
148
|
+
A standalone component with `OnPush` change detection. Its host element uses `display: contents`, so it doesn't add a box around the `<svg>`. With the default name, the class is `SvgIconComponent` and the selector is `svg-icon`.
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import { ChangeDetectionStrategy, Component } from "@angular/core";
|
|
152
|
+
|
|
153
|
+
@Component({
|
|
154
|
+
selector: "svg-icon",
|
|
155
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
156
|
+
host: { style: "display: contents" },
|
|
157
|
+
template: `
|
|
158
|
+
<svg>
|
|
159
|
+
<!-- ... -->
|
|
160
|
+
</svg>
|
|
161
|
+
`,
|
|
162
|
+
})
|
|
163
|
+
export class SvgIconComponent {}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Astro
|
|
167
|
+
|
|
168
|
+
A component that spreads `Astro.props` onto the `<svg>`. Inner `<style>` and `<script>` tags get `is:inline` so Astro leaves them as they are instead of scoping or bundling them.
|
|
169
|
+
|
|
170
|
+
```astro
|
|
171
|
+
---
|
|
172
|
+
import type { HTMLAttributes } from "astro/types";
|
|
173
|
+
|
|
174
|
+
type Props = HTMLAttributes<"svg">;
|
|
175
|
+
|
|
176
|
+
const props = Astro.props;
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
<svg {...props}>
|
|
180
|
+
<!-- ... -->
|
|
181
|
+
</svg>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Web Components
|
|
185
|
+
|
|
186
|
+
A custom element with a shadow root. It forwards `width`, `height`, `fill`, `stroke` and `stroke-width` from the element to the inner `<svg>`, and registers itself unless the tag is already defined. The class is the default export, or exported directly with [`exportType: "directly"`](#export-type). With TypeScript on, it also adds the tag to `HTMLElementTagNameMap`.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
const template = document.createElement("template");
|
|
190
|
+
template.innerHTML = `
|
|
191
|
+
<style>:host { display: inline-flex; }</style>
|
|
192
|
+
<svg><!-- ... --></svg>
|
|
193
|
+
`;
|
|
194
|
+
|
|
195
|
+
class SvgIcon extends HTMLElement {
|
|
196
|
+
static observedAttributes = [
|
|
197
|
+
"width",
|
|
198
|
+
"height",
|
|
199
|
+
"fill",
|
|
200
|
+
"stroke",
|
|
201
|
+
"stroke-width",
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
// ...
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!customElements.get("svg-icon")) {
|
|
208
|
+
customElements.define("svg-icon", SvgIcon);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export default SvgIcon;
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```html
|
|
215
|
+
<svg-icon width="32" fill="currentColor"></svg-icon>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Options
|
|
219
|
+
|
|
220
|
+
Every option is optional.
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
transform(svg, "react", {
|
|
224
|
+
typescript: true,
|
|
225
|
+
output: {
|
|
226
|
+
name: "ArrowLeft",
|
|
227
|
+
exportType: "default",
|
|
228
|
+
dimensions: true,
|
|
229
|
+
svgProps: {},
|
|
230
|
+
expandProps: "end",
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
| Option | Type | Default | Frameworks |
|
|
236
|
+
| -------------------- | ----------------------------------------------- | ----------- | --------------------------------------------------- |
|
|
237
|
+
| `typescript` | `boolean` | `true` | React, Preact, Solid, Svelte, Astro, Web Components |
|
|
238
|
+
| `output.name` | `string` | `"SvgIcon"` | React, Preact, Solid, Angular, Web Components |
|
|
239
|
+
| `output.exportType` | `"default"` or `"directly"` | `"default"` | React, Preact, Solid, Web Components |
|
|
240
|
+
| `output.dimensions` | `boolean` | `true` | All |
|
|
241
|
+
| `output.svgProps` | `Record<string, string>`, with `null` to remove | `{}` | All |
|
|
242
|
+
| `output.expandProps` | `"start"`, `"end"` or `false` | `"end"` | React, Preact, Solid, Svelte, Astro |
|
|
243
|
+
| `preTransform` | `(svg: string) => string` | — | All |
|
|
244
|
+
|
|
245
|
+
### TypeScript
|
|
246
|
+
|
|
247
|
+
The output is TypeScript by default. Set `typescript` to `false` to get JavaScript:
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
transform(svg, "react", { typescript: false });
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
> Vue and Angular don't accept this option, and passing it is a type error: the Vue component has no `<script>`, and Angular components are always TypeScript.
|
|
254
|
+
|
|
255
|
+
### Name
|
|
256
|
+
|
|
257
|
+
`output.name` sets the component name and is converted to PascalCase.
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
transform(svg, "angular", { output: { name: "ArrowLeft" } });
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
- **React**, **Preact** and **Solid**: the component is `ArrowLeft`.
|
|
264
|
+
- **Angular**: the class is `ArrowLeftComponent` (the suffix is not added twice) and the selector is `arrow-left`.
|
|
265
|
+
- **Web Components**: the class is `ArrowLeft` and the tag is `arrow-left`. Custom element names need a hyphen, so a one-word name like `Arrow` gets the tag `svg-arrow`.
|
|
266
|
+
|
|
267
|
+
> Svelte, Vue and Astro ignore it, because those components take their name from the file they're saved in.
|
|
268
|
+
|
|
269
|
+
### Export type
|
|
270
|
+
|
|
271
|
+
`output.exportType` sets how the component is exported, in React, Preact, Solid and Web Components:
|
|
272
|
+
|
|
273
|
+
- `"default"` (default): `export default ArrowLeft;` at the end.
|
|
274
|
+
- `"directly"`: on the declaration, as `export const ArrowLeft = (props) => ...` in React, Preact and Solid, and `export class ArrowLeft extends HTMLElement` in Web Components. Import it with `import { ArrowLeft } from "./ArrowLeft"`.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
transform(svg, "react", {
|
|
278
|
+
output: { name: "ArrowLeft", exportType: "directly" },
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
> Svelte, Vue and Astro ignore it, because those components are files. Angular ignores it too: its class is always exported directly.
|
|
283
|
+
|
|
284
|
+
### Dimensions
|
|
285
|
+
|
|
286
|
+
`output.dimensions` keeps the `width` and `height` attributes of the root `<svg>`. Set it to `false` to remove them, so the icon is sized with CSS:
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
transform(svg, "react", {
|
|
290
|
+
output: { dimensions: false },
|
|
291
|
+
});
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### SVG props
|
|
295
|
+
|
|
296
|
+
`output.svgProps` adds, replaces or removes attributes of the root `<svg>`. A string sets the attribute, and `null` removes it:
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
transform(svg, "react", {
|
|
300
|
+
output: {
|
|
301
|
+
svgProps: {
|
|
302
|
+
"aria-hidden": "true",
|
|
303
|
+
"stroke-width": "1.5",
|
|
304
|
+
class: null,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
- Use the SVG attribute names, as in the `.svg` file. Each framework prints them with its own syntax, so `stroke-width` becomes `strokeWidth` in React.
|
|
311
|
+
- It runs after `dimensions`, so `{ dimensions: false, svgProps: { width: "1em", height: "1em" } }` replaces the original size with `1em`.
|
|
312
|
+
- The values are static strings. Props passed to the component still override them where the framework spreads them onto the `<svg>`.
|
|
313
|
+
|
|
314
|
+
### Expand props
|
|
315
|
+
|
|
316
|
+
`output.expandProps` sets where the component's props are spread on the root `<svg>`:
|
|
317
|
+
|
|
318
|
+
- `"end"` (default): after the SVG's attributes, so props passed to the component override them.
|
|
319
|
+
- `"start"`: before them, so the SVG's attributes win.
|
|
320
|
+
- `false`: not spread. The component doesn't declare props at all.
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
transform(svg, "react", {
|
|
324
|
+
output: { expandProps: "start" },
|
|
325
|
+
});
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
- It runs after `svgProps`, so with `"end"` props also override the attributes set there.
|
|
329
|
+
- Vue, Angular and Web Components ignore it: Vue passes attributes through on its own, and the others don't spread props.
|
|
330
|
+
|
|
331
|
+
### Pre-transform
|
|
332
|
+
|
|
333
|
+
`preTransform` runs on the SVG string before svgt parses it, and returns the SVG string to transform.
|
|
334
|
+
|
|
335
|
+
#### SVGO
|
|
336
|
+
|
|
337
|
+
Example with [SVGO](https://github.com/svg/svgo):
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
import { optimize } from "svgo";
|
|
341
|
+
import { transform } from "@svgt/core";
|
|
342
|
+
|
|
343
|
+
transform(svg, "react", {
|
|
344
|
+
preTransform: (svg) => optimize(svg, { multipass: true }).data,
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
## Errors
|
|
349
|
+
|
|
350
|
+
`transform()` throws an `Error` prefixed with `[svgt]` when the SVG can't be parsed, such as `[svgt] Unclosed tag <div> (at position 5)`. It also throws when `preTransform` throws (`[svgt] preTransform failed: ...`, with the original error as `cause`) or doesn't return a string, when `output.svgProps` has an invalid attribute name (`[svgt] Invalid attribute name in svgProps: "1bad"`), when `output.exportType` isn't `"default"` or `"directly"`, and when `output.expandProps` isn't `"start"`, `"end"` or `false`.
|
|
351
|
+
|
|
352
|
+
## References
|
|
353
|
+
|
|
354
|
+
- [gregberge/svgr](https://react-svgr.com/) - Transform SVGs into React components.
|
|
355
|
+
- [SVGO](https://github.com/svg/svgo) - SVG Optimizer for Node.js and CLI.
|
|
356
|
+
|
|
357
|
+
## License
|
|
358
|
+
|
|
359
|
+
MIT
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { t as BaseTransformOptions } from "./options-CMZc7e_j.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert an SVG string into an Angular standalone component.
|
|
4
|
+
* Angular components are always TypeScript.
|
|
5
|
+
*/
|
|
6
|
+
export declare const transform: (svg: string, options?: BaseTransformOptions) => string;
|
|
7
|
+
export type { BaseTransformOptions as TransformOptions };
|
package/dist/angular.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import{c as e,i as t,n,s as r}from"./template-CSV8K9KX.js";import{n as i}from"./case-D05A9Slu.js";const a=(a,o)=>{let{root:s,name:c}=r(a,o),l=n(),u=e(s,{...l,text:(e,t)=>l.text(e,t).replace(/@/g,`@`),indent:2}),d=i(c),f=c.endsWith(`Component`)?c:`${c}Component`;return`import { ChangeDetectionStrategy, Component } from "@angular/core";
|
|
2
|
+
|
|
3
|
+
@Component({
|
|
4
|
+
selector: "${d}",
|
|
5
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
6
|
+
host: { style: "display: contents" },
|
|
7
|
+
template: \`
|
|
8
|
+
${t(u)}
|
|
9
|
+
\`,
|
|
10
|
+
})
|
|
11
|
+
export class ${f} {}
|
|
12
|
+
`};export{a as transform};
|
package/dist/astro.d.ts
ADDED
package/dist/astro.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import{c as e,l as t,n,r,s as i,t as a}from"./template-CSV8K9KX.js";const o=(o,s)=>{let{root:c,typescript:l}=i(o,s);r(c,e=>{a.has(e.name)&&e.attributes.push({name:`is:inline`,value:``})});let u=n(`{...props}`),d=e(c,{...u,attribute:(e,t)=>e.name===`is:inline`?`is:inline`:u.attribute(e,t)});return t(c)?`${l?`---
|
|
2
|
+
import type { HTMLAttributes } from "astro/types";
|
|
3
|
+
|
|
4
|
+
type Props = HTMLAttributes<"svg">;
|
|
5
|
+
|
|
6
|
+
const props = Astro.props;
|
|
7
|
+
---`:`---
|
|
8
|
+
const props = Astro.props;
|
|
9
|
+
---`}\n\n${d}\n`:`${d}\n`};export{o as transform};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=e=>{let t=e.replace(/\.svg$/i,``).split(/[^a-zA-Z0-9]+/).filter(Boolean).map(e=>e[0].toUpperCase()+e.slice(1)).join(``);return t?/^\d/.test(t)?`Svg${t}`:t:`SvgIcon`},t=e=>e.replace(/([a-z\d])([A-Z])/g,`$1-$2`).replace(/([A-Z]+)([A-Z][a-z])/g,`$1-$2`).toLowerCase(),n=e=>e.replace(/[-:]([a-z])/g,(e,t)=>t.toUpperCase());export{t as n,e as r,n as t};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as Framework, c as fileName, i as FRAMEWORK_IDS, n as ExportTypeFramework, o as FrameworkInfo, r as FRAMEWORKS, s as TypeScriptAgnosticFramework, t as ExpandPropsFramework } from "./index-CFHXlo8P.js";
|
|
2
|
+
export { ExpandPropsFramework, ExportTypeFramework, FRAMEWORKS, FRAMEWORK_IDS, Framework, FrameworkInfo, TypeScriptAgnosticFramework, fileName };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{n as e,r as t}from"./case-D05A9Slu.js";const n={react:{label:`React`,typescriptOption:!0,exportTypeOption:!0,expandPropsOption:!0,fileName:(e,t)=>`${e}.${t?`tsx`:`jsx`}`},preact:{label:`Preact`,typescriptOption:!0,exportTypeOption:!0,expandPropsOption:!0,fileName:(e,t)=>`${e}.${t?`tsx`:`jsx`}`},solid:{label:`Solid`,typescriptOption:!0,exportTypeOption:!0,expandPropsOption:!0,fileName:(e,t)=>`${e}.${t?`tsx`:`jsx`}`},svelte:{label:`Svelte`,typescriptOption:!0,exportTypeOption:!1,expandPropsOption:!0,fileName:e=>`${e}.svelte`},vue:{label:`Vue`,typescriptOption:!1,exportTypeOption:!1,expandPropsOption:!1,fileName:e=>`${e}.vue`},angular:{label:`Angular`,typescriptOption:!1,exportTypeOption:!1,expandPropsOption:!1,fileName:t=>`${e(t)}.component.ts`},astro:{label:`Astro`,typescriptOption:!0,exportTypeOption:!1,expandPropsOption:!0,fileName:e=>`${e}.astro`},"web-components":{label:`Web Components`,typescriptOption:!0,exportTypeOption:!0,expandPropsOption:!1,fileName:(t,n)=>`${e(t)}.${n?`ts`:`js`}`}},r=Object.keys(n),i=(e,r,i=!0)=>n[e].fileName(t(r),i);export{n as FRAMEWORKS,r as FRAMEWORK_IDS,i as fileName};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type Framework = "react" | "preact" | "solid" | "svelte" | "vue" | "angular" | "astro" | "web-components";
|
|
2
|
+
/** Frameworks whose output does not depend on the `typescript` option. */
|
|
3
|
+
type TypeScriptAgnosticFramework = "angular" | "vue";
|
|
4
|
+
/** Frameworks whose output depends on the `exportType` option. */
|
|
5
|
+
type ExportTypeFramework = "react" | "preact" | "solid" | "web-components";
|
|
6
|
+
/** Frameworks whose output depends on the `expandProps` option. */
|
|
7
|
+
type ExpandPropsFramework = "react" | "preact" | "solid" | "svelte" | "astro";
|
|
8
|
+
interface FrameworkInfo<F extends Framework = Framework> {
|
|
9
|
+
/** Display name, such as `"Web Components"`. */
|
|
10
|
+
label: string;
|
|
11
|
+
/** Whether `transform()` accepts the `typescript` option. */
|
|
12
|
+
typescriptOption: F extends TypeScriptAgnosticFramework ? false : true;
|
|
13
|
+
/** Whether `transform()` uses the `output.exportType` option. */
|
|
14
|
+
exportTypeOption: F extends ExportTypeFramework ? true : false;
|
|
15
|
+
/** Whether `transform()` uses the `output.expandProps` option. */
|
|
16
|
+
expandPropsOption: F extends ExpandPropsFramework ? true : false;
|
|
17
|
+
/** File name for a component, following the framework's convention. */
|
|
18
|
+
fileName: (name: string, typescript: boolean) => string;
|
|
19
|
+
}
|
|
20
|
+
declare const FRAMEWORKS: { readonly [F in Framework]: FrameworkInfo<F>; };
|
|
21
|
+
declare const FRAMEWORK_IDS: readonly Framework[];
|
|
22
|
+
/**
|
|
23
|
+
* File name for a component, following each framework's convention.
|
|
24
|
+
* `name` is converted to PascalCase first, like `output.name` in `transform()`.
|
|
25
|
+
*/
|
|
26
|
+
declare const fileName: (framework: Framework, name: string, typescript?: boolean) => string;
|
|
27
|
+
export { Framework as a, fileName as c, FRAMEWORK_IDS as i, ExportTypeFramework as n, FrameworkInfo as o, FRAMEWORKS as r, TypeScriptAgnosticFramework as s, ExpandPropsFramework as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { a as Framework } from "./index-CFHXlo8P.js";
|
|
2
|
+
import { a as PreTransform, c as SvgProps, i as OutputOptions, n as ExportType, o as TransformOptions, r as FrameworkOptions, s as ExpandProps, t as BaseTransformOptions } from "./options-CMZc7e_j.js";
|
|
3
|
+
/**
|
|
4
|
+
* Transform an SVG string into a component for the given framework.
|
|
5
|
+
* The `typescript` option is only accepted by frameworks it applies to.
|
|
6
|
+
*/
|
|
7
|
+
export declare const transform: <F extends Framework>(svg: string, framework: F, options?: FrameworkOptions<F>) => string;
|
|
8
|
+
export type { BaseTransformOptions, ExpandProps, ExportType, Framework, FrameworkOptions, OutputOptions, PreTransform, SvgProps, TransformOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{transform as e}from"./vue.js";import{transform as t}from"./react.js";import{transform as n}from"./solid.js";import{transform as r}from"./astro.js";import{transform as i}from"./preact.js";import{transform as a}from"./svelte.js";import{transform as o}from"./angular.js";import{transform as s}from"./web-components.js";const c={react:t,preact:i,solid:n,svelte:a,vue:e,angular:o,astro:r,"web-components":s},l=(e,t,n)=>{let r=c[t];if(!r)throw Error(`[svgt] Unknown framework "${t}"`);return r(e,n)};export{l as transform};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import{t as e}from"./template-CSV8K9KX.js";const t=(e,t)=>t.attributes.some(({name:t})=>t===e)?null:e,n=(e,t)=>/["\\&\n{}]/.test(t)?`${e}={${JSON.stringify(t)}}`:`${e}="${t}"`,r=(t,n)=>e.has(n.name)?`{${JSON.stringify(t)}}`:t.replace(/[{}<>&]/g,e=>`{${JSON.stringify(e)}}`),i=({name:e,markup:t,params:n,imports:r,exportType:i})=>{let a=r.length?`${r.join(`
|
|
2
|
+
`)}\n\n`:``,o=`const ${e} = (${n}) => (
|
|
3
|
+
${t}
|
|
4
|
+
);
|
|
5
|
+
`;return i===`directly`?`${a}export ${o}`:`${a}${o}\nexport default ${e};\n`};export{t as i,i as n,r,n as t};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { a as Framework, s as TypeScriptAgnosticFramework } from "./index-CFHXlo8P.js";
|
|
2
|
+
interface SvgAttribute {
|
|
3
|
+
name: string;
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
interface SvgElement {
|
|
7
|
+
type: "element";
|
|
8
|
+
name: string;
|
|
9
|
+
attributes: SvgAttribute[];
|
|
10
|
+
children: SvgNode[];
|
|
11
|
+
}
|
|
12
|
+
interface SvgText {
|
|
13
|
+
type: "text";
|
|
14
|
+
value: string;
|
|
15
|
+
}
|
|
16
|
+
type SvgNode = SvgElement | SvgText;
|
|
17
|
+
/**
|
|
18
|
+
* Modifies the parsed SVG before it's printed for a framework.
|
|
19
|
+
* `value` is what the user passed in `output` for this customizer.
|
|
20
|
+
* It only runs when that value is set, so the default is to do nothing.
|
|
21
|
+
*/
|
|
22
|
+
type Customizer<T> = (root: SvgElement, value: T) => void;
|
|
23
|
+
/** The `output` value type a customizer accepts. */
|
|
24
|
+
type CustomizerValue<C> = C extends Customizer<infer T> ? T : never;
|
|
25
|
+
type SvgProps = Record<string, string | null>;
|
|
26
|
+
type ExpandProps = "start" | "end" | false;
|
|
27
|
+
interface Customizers {
|
|
28
|
+
/** Keep `width` and `height` on the root `<svg>`. @default true */
|
|
29
|
+
dimensions: Customizer<boolean>;
|
|
30
|
+
/** Add, replace (string) or remove (`null`) attributes of the root `<svg>`. */
|
|
31
|
+
svgProps: Customizer<SvgProps>;
|
|
32
|
+
/** Spread the component's props at the `"start"` or `"end"` of the root `<svg>`, or not at all (`false`). @default "end" */
|
|
33
|
+
expandProps: Customizer<ExpandProps>;
|
|
34
|
+
}
|
|
35
|
+
type CustomizerOptions = { [K in keyof Customizers]?: CustomizerValue<Customizers[K]>; };
|
|
36
|
+
/**
|
|
37
|
+
* How a React, Preact, Solid or Web Components component is exported:
|
|
38
|
+
* `"default"` (`export default SvgIcon;`) or `"directly"` (`export const SvgIcon = ...`, `export class SvgIcon ...`).
|
|
39
|
+
*/
|
|
40
|
+
type ExportType = "default" | "directly";
|
|
41
|
+
/** What the generated component looks like: its name, export and the customizers. */
|
|
42
|
+
interface OutputOptions extends CustomizerOptions {
|
|
43
|
+
/** Component name. Converted to PascalCase. @default "SvgIcon" */
|
|
44
|
+
name?: string;
|
|
45
|
+
/** How the component is exported. Only React, Preact, Solid and Web Components use it. @default "default" */
|
|
46
|
+
exportType?: ExportType;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Runs on the SVG string before it's transformed.
|
|
50
|
+
*/
|
|
51
|
+
type PreTransform = (svg: string) => string;
|
|
52
|
+
/** Options shared by every framework. */
|
|
53
|
+
interface BaseTransformOptions {
|
|
54
|
+
/** Component name and customizers. */
|
|
55
|
+
output?: OutputOptions;
|
|
56
|
+
/** Runs on the SVG string before it's transformed. */
|
|
57
|
+
preTransform?: PreTransform;
|
|
58
|
+
}
|
|
59
|
+
/** Options for frameworks that can emit either TypeScript or JavaScript. */
|
|
60
|
+
interface TransformOptions extends BaseTransformOptions {
|
|
61
|
+
/** Use TypeScript. @default true */
|
|
62
|
+
typescript?: boolean;
|
|
63
|
+
}
|
|
64
|
+
/** The options each framework accepts. */
|
|
65
|
+
type FrameworkOptions<F extends Framework> = F extends TypeScriptAgnosticFramework ? BaseTransformOptions : TransformOptions;
|
|
66
|
+
export { PreTransform as a, SvgProps as c, OutputOptions as i, ExportType as n, TransformOptions as o, FrameworkOptions as r, ExpandProps as s, BaseTransformOptions as t };
|
package/dist/preact.d.ts
ADDED
package/dist/preact.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as e,l as t,s as n}from"./template-CSV8K9KX.js";import{i as r,n as i,r as a,t as o}from"./jsx-CLs13YQ-.js";const s=(e,t)=>e===`xlink:href`?r(`href`,t):e===`xmlns:xlink`?null:e,c=/* @__PURE__ */ new Set([`tabindex`,`aria-colcount`,`aria-colindex`,`aria-colspan`,`aria-level`,`aria-posinset`,`aria-rowcount`,`aria-rowindex`,`aria-rowspan`,`aria-setsize`,`aria-valuemax`,`aria-valuemin`,`aria-valuenow`]),l=({name:e,value:t},n)=>{let r=s(e,n);return r===null?null:c.has(r)&&/^-?\d+(\.\d+)?$/.test(t)?`${r}={${t}}`:o(r,t)},u=(r,o)=>{let{root:s,name:c,typescript:u,exportType:d}=n(r,o),f=e(s,{attribute:l,text:a,spread:`{...props}`,indent:1}),p=t(s);return i({name:c,markup:f,params:p?u?`props: SVGAttributes<SVGSVGElement>`:`props`:``,imports:u&&p?[`import type { SVGAttributes } from "preact";`]:[],exportType:d})};export{u as transform};
|
package/dist/react.d.ts
ADDED
package/dist/react.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as e,l as t,s as n}from"./template-CSV8K9KX.js";import{t as r}from"./case-D05A9Slu.js";import{n as i,r as a,t as o}from"./jsx-CLs13YQ-.js";const s=e=>e===`class`?`className`:e===`tabindex`?`tabIndex`:e.startsWith(`data-`)||e.startsWith(`aria-`)?e:r(e),c=e=>`{{ ${e.split(`;`).map(e=>{let t=e.indexOf(`:`);if(t===-1)return null;let n=e.slice(0,t).trim(),i=e.slice(t+1).trim();return!n||!i?null:`${n.startsWith(`--`)?JSON.stringify(n):r(n.toLowerCase().replace(/^-ms-/,`ms-`))}: ${JSON.stringify(i)}`}).filter(Boolean).join(`, `)} }}`,l=/* @__PURE__ */ new Set([`tabIndex`]),u=({name:e,value:t},n)=>{let r=s(e);if(r===`style`){let e=c(t);return n&&e.includes(`"--`)?`style={${e.slice(1,-1)} as CSSProperties}`:`style=${e}`}return l.has(r)&&/^-?\d+$/.test(t)?`${r}={${t}}`:o(r,t)},d=(r,o)=>{let{root:s,name:c,typescript:l,exportType:d}=n(r,o),f=e(s,{attribute:e=>u(e,l),text:a,spread:`{...props}`,indent:1}),p=t(s),m=[f.includes(` as CSSProperties}`)&&`CSSProperties`,p&&`SVGProps`].filter(Boolean);return i({name:c,markup:f,params:p?l?`props: SVGProps<SVGSVGElement>`:`props`:``,imports:l&&m.length?[`import type { ${m.join(`, `)} } from "react";`]:[],exportType:d})};export{d as transform};
|
package/dist/solid.d.ts
ADDED
package/dist/solid.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as e,l as t,s as n}from"./template-CSV8K9KX.js";import{i as r,n as i,r as a,t as o}from"./jsx-CLs13YQ-.js";const s=(e,t)=>e===`xlink:href`?r(`href`,t):e===`xml:lang`?r(`lang`,t):/^(xlink|xml|xmlns):/.test(e)?null:e,c=({name:e,value:t},n)=>{let r=s(e,n);return r===null?null:o(r,t)},l=(r,o)=>{let{root:s,name:l,typescript:u,exportType:d}=n(r,o),f=e(s,{attribute:c,text:a,spread:`{...props}`,indent:1}),p=t(s);return i({name:l,markup:f,params:p?u?`props: ComponentProps<"svg">`:`props`:``,imports:u&&p?[`import type { ComponentProps } from "@solidjs/web";`]:[],exportType:d})};export{l as transform};
|
package/dist/svelte.d.ts
ADDED
package/dist/svelte.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import{c as e,l as t,n,s as r}from"./template-CSV8K9KX.js";const i=(i,a)=>{let{root:o,typescript:s}=r(i,a),c=e(o,n(`{...props}`));return t(o)?`${s?`<script lang="ts">
|
|
2
|
+
import type { SVGAttributes } from "svelte/elements";
|
|
3
|
+
|
|
4
|
+
let props: SVGAttributes<SVGSVGElement> = $props();
|
|
5
|
+
<\/script>`:`<script>
|
|
6
|
+
let props = $props();
|
|
7
|
+
<\/script>`}\n\n${c}\n`:`${c}\n`};export{i as transform};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{r as e}from"./case-D05A9Slu.js";const t=[`start`,`end`,!1],n=`...props`,r=e=>e.name===n,i=e=>e.attributes.some(r),a=(e,i)=>{if(!t.includes(i))throw Error(`[svgt] Invalid expandProps: ${JSON.stringify(i)}. Use "start", "end" or false.`);if(e.attributes=e.attributes.filter(e=>!r(e)),i===!1)return;let a={name:n,value:``};i===`start`?e.attributes.unshift(a):e.attributes.push(a)},o=(e,t)=>{let n=(e,i,a)=>{let o=` `.repeat(a);if(e.type===`text`)return o+t.text(e.value,i);let s=e.attributes.map(n=>r(n)?t.spread??null:t.attribute(n,e)).filter(e=>e!==null),c=s.length?` ${s.join(` `)}`:``,l=o.length+e.name.length+c.length+3<=80||s.length<2?c:`\n${s.map(e=>o+` `+e).join(`
|
|
2
|
+
`)}\n${o}`;if(!e.children.length)return`${o}<${e.name}${l}${l.endsWith(`
|
|
3
|
+
`+o)?``:` `}/>`;if(e.children.every(e=>e.type===`text`)){let n=e.children.map(n=>t.text(n.value,e)).join(` `);return`${o}<${e.name}${l}>${n}</${e.name}>`}let u=e.children.map(t=>n(t,e,a+1)).join(`
|
|
4
|
+
`);return`${o}<${e.name}${l}>\n${u}\n${o}</${e.name}>`};return n(e,e,t.indent??0)},s=/[\w:.-]/,c=/\s/,l={amp:`&`,lt:`<`,gt:`>`,quot:`"`,apos:`'`,nbsp:`\xA0`},u=e=>e.replace(/&(#x[\da-f]+|#\d+|\w+);/gi,(e,t)=>{if(t[0]===`#`){let n=t[1]===`x`||t[1]===`X`?parseInt(t.slice(2),16):parseInt(t.slice(1),10);return Number.isNaN(n)?e:String.fromCodePoint(n)}return l[t.toLowerCase()]??e}),d=e=>{let t=e.trim(),n=0,r=e=>{throw Error(`[svgt] ${e} (at position ${n})`)},i=()=>{for(;n<t.length&&c.test(t[n]);)n++},a=e=>{let i=t.indexOf(e,n);i===-1&&r(`Expected "${e}"`),n=i+e.length},o=()=>{let e=n;for(;n<t.length&&s.test(t[n]);)n++;return e===n&&r(`Expected a tag or attribute name`),t.slice(e,n)},l=()=>{let e=[];for(;;){i();let a=t[n];if(a===void 0&&r(`Unexpected end of input`),a===`>`||a===`/`)return e;let s=o();i();let c=``;if(t[n]===`=`){n++,i();let e=t[n];if(e===`"`||e===`'`){let i=t.indexOf(e,n+1);i===-1&&r(`Unclosed attribute value for "${s}"`),c=t.slice(n+1,i),n=i+1}else{let e=n;for(;n<t.length&&!/[\s/>]/.test(t[n]);)n++;c=t.slice(e,n)}}e.push({name:s,value:u(c)})}},d=e=>{let s=[];for(;n<t.length;)if(t.startsWith(`<!--`,n))a(`-->`);else if(t.startsWith(`<![CDATA[`,n)){let e=n+9;a(`]]>`),s.push({type:`text`,value:t.slice(e,n-3)})}else if(t.startsWith(`<?`,n))a(`?>`);else if(t.startsWith(`<!`,n))a(`>`);else if(t.startsWith(`</`,n)){n+=2;let t=o();return e&&t===e.name||r(`Unexpected closing tag </${t}>`),i(),a(`>`),s}else if(t[n]===`<`){n++;let e={type:`element`,name:o(),attributes:l(),children:[]};t[n]===`/`?(n++,t[n]!==`>`&&r(`Expected '>'`),n++):(n++,e.children=d(e)),s.push(e)}else{let e=t.indexOf(`<`,n),r=t.slice(n,e===-1?t.length:e);n=e===-1?t.length:e;let i=u(r).replace(/\s+/g,` `);i.trim()&&s.push({type:`text`,value:i.trim()})}return e&&r(`Unclosed tag <${e.name}>`),s},f=d(null).find(e=>e.type===`element`&&e.name===`svg`);if(!f)throw Error(`[svgt] No <svg> root element found`);return f},f=/* @__PURE__ */ new Set([`width`,`height`]),p=(e,t)=>{t||(e.attributes=e.attributes.filter(({name:e})=>!f.has(e)))},m=/^[A-Za-z_][\w:.-]*$/,h=e=>m.test(e),g={dimensions:p,svgProps:(e,t)=>{for(let[n,r]of Object.entries(t)){if(!h(n))throw Error(`[svgt] Invalid attribute name in svgProps: "${n}"`);let t=e.attributes.findIndex(e=>e.name===n);r===null?e.attributes=e.attributes.filter(e=>e.name!==n):t===-1?e.attributes.push({name:n,value:r}):e.attributes[t]={name:n,value:r}}},expandProps:a},_=(e,t)=>{for(let n of Object.keys(g)){let r=t[n];r!==void 0&&g[n](e,r)}},v=[`default`,`directly`],y=(e,t)=>{if(!t)return e;let n;try{n=t(e)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`[svgt] preTransform failed: ${t}`,{cause:e})}if(typeof n!=`string`){let e=n instanceof Promise?`a Promise`:typeof n;throw Error(`[svgt] preTransform must return a string, got ${e}`)}return n},b=(t,n={})=>{let{name:r=`SvgIcon`,exportType:i=`default`,expandProps:a=`end`,...o}=n.output??{};if(!v.includes(i))throw Error(`[svgt] Invalid exportType: ${JSON.stringify(i)}. Use "default" or "directly".`);let s=d(y(t,n.preTransform));return _(s,{...o,expandProps:a}),{root:s,name:e(r),typescript:n.typescript??!0,exportType:i}},x=e=>e.replace(/&/g,`&`).replace(/"/g,`"`).replace(/</g,`<`),S=e=>e.replace(/&/g,`&`).replace(/</g,`<`).replace(/>/g,`>`),C=e=>e.replace(/\{/g,`{`).replace(/\}/g,`}`),w=e=>e.replace(/\\/g,`\\\\`).replace(/`/g,"\\`").replace(/\$\{/g,"\\${"),T=({name:e,value:t},n=x)=>`${e}="${n(t)}"`,E=/* @__PURE__ */ new Set([`style`,`script`]),D=e=>({attribute:e=>T(e,e=>C(x(e))),text:(e,t)=>E.has(t.name)?e:C(S(e)),spread:e}),O=(e,t)=>{t(e);for(let n of e.children)n.type===`element`&&O(n,t)};export{S as a,o as c,w as i,i as l,D as n,T as o,O as r,b as s,E as t};
|
package/dist/vue.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { t as BaseTransformOptions } from "./options-CMZc7e_j.js";
|
|
2
|
+
/**
|
|
3
|
+
* Convert an SVG string into a Vue single-file component.
|
|
4
|
+
*/
|
|
5
|
+
export declare const transform: (svg: string, options?: BaseTransformOptions) => string;
|
|
6
|
+
export type { BaseTransformOptions as TransformOptions };
|
package/dist/vue.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as e,n as t,r as n,s as r,t as i}from"./template-CSV8K9KX.js";const a=(a,o)=>{let{root:s}=r(a,o);return n(s,e=>{i.has(e.name)&&(e.attributes.unshift({name:`is`,value:e.name}),e.name=`component`)}),`<template>\n${e(s,{...t(),indent:1})}\n</template>\n`};export{a as transform};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import{a as e,c as t,i as n,o as r,s as i,t as a}from"./template-CSV8K9KX.js";import{n as o}from"./case-D05A9Slu.js";const s=[`width`,`height`,`fill`,`stroke`,`stroke-width`],c=(c,l)=>{let{root:u,name:d,typescript:f,exportType:p}=i(c,l),m=p===`directly`,h=t(u,{attribute:e=>r(e),text:(t,n)=>a.has(n.name)?t:e(t),indent:1}),g=o(d),_=g.includes(`-`)?g:`svg-${g}`,v=s.map(e=>`"${e}"`).join(`, `),y=e=>f?e:``,b=y(`
|
|
2
|
+
declare global {
|
|
3
|
+
interface HTMLElementTagNameMap {
|
|
4
|
+
"${_}": ${d};
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
`);return`const template = document.createElement("template");
|
|
8
|
+
template.innerHTML = \`
|
|
9
|
+
<style>:host { display: inline-flex; }</style>
|
|
10
|
+
${n(h)}
|
|
11
|
+
\`;
|
|
12
|
+
|
|
13
|
+
${m?`export `:``}class ${d} extends HTMLElement {
|
|
14
|
+
static observedAttributes = [${v}];
|
|
15
|
+
|
|
16
|
+
#svg${y(`: SVGSVGElement`)};
|
|
17
|
+
|
|
18
|
+
constructor() {
|
|
19
|
+
super();
|
|
20
|
+
const shadow = this.attachShadow({ mode: "open" });
|
|
21
|
+
shadow.append(template.content.cloneNode(true));
|
|
22
|
+
this.#svg = ${f?`shadow.querySelector("svg")!`:`/** @type {SVGSVGElement} */ (shadow.querySelector("svg"))`};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
attributeChangedCallback(
|
|
26
|
+
name${y(`: string`)},
|
|
27
|
+
_previous${y(`: string | null`)},
|
|
28
|
+
value${y(`: string | null`)},
|
|
29
|
+
) {
|
|
30
|
+
if (value === null) this.#svg.removeAttribute(name);
|
|
31
|
+
else this.#svg.setAttribute(name, value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!customElements.get("${_}")) {
|
|
36
|
+
customElements.define("${_}", ${d});
|
|
37
|
+
}
|
|
38
|
+
${m?``:`\nexport default ${d};\n`}${b}`};export{c as transform};
|
package/package.json
CHANGED
|
@@ -1,6 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svgt/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A zero-dependency library to transform SVG into framework components.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/pheralb/svgt.git",
|
|
10
|
+
"directory": "packages/core"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/pheralb/svgt#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/pheralb/svgt/issues"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./frameworks": {
|
|
26
|
+
"types": "./dist/frameworks.d.ts",
|
|
27
|
+
"default": "./dist/frameworks.js"
|
|
28
|
+
},
|
|
29
|
+
"./react": {
|
|
30
|
+
"types": "./dist/react.d.ts",
|
|
31
|
+
"default": "./dist/react.js"
|
|
32
|
+
},
|
|
33
|
+
"./preact": {
|
|
34
|
+
"types": "./dist/preact.d.ts",
|
|
35
|
+
"default": "./dist/preact.js"
|
|
36
|
+
},
|
|
37
|
+
"./solid": {
|
|
38
|
+
"types": "./dist/solid.d.ts",
|
|
39
|
+
"default": "./dist/solid.js"
|
|
40
|
+
},
|
|
41
|
+
"./svelte": {
|
|
42
|
+
"types": "./dist/svelte.d.ts",
|
|
43
|
+
"default": "./dist/svelte.js"
|
|
44
|
+
},
|
|
45
|
+
"./vue": {
|
|
46
|
+
"types": "./dist/vue.d.ts",
|
|
47
|
+
"default": "./dist/vue.js"
|
|
48
|
+
},
|
|
49
|
+
"./angular": {
|
|
50
|
+
"types": "./dist/angular.d.ts",
|
|
51
|
+
"default": "./dist/angular.js"
|
|
52
|
+
},
|
|
53
|
+
"./astro": {
|
|
54
|
+
"types": "./dist/astro.d.ts",
|
|
55
|
+
"default": "./dist/astro.js"
|
|
56
|
+
},
|
|
57
|
+
"./web-components": {
|
|
58
|
+
"types": "./dist/web-components.d.ts",
|
|
59
|
+
"default": "./dist/web-components.js"
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"keywords": [
|
|
64
|
+
"svg",
|
|
65
|
+
"transform",
|
|
66
|
+
"frontend"
|
|
67
|
+
],
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@types/node": "24.19.2",
|
|
70
|
+
"@types/react": "19.3.3",
|
|
71
|
+
"@vitest/coverage-v8": "5.1.2",
|
|
72
|
+
"tsdown": "0.23.7",
|
|
73
|
+
"typescript": "6.0.3",
|
|
74
|
+
"vite": "8.4.0",
|
|
75
|
+
"vitest": "5.1.2"
|
|
76
|
+
},
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "tsdown",
|
|
79
|
+
"dev": "tsdown --watch --no-clean",
|
|
80
|
+
"check": "tsc --noEmit",
|
|
81
|
+
"test": "vitest run",
|
|
82
|
+
"test:watch": "vitest",
|
|
83
|
+
"test:coverage": "vitest run --coverage"
|
|
84
|
+
}
|
|
6
85
|
}
|