@wc-toolkit/cem-generator-svelte 0.1.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 wc-toolkit contributors
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 ADDED
@@ -0,0 +1,18 @@
1
+ # @wc-toolkit/cem-generator-svelte
2
+
3
+ Svelte detector plugin for components compiled as custom elements with
4
+ `<svelte:options customElement="..." />`.
5
+
6
+ ```ts
7
+ import { sveltePlugin } from "@wc-toolkit/cem-generator-svelte";
8
+
9
+ const manifest = generateCem({
10
+ plugins: [sveltePlugin()],
11
+ });
12
+ ```
13
+
14
+ The plugin analyzes `.svelte` files, extracting props, slots, parts, CSS
15
+ custom properties, static events, and component JSDoc metadata.
16
+
17
+ Run the included demo with `pnpm demo:svelte`. It writes the generated manifest
18
+ to `demo/custom-elements.json`.
@@ -0,0 +1,3 @@
1
+ import { type DetectorPlugin } from "@wc-toolkit/cem-generator";
2
+ /** Detects Svelte components compiled as custom elements. */
3
+ export declare function sveltePlugin(): DetectorPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,154 @@
1
+ import path from "node:path";
2
+ import { parseCssMetadata, } from "@wc-toolkit/cem-generator";
3
+ /** Detects Svelte components compiled as custom elements. */
4
+ export function sveltePlugin() {
5
+ return {
6
+ name: "svelte",
7
+ shouldAnalyze(sourceText, filePath) {
8
+ return filePath.endsWith(".svelte") && /<svelte:options\b[^>]*customElement/.test(sourceText);
9
+ },
10
+ onFile(context) {
11
+ const source = context.sourceText;
12
+ const tagName = getTagName(source);
13
+ if (!tagName)
14
+ return {};
15
+ const props = getProps(source);
16
+ const metadata = getComponentMetadata(source, tagName);
17
+ const fragment = {
18
+ name: getComponentName(context.filePath),
19
+ module: context.filePath,
20
+ description: metadata.description,
21
+ summary: metadata.summary,
22
+ deprecated: metadata.deprecated,
23
+ tagName: metadata.tagName ?? tagName,
24
+ members: props.length ? props.map(({ attribute: _attribute, ...member }) => member) : undefined,
25
+ attributes: props.length ? props.map(({ name: fieldName, type, parsedType, description, attribute }) => ({
26
+ name: attribute ?? fieldName,
27
+ type,
28
+ parsedType,
29
+ description,
30
+ fieldName,
31
+ })) : undefined,
32
+ events: mergeNamed(discoverEvents(source), metadata.events),
33
+ slots: mergeNamed(discoverSlots(source), metadata.slots),
34
+ cssParts: mergeNamed(discoverParts(source), metadata.cssParts),
35
+ cssProperties: mergeNamed(discoverCssProperties(source), metadata.cssProperties),
36
+ cssStates: metadata.cssStates,
37
+ };
38
+ return { [fragment.name]: fragment };
39
+ },
40
+ };
41
+ }
42
+ function getTagName(source) {
43
+ const options = source.match(/<svelte:options\b[^>]*customElement\s*=\s*(?:["']([^"']+)["']|\{\{?\s*tag\s*:\s*["']([^"']+)["'])/s);
44
+ return options?.[1] ?? options?.[2];
45
+ }
46
+ function getComponentName(filePath) {
47
+ return path.basename(filePath, ".svelte").replace(/[^A-Za-z0-9]+(.)/g, (_match, character) => character.toUpperCase());
48
+ }
49
+ function getProps(source) {
50
+ const props = [];
51
+ const script = source.match(/<script\b[^>]*>([\s\S]*?)<\/script>/)?.[1] ?? "";
52
+ const interfaces = new Map();
53
+ for (const match of script.matchAll(/(?:\/\*\*([\s\S]*?)\*\/\s*)?(\w+)\??\s*:\s*([^;\n,}]+)/g)) {
54
+ interfaces.set(match[2], { type: match[3].trim(), description: cleanComment(match[1]) });
55
+ }
56
+ for (const match of script.matchAll(/(?:\/\*\*([\s\S]*?)\*\/\s*)?export\s+let\s+(\w+)\s*(?::\s*([^=;]+))?\s*(?:=\s*([^;\n]+))?/g)) {
57
+ const [, comment, name, type, defaultValue] = match;
58
+ props.push({ name, kind: "field", type: type?.trim() ?? inferType(defaultValue), description: cleanComment(comment), default: defaultValue?.trim(), attribute: name.toLowerCase() });
59
+ }
60
+ const runeProps = script.match(/\b(?:let|const)\s*\{([\s\S]*?)\}\s*(?::\s*(\w+))?\s*=\s*\$props\s*\(\s*\)/)?.[1];
61
+ for (const entry of runeProps?.split(",") ?? []) {
62
+ const match = entry.trim().match(/^(\w+)(?:\s*=\s*(.+))?$/);
63
+ if (!match || props.some((prop) => prop.name === match[1]))
64
+ continue;
65
+ const info = interfaces.get(match[1]);
66
+ props.push({ name: match[1], kind: "field", type: info?.type ?? inferType(match[2]), description: info?.description, default: match[2]?.trim(), attribute: match[1].toLowerCase() });
67
+ }
68
+ const options = source.match(/<svelte:options\b([\s\S]*?)\/>/)?.[1] ?? "";
69
+ for (const match of options.matchAll(/(\w+)\s*:\s*\{[^}]*?attribute\s*:\s*["']([^"']+)["'][^}]*\}/g)) {
70
+ const prop = props.find((item) => item.name === match[1]);
71
+ if (prop)
72
+ prop.attribute = match[2];
73
+ }
74
+ return props;
75
+ }
76
+ function getComponentMetadata(source, tagName) {
77
+ const comment = source.match(/\/\*\*([\s\S]*?)\*\/\s*<svelte:options/)?.[1] ?? "";
78
+ const description = cleanComment(comment.replace(/\n\s*\*\s*@\w+[\s\S]*/, ""));
79
+ const summary = getTagDescription(comment, "summary");
80
+ const deprecated = getTagDescription(comment, "deprecated");
81
+ return {
82
+ description,
83
+ summary,
84
+ deprecated: deprecated || undefined,
85
+ tagName,
86
+ events: parseNamedTags(comment, "event"),
87
+ slots: parseNamedTags(comment, "slot"),
88
+ cssParts: parseNamedTags(comment, "csspart"),
89
+ cssProperties: parseCssTags(comment),
90
+ cssStates: parseNamedTags(comment, "cssState"),
91
+ };
92
+ }
93
+ function discoverSlots(source) {
94
+ return [...source.matchAll(/<slot(?:\s+name\s*=\s*["']([^"']+)["'])?[^>]*>/g)].map((match) => ({ name: match[1] ?? "", description: trailingHtmlComment(source.slice(0, match.index ?? 0)) }));
95
+ }
96
+ function discoverParts(source) {
97
+ return [...source.matchAll(/<[^>]*\bpart\s*=\s*["']([^"']+)["'][^>]*>/g)].flatMap((match) => match[1].split(/\s+/).filter(Boolean).map((name) => ({ name, description: trailingHtmlComment(source.slice(0, match.index ?? 0)) })));
98
+ }
99
+ function discoverCssProperties(source) {
100
+ const properties = [];
101
+ for (const match of source.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/g))
102
+ properties.push(...(parseCssMetadata(match[1]) ?? []));
103
+ return properties;
104
+ }
105
+ function discoverEvents(source) {
106
+ const events = [];
107
+ for (const match of source.matchAll(/(?:dispatch\s*\(\s*|dispatchEvent\s*\(\s*new\s+CustomEvent\s*\(\s*)["']([^"']+)["']/g))
108
+ events.push({ name: match[1], type: "CustomEvent" });
109
+ return events;
110
+ }
111
+ function parseNamedTags(comment, tag) {
112
+ const values = [...comment.matchAll(new RegExp(`@${tag}\\s+([^\\s-]+|-)\\s*(?:-\\s*)?([^\\n]*)`, "g"))].map((match) => ({ name: match[1] === "-" ? "" : match[1], description: match[2]?.trim() || undefined }));
113
+ return values.length ? values : undefined;
114
+ }
115
+ function parseCssTags(comment) {
116
+ const values = [...comment.matchAll(/@cssprop(?:erty)?\s+(?:\[([^=\]]+)=([^\]]+)\]|(\S+))\s*(?:-\s*)?([^\n]*)/g)].map((match) => ({ name: match[1] ?? match[3], default: match[2], description: match[4]?.trim() || undefined }));
117
+ return values.length ? values : undefined;
118
+ }
119
+ function getTagDescription(comment, tag) {
120
+ return comment.match(new RegExp(`@${tag}\\s+([^\\n]*)`))?.[1]?.trim() || undefined;
121
+ }
122
+ function cleanComment(value) {
123
+ if (!value)
124
+ return undefined;
125
+ const text = value.split("\n").map((line) => line.replace(/^\s*\*\s?/, "")).join(" ").replace(/\s+/g, " ").trim();
126
+ return text || undefined;
127
+ }
128
+ function trailingHtmlComment(value) {
129
+ const end = value.lastIndexOf("-->");
130
+ if (end < 0 || value.slice(end + 3).trim())
131
+ return undefined;
132
+ const start = value.lastIndexOf("<!--", end);
133
+ return start >= 0 ? cleanComment(value.slice(start + 4, end)) : undefined;
134
+ }
135
+ function inferType(value) {
136
+ if (!value)
137
+ return undefined;
138
+ if (/^['"]/.test(value))
139
+ return "string";
140
+ if (/^(true|false)$/.test(value))
141
+ return "boolean";
142
+ if (/^\d/.test(value))
143
+ return "number";
144
+ if (/^[\[{]/.test(value))
145
+ return value.startsWith("[") ? "array" : "object";
146
+ return undefined;
147
+ }
148
+ function mergeNamed(...sources) {
149
+ const values = new Map();
150
+ for (const source of sources)
151
+ for (const item of source ?? [])
152
+ values.set(item.name, { ...values.get(item.name), ...item });
153
+ return values.size ? [...values.values()] : undefined;
154
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@wc-toolkit/cem-generator-svelte",
3
+ "version": "0.1.1",
4
+ "description": "Svelte detector plugin for generating Custom Elements Manifests.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/wc-toolkit/cem-generator.git",
9
+ "directory": "packages/plugins/svelte"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/wc-toolkit/cem-generator/issues"
13
+ },
14
+ "homepage": "https://wc-toolkit.github.io/cem-generator/plugins/svelte/",
15
+ "keywords": [
16
+ "custom-elements",
17
+ "custom-elements-manifest",
18
+ "web-components",
19
+ "svelte"
20
+ ],
21
+ "type": "module",
22
+ "main": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "publishConfig": {
28
+ "access": "public",
29
+ "provenance": true
30
+ },
31
+ "dependencies": {
32
+ "typescript": "^5.9.0",
33
+ "@wc-toolkit/cem-generator": "0.1.1",
34
+ "@wc-toolkit/cem-generator-utils": "0.1.0"
35
+ },
36
+ "devDependencies": {
37
+ "svelte": "^5.0.0"
38
+ },
39
+ "scripts": {
40
+ "test": "node --test tests/*.test.mjs",
41
+ "demo": "tsx demo/run.ts"
42
+ }
43
+ }