@wc-toolkit/vuejs-types 1.0.0 → 1.0.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/dist/index.cjs CHANGED
@@ -40,6 +40,42 @@ var import_fs = __toESM(require("fs"), 1);
40
40
  var import_path = __toESM(require("path"), 1);
41
41
 
42
42
  // node_modules/.pnpm/@wc-toolkit+cem-utilities@1.4.1/node_modules/@wc-toolkit/cem-utilities/dist/index.js
43
+ var JS_TYPES = /* @__PURE__ */ new Set([
44
+ "any",
45
+ "bigint",
46
+ "boolean",
47
+ "never",
48
+ "null",
49
+ "number",
50
+ "string",
51
+ "Symbol",
52
+ "undefined",
53
+ "unknown"
54
+ ]);
55
+ var DOM_EVENTS = /* @__PURE__ */ new Set([
56
+ "AnimationEvent",
57
+ "BeforeUnloadEvent",
58
+ "ClipboardEvent",
59
+ "DragEvent",
60
+ "Event",
61
+ "FocusEvent",
62
+ "HashChangeEvent",
63
+ "InputEvent",
64
+ "KeyboardEvent",
65
+ "MessageEvent",
66
+ "MouseEvent",
67
+ "MutationObserver",
68
+ "PageTransitionEvent",
69
+ "PointerEvent",
70
+ "PopStateEvent",
71
+ "ProgressEvent",
72
+ "StorageEvent",
73
+ "TouchEvent",
74
+ "TransitionEvent",
75
+ "UIEvent",
76
+ "WebGLContextEvent",
77
+ "WheelEvent"
78
+ ]);
43
79
  var definitionExports = /* @__PURE__ */ new Map();
44
80
  var components = [];
45
81
  var manifest;
@@ -86,6 +122,16 @@ function getComponentPublicProperties(component) {
86
122
  (member) => member.kind === "field" && member.privacy !== "private" && member.privacy !== "protected" && !member.static && !member.name.startsWith("#")
87
123
  ) || [];
88
124
  }
125
+ function getCustomEventDetailTypes(component, excludedTypes) {
126
+ if (!component || !component.events) {
127
+ return [];
128
+ }
129
+ const types = component?.events?.map((e) => {
130
+ const eventType = e.type?.text.replace("[]", "").replace(" | undefined", "");
131
+ return eventType && !excludedTypes?.includes(eventType) && !JS_TYPES.has(eventType) && !DOM_EVENTS.has(eventType) && !eventType.includes("<") && !eventType.includes(">") && !eventType.includes(`{`) && !eventType.includes("'") && !eventType.includes(`"`) ? eventType : void 0;
132
+ })?.filter((e) => e !== void 0 && !e?.startsWith("HTML")) || [];
133
+ return types?.length ? [...new Set(types)] : [];
134
+ }
89
135
  function setAllDefinitionExports(customElementsManifest) {
90
136
  if (!customElementsManifest) {
91
137
  return;
@@ -339,6 +385,25 @@ function getImports(manifest2, options) {
339
385
  });
340
386
  });
341
387
  }
388
+ getAllComponents(manifest2, options.exclude).forEach((component) => {
389
+ if (!component.name || !component.events) return;
390
+ const componentModule = componentModules.get(component.name);
391
+ if (!componentModule) return;
392
+ const detailTypes = getEventDetailImportTypes(component);
393
+ if (detailTypes.length === 0) return;
394
+ const importPath = options.globalTypePath ? normalizeImportPath(options.globalTypePath, options) : normalizeImportPath(
395
+ getComponentImportPath(
396
+ component.name,
397
+ component.tagName,
398
+ componentModule.modulePath,
399
+ options
400
+ ),
401
+ options
402
+ );
403
+ detailTypes.forEach((typeName) => {
404
+ addImport(imports, importPath, typeName);
405
+ });
406
+ });
342
407
  return Array.from(imports.entries()).map(
343
408
  ([importPath, exportNames]) => `import type { ${Array.from(exportNames).join(", ")} } from "${importPath}";`
344
409
  ).join("\n");
