@ts-svg/svelte 0.0.1-beta.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/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@ts-svg/svelte",
3
+ "version": "0.0.1-beta.0",
4
+ "description": "Vite Plugin for loading all svg files inside a folder with types.",
5
+ "keywords": [
6
+ "vite-plugin",
7
+ "vite",
8
+ "svg",
9
+ "typescript",
10
+ "svelte"
11
+ ],
12
+ "author": {
13
+ "name": "Chun Nam Wong",
14
+ "url": "https://chunnamwong.com"
15
+ },
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/chunnamwong/ts-svg.git",
20
+ "directory": "packages/svelte"
21
+ },
22
+ "type": "module",
23
+ "main": "src/index.js",
24
+ "types": "src/index.d.ts",
25
+ "bin": {
26
+ "ts-svg": "src/cli.js"
27
+ },
28
+ "files": [
29
+ "src"
30
+ ],
31
+ "peerDependencies": {
32
+ "svelte": ">=4.0.0"
33
+ },
34
+ "dependencies": {
35
+ "svgo": "^4.0.0",
36
+ "@ts-svg/core": "^0.0.1-beta.0"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^24.6.0"
40
+ }
41
+ }
package/src/cli.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { main } from '@ts-svg/core/cli';
4
+
5
+ await main();
package/src/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { PluginOption } from 'vite';
2
+ import type { Config } from 'svgo';
3
+
4
+ type Options = {
5
+ path: string;
6
+ svgoConfig?: Config;
7
+ };
8
+
9
+ export function tsSvg(opts: Options): PluginOption;
package/src/index.js ADDED
@@ -0,0 +1,68 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { tsSvg as tsSvgCore } from '@ts-svg/core';
3
+ import { optimize } from 'svgo';
4
+ import { compile } from 'svelte/compiler';
5
+
6
+ /**
7
+ * @typedef Options
8
+ * @type {object}
9
+ * @property {string} path The svg folder path
10
+ * @property {import('svgo').Config | undefined} svgoConfig
11
+ */
12
+
13
+ /** @type {import('svgo').Config} */
14
+ const defaultSvgoConfig = {
15
+ plugins: [
16
+ 'preset-default',
17
+ 'removeDimensions',
18
+ 'convertColors',
19
+ 'removeUselessStrokeAndFill',
20
+ {
21
+ name: 'addAttributesToSVGElement',
22
+ params: {
23
+ attribute: {
24
+ role: 'img',
25
+ 'aria-hidden': 'true',
26
+ },
27
+ },
28
+ },
29
+ ],
30
+ };
31
+
32
+ /**
33
+ * Returns the Vite plugin.
34
+ * @param {Options} options
35
+ * @returns {import('vite').PluginOption & import('@ts-svg/core').Options}
36
+ */
37
+ export function tsSvg(options) {
38
+ return tsSvgCore({
39
+ path: options.path,
40
+ query: '?svelte',
41
+ svgModuleDeclaration: `
42
+ declare module '*.svg?svelte' {
43
+ import type { Component } from 'svelte';
44
+ const component: Component;
45
+ export default component;
46
+ }
47
+ `,
48
+ async transform(_code, id) {
49
+ if (!id.endsWith('.svg?svelte')) {
50
+ return;
51
+ }
52
+ const svgPath = id.replace(/\.svg\?svelte$/, '.svg');
53
+ const svgSource = await readFile(svgPath, 'utf-8');
54
+ const { data: optimizedSvgSource } = optimize(
55
+ svgSource,
56
+ options.svgoConfig ?? defaultSvgoConfig,
57
+ );
58
+ const componentCode = optimizedSvgSource.replace(/<svg([^>]*)>/, '<svg$1 {...$$$$props}>');
59
+ const component = compile(componentCode, {
60
+ generate: this.environment.name === 'ssr' ? 'server' : undefined,
61
+ });
62
+ return {
63
+ code: component.js.code,
64
+ map: { mappings: '' },
65
+ };
66
+ },
67
+ });
68
+ }