@wc-toolkit/cem-generator-vue 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,17 @@
1
+ # @wc-toolkit/cem-generator-vue
2
+
3
+ Vue detector plugin for custom elements created with `defineCustomElement`.
4
+
5
+ ```ts
6
+ import { vuePlugin } from "@wc-toolkit/cem-generator-vue";
7
+
8
+ const manifest = generateCem({
9
+ plugins: [vuePlugin()],
10
+ });
11
+ ```
12
+
13
+ The plugin detects `customElements.define` registrations and extracts Vue
14
+ `props` and `emits` metadata.
15
+
16
+ Run the included demo with `pnpm demo:vue`. It writes the generated manifest
17
+ to `demo/custom-elements.json`.
@@ -0,0 +1,3 @@
1
+ import { type DetectorPlugin } from "@wc-toolkit/cem-generator";
2
+ /** Detects custom elements created with Vue's `defineCustomElement`. */
3
+ export declare function vuePlugin(): DetectorPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,204 @@
1
+ import ts from "typescript";
2
+ import { discoverFrameworkApis, } from "@wc-toolkit/cem-generator";
3
+ import { getJSDocInfo, parseCemClassTags } from "@wc-toolkit/cem-generator-utils";
4
+ /** Detects custom elements created with Vue's `defineCustomElement`. */
5
+ export function vuePlugin() {
6
+ return {
7
+ name: "vue",
8
+ shouldAnalyze(sourceText) {
9
+ return /from\s+["']vue["']/.test(sourceText) &&
10
+ /\bdefineCustomElement\s*\(/.test(sourceText);
11
+ },
12
+ onFile(context) {
13
+ const fragment = {};
14
+ const defineNames = getDefineCustomElementNames(context.sourceFile);
15
+ const tags = getCustomElementRegistrations(context.sourceFile);
16
+ ts.forEachChild(context.sourceFile, function visit(node) {
17
+ if (!ts.isCallExpression(node) || !isDefineCustomElementCall(node, defineNames)) {
18
+ ts.forEachChild(node, visit);
19
+ return;
20
+ }
21
+ const declaration = findContainingVariable(node);
22
+ const name = declaration?.name && ts.isIdentifier(declaration.name) ? declaration.name.text : undefined;
23
+ if (!name) {
24
+ ts.forEachChild(node, visit);
25
+ return;
26
+ }
27
+ const component = node.arguments[0];
28
+ const options = resolveComponentOptions(component, context);
29
+ const classDoc = declaration ? parseCemClassTags(declaration) : {};
30
+ const props = options ? getProps(options, context) : undefined;
31
+ const discovered = options ? discoverFrameworkApis(options, context.sourceFile) : {};
32
+ const attributes = props?.map(({ name: fieldName, ...member }) => ({
33
+ name: fieldName,
34
+ type: member.type,
35
+ description: member.description,
36
+ fieldName,
37
+ }));
38
+ const events = options ? getEmits(options) : undefined;
39
+ fragment[name] = {
40
+ name,
41
+ exportName: getExportName(declaration),
42
+ module: context.filePath,
43
+ description: (declaration ? getJSDocInfo(declaration).description : undefined) || (options ? getJSDocInfo(options).description || undefined : undefined),
44
+ summary: classDoc.summary,
45
+ deprecated: classDoc.deprecated,
46
+ tagName: classDoc.tagName ?? tags.get(name),
47
+ members: props?.length ? props.map(({ name: _name, ...member }) => ({ name: _name, ...member })) : undefined,
48
+ attributes: attributes?.length ? attributes : undefined,
49
+ events: mergeEvents(mergeEvents(events, discovered.events), classDoc.events),
50
+ slots: mergeNamed(discovered.slots, classDoc.slots),
51
+ cssParts: mergeNamed(discovered.cssParts, classDoc.cssParts),
52
+ cssProperties: mergeNamed(discovered.cssProperties, classDoc.cssProperties),
53
+ cssStates: mergeNamed(discovered.cssStates, classDoc.cssStates),
54
+ omitInherited: classDoc.omitInherited,
55
+ customJsDocTags: classDoc.customJsDocTags,
56
+ };
57
+ ts.forEachChild(node, visit);
58
+ });
59
+ return fragment;
60
+ },
61
+ };
62
+ }
63
+ function mergeNamed(discovered, documented) {
64
+ const values = new Map();
65
+ for (const item of discovered ?? [])
66
+ values.set(item.name, item);
67
+ for (const item of documented ?? [])
68
+ values.set(item.name, { ...values.get(item.name), ...item });
69
+ return values.size ? [...values.values()] : undefined;
70
+ }
71
+ function getDefineCustomElementNames(sourceFile) {
72
+ const names = new Set();
73
+ for (const statement of sourceFile.statements) {
74
+ if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier) || statement.moduleSpecifier.text !== "vue")
75
+ continue;
76
+ const bindings = statement.importClause?.namedBindings;
77
+ if (!bindings || !ts.isNamedImports(bindings))
78
+ continue;
79
+ for (const element of bindings.elements) {
80
+ if ((element.propertyName?.text ?? element.name.text) === "defineCustomElement")
81
+ names.add(element.name.text);
82
+ }
83
+ }
84
+ return names;
85
+ }
86
+ function isDefineCustomElementCall(node, names) {
87
+ return ts.isIdentifier(node.expression) && names.has(node.expression.text);
88
+ }
89
+ function getCustomElementRegistrations(sourceFile) {
90
+ const tags = new Map();
91
+ function visit(node) {
92
+ if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) &&
93
+ node.expression.getText(sourceFile) === "customElements.define") {
94
+ const tag = node.arguments[0];
95
+ const constructor = node.arguments[1];
96
+ if (tag && ts.isStringLiteralLike(tag) && constructor && ts.isIdentifier(constructor))
97
+ tags.set(constructor.text, tag.text);
98
+ }
99
+ ts.forEachChild(node, visit);
100
+ }
101
+ visit(sourceFile);
102
+ return tags;
103
+ }
104
+ function findContainingVariable(node) {
105
+ let current = node.parent;
106
+ while (current && !ts.isSourceFile(current)) {
107
+ if (ts.isVariableDeclaration(current))
108
+ return current;
109
+ current = current.parent;
110
+ }
111
+ return undefined;
112
+ }
113
+ function resolveComponentOptions(component, context) {
114
+ if (!component)
115
+ return undefined;
116
+ if (ts.isObjectLiteralExpression(component))
117
+ return component;
118
+ if (!ts.isIdentifier(component))
119
+ return undefined;
120
+ const symbol = context.checker.getSymbolAtLocation(component);
121
+ const resolved = symbol && symbol.flags & ts.SymbolFlags.Alias ? context.checker.getAliasedSymbol(symbol) : symbol;
122
+ const declaration = resolved?.declarations?.find(ts.isVariableDeclaration);
123
+ const initializer = declaration?.initializer;
124
+ if (initializer && ts.isObjectLiteralExpression(initializer))
125
+ return initializer;
126
+ if (initializer && ts.isCallExpression(initializer) && ts.isIdentifier(initializer.expression) && initializer.expression.text === "defineComponent") {
127
+ return initializer.arguments[0] && ts.isObjectLiteralExpression(initializer.arguments[0]) ? initializer.arguments[0] : undefined;
128
+ }
129
+ return undefined;
130
+ }
131
+ function getProps(options, context) {
132
+ const props = getObjectProperty(options, "props");
133
+ if (!props || !ts.isObjectLiteralExpression(props.initializer))
134
+ return [];
135
+ return props.initializer.properties.flatMap((property) => {
136
+ if (!ts.isPropertyAssignment(property))
137
+ return [];
138
+ const name = getPropertyName(property.name);
139
+ if (!name)
140
+ return [];
141
+ const metadata = getPropMetadata(property.initializer, context);
142
+ return [{ name, kind: "field", type: metadata.type, description: getJSDocInfo(property).description || undefined, default: metadata.default }];
143
+ });
144
+ }
145
+ function getPropMetadata(initializer, context) {
146
+ if (ts.isObjectLiteralExpression(initializer)) {
147
+ const type = getObjectProperty(initializer, "type");
148
+ const defaultValue = getObjectProperty(initializer, "default");
149
+ return { type: type && getVueType(type.initializer, context), default: defaultValue?.initializer.getText() };
150
+ }
151
+ return { type: getVueType(initializer, context) };
152
+ }
153
+ function getVueType(node, context) {
154
+ const name = ts.isIdentifier(node) ? node.text : undefined;
155
+ if (name === "String")
156
+ return "string";
157
+ if (name === "Number")
158
+ return "number";
159
+ if (name === "Boolean")
160
+ return "boolean";
161
+ if (name === "Object")
162
+ return "object";
163
+ if (name === "Array")
164
+ return "array";
165
+ return context.checker.typeToString(context.checker.getTypeAtLocation(node));
166
+ }
167
+ function getEmits(options) {
168
+ const emits = getObjectProperty(options, "emits");
169
+ if (!emits)
170
+ return undefined;
171
+ if (ts.isArrayLiteralExpression(emits.initializer)) {
172
+ return emits.initializer.elements.flatMap((event) => ts.isStringLiteralLike(event) ? [{ name: event.text }] : []);
173
+ }
174
+ if (ts.isObjectLiteralExpression(emits.initializer)) {
175
+ return emits.initializer.properties.flatMap((property) => {
176
+ const name = property.name && getPropertyName(property.name);
177
+ return name ? [{ name }] : [];
178
+ });
179
+ }
180
+ return undefined;
181
+ }
182
+ function mergeEvents(emitted, documented) {
183
+ const events = new Map();
184
+ for (const event of emitted ?? [])
185
+ events.set(event.name, event);
186
+ for (const event of documented ?? [])
187
+ events.set(event.name, { ...events.get(event.name), ...event });
188
+ return events.size ? [...events.values()] : undefined;
189
+ }
190
+ function getObjectProperty(object, name) {
191
+ return object.properties.find((property) => ts.isPropertyAssignment(property) && getPropertyName(property.name) === name);
192
+ }
193
+ function getPropertyName(name) {
194
+ return ts.isIdentifier(name) || ts.isStringLiteralLike(name) ? name.text : undefined;
195
+ }
196
+ function getExportName(node) {
197
+ const declaration = node?.parent.parent;
198
+ if (!declaration || !ts.isVariableStatement(declaration))
199
+ return undefined;
200
+ const modifiers = ts.canHaveModifiers(declaration) ? ts.getModifiers(declaration) : undefined;
201
+ if (!modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword))
202
+ return undefined;
203
+ return modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword) ? "default" : node?.name.getText();
204
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@wc-toolkit/cem-generator-vue",
3
+ "version": "0.1.1",
4
+ "description": "Vue 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/vue"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/wc-toolkit/cem-generator/issues"
13
+ },
14
+ "homepage": "https://wc-toolkit.github.io/cem-generator/plugins/vue/",
15
+ "keywords": [
16
+ "custom-elements",
17
+ "custom-elements-manifest",
18
+ "web-components",
19
+ "vue"
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
+ "vue": "^3.5.0"
38
+ },
39
+ "scripts": {
40
+ "test": "node --test tests/*.test.mjs",
41
+ "demo": "tsx demo/run.ts"
42
+ }
43
+ }