@@ -350,8 +415,7 @@ function getTypeTemplate(manifest2, options) {
350
415
  ${imports}
351
416
  import type { HTMLAttributes, PublicProps, EmitFn, EmitsToProps } from "vue";
352
417
 
353
- /**
354
- * This file was autogenerated by @wc-toolkit/vuejs-types
418
+ /** This file was autogenerated by @wc-toolkit/vuejs-types */
355
419
  ${options.stronglyTypedEvents ? `/**
356
420
  * A generic type for strongly typing custom events with their targets
357
421
  * @template T - The type of the event target (extends EventTarget)
@@ -645,6 +709,20 @@ function getStronglyTypedEvents(component) {
645
709
  });
646
710
  return types.join("\n");
647
711
  }
712
+ function getEventDetailImportTypes(component) {
713
+ const bare = getCustomEventDetailTypes(component);
714
+ const wrapped = (component.events || []).map((event) => {
715
+ const text = event.type?.text?.trim();
716
+ if (!text) return void 0;
717
+ const match = /^CustomEvent<(.+)>$/.exec(text);
718
+ return match?.[1]?.trim();
719
+ }).filter(
720
+ (detail) => detail !== void 0 && /^[A-Z][\w$]*$/.test(detail)
721
+ );
722
+ return [.../* @__PURE__ */ new Set([...bare, ...wrapped])].filter(
723
+ (name) => name !== "CustomEvent" && name !== "Event"
724
+ );
725
+ }
648
726
  function createOutDir(outDir) {
649
727
  if (outDir !== "./" && !import_fs.default.existsSync(outDir)) {
650
728
  import_fs.default.mkdirSync(outDir, { recursive: true });
package/dist/index.js CHANGED
@@ -3,6 +3,42 @@ import fs from "fs";
3
3
  import path from "path";
4
4
 
5
5
  // node_modules/.pnpm/@wc-toolkit+cem-utilities@1.4.1/node_modules/@wc-toolkit/cem-utilities/dist/index.js
6
+ var JS_TYPES = /* @__PURE__ */ new Set([
7
+ "any",
8
+ "bigint",
9
+ "boolean",
10
+ "never",
11
+ "null",
12
+ "number",
13
+ "string",
14
+ "Symbol",
15
+ "undefined",
16
+ "unknown"
17
+ ]);
18
+ var DOM_EVENTS = /* @__PURE__ */ new Set([
19
+ "AnimationEvent",
20
+ "BeforeUnloadEvent",
21
+ "ClipboardEvent",
22
+ "DragEvent",
23
+ "Event",
24
+ "FocusEvent",
25
+ "HashChangeEvent",
26
+ "InputEvent",
27
+ "KeyboardEvent",
28
+ "MessageEvent",
29
+ "MouseEvent",
30
+ "MutationObserver",
31
+ "PageTransitionEvent",
32
+ "PointerEvent",
33
+ "PopStateEvent",
34
+ "ProgressEvent",
35
+ "StorageEvent",
36
+ "TouchEvent",
37
+ "TransitionEvent",
38
+ "UIEvent",
39
+ "WebGLContextEvent",
40
+ "WheelEvent"
41
+ ]);
6
42
  var definitionExports = /* @__PURE__ */ new Map();
7
43
  var components = [];
8
44
  var manifest;
@@ -49,6 +85,16 @@ function getComponentPublicProperties(component) {
49
85
  (member) => member.kind === "field" && member.privacy !== "private" && member.privacy !== "protected" && !member.static && !member.name.startsWith("#")
50
86
  ) || [];
51
87
  }
88
+ function getCustomEventDetailTypes(component, excludedTypes) {
89
+ if (!component || !component.events) {
90
+ return [];
91
+ }
92
+ const types = component?.events?.map((e) => {
93
+ const eventType = e.type?.text.replace("[]", "").replace(" | undefined", "");
94
+ return eventType && !excludedTypes?.includes(eventType) && !JS_TYPES.has(eventType) && !DOM_EVENTS.has(eventType) && !eventType.includes("<") && !eventType.includes(">") && !eventType.includes(`{`) && !eventType.includes("'") && !eventType.includes(`"`) ? eventType : void 0;
95
+ })?.filter((e) => e !== void 0 && !e?.startsWith("HTML")) || [];
96
+ return types?.length ? [...new Set(types)] : [];
97
+ }
52
98
  function setAllDefinitionExports(customElementsManifest) {
53
99
  if (!customElementsManifest) {
54
100
  return;
@@ -302,6 +348,25 @@ function getImports(manifest2, options) {
302
348
  });
303
349
  });
304
350
  }
351
+ getAllComponents(manifest2, options.exclude).forEach((component) => {
352
+ if (!component.name || !component.events) return;
353
+ const componentModule = componentModules.get(component.name);
354
+ if (!componentModule) return;
355
+ const detailTypes = getEventDetailImportTypes(component);
356
+ if (detailTypes.length === 0) return;
357
+ const importPath = options.globalTypePath ? normalizeImportPath(options.globalTypePath, options) : normalizeImportPath(
358
+ getComponentImportPath(
359
+ component.name,
360
+ component.tagName,
361
+ componentModule.modulePath,
362
+ options
363
+ ),
364
+ options
365
+ );
366
+ detailTypes.forEach((typeName) => {
367
+ addImport(imports, importPath, typeName);
368
+ });
369
+ });
305
370
  return Array.from(imports.entries()).map(
306
371
  ([importPath, exportNames]) => `import type { ${Array.from(exportNames).join(", ")} } from "${importPath}";`
307
372
  ).join("\n");
@@ -313,8 +378,7 @@ function getTypeTemplate(manifest2, options) {
313
378
  ${imports}
314
379
  import type { HTMLAttributes, PublicProps, EmitFn, EmitsToProps } from "vue";
315
380
 
316
- /**
317
- * This file was autogenerated by @wc-toolkit/vuejs-types
381
+ /** This file was autogenerated by @wc-toolkit/vuejs-types */
318
382
  ${options.stronglyTypedEvents ? `/**
319
383
  * A generic type for strongly typing custom events with their targets
320
384
  * @template T - The type of the event target (extends EventTarget)
@@ -608,6 +672,20 @@ function getStronglyTypedEvents(component) {
608
672
  });
609
673
  return types.join("\n");
610
674
  }
675
+ function getEventDetailImportTypes(component) {
676
+ const bare = getCustomEventDetailTypes(component);
677
+ const wrapped = (component.events || []).map((event) => {
678
+ const text = event.type?.text?.trim();
679
+ if (!text) return void 0;
680
+ const match = /^CustomEvent<(.+)>$/.exec(text);
681
+ return match?.[1]?.trim();
682
+ }).filter(
683
+ (detail) => detail !== void 0 && /^[A-Z][\w$]*$/.test(detail)
684
+ );
685
+ return [.../* @__PURE__ */ new Set([...bare, ...wrapped])].filter(
686
+ (name) => name !== "CustomEvent" && name !== "Event"
687
+ );
688
+ }
611
689
  function createOutDir(outDir) {
612
690
  if (outDir !== "./" && !fs.existsSync(outDir)) {
613
691
  fs.mkdirSync(outDir, { recursive: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wc-toolkit/vuejs-types",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "This package generates Vue.js types for custom elements / web components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",