@wc-toolkit/vuejs-types 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -19,10 +19,11 @@ Types are generated from a Custom Elements Manifest (CEM) and include documentat
19
19
 
20
20
  ## Usage
21
21
 
22
- Two primary ways to generate types:
22
+ Three primary ways to generate types:
23
23
 
24
24
  1. Programmatically, from a build script
25
25
  2. As a plugin for the Custom Elements Manifest Analyzer
26
+ 3. As a plugin for `@wc-toolkit/cem-generator`
26
27
 
27
28
  ### Install
28
29
 
@@ -65,6 +66,17 @@ export default {
65
66
  };
66
67
  ```
67
68
 
69
+ ### cem-generator Plugin
70
+
71
+ ```ts
72
+ import { generateCem } from "@wc-toolkit/cem-generator";
73
+ import { vuejsTypesGeneratorPlugin } from "@wc-toolkit/vuejs-types";
74
+
75
+ generateCem({
76
+ plugins: [vuejsTypesGeneratorPlugin({ outdir: "./dist/types" })],
77
+ });
78
+ ```
79
+
68
80
  ## Implementation / Integrating generated types
69
81
 
70
82
  There are two common ways consumers can include the generated types in their Vue projects.
package/dist/index.cjs CHANGED
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  generateVuejsTypes: () => generateVuejsTypes,
34
+ vuejsTypesGeneratorPlugin: () => vuejsTypesGeneratorPlugin,
34
35
  vuejsTypesPlugin: () => vuejsTypesPlugin
35
36
  });
36
37
  module.exports = __toCommonJS(index_exports);
@@ -40,6 +41,42 @@ var import_fs = __toESM(require("fs"), 1);
40
41
  var import_path = __toESM(require("path"), 1);
41
42
 
42
43
  // node_modules/.pnpm/@wc-toolkit+cem-utilities@1.4.1/node_modules/@wc-toolkit/cem-utilities/dist/index.js
44
+ var JS_TYPES = /* @__PURE__ */ new Set([
45
+ "any",
46
+ "bigint",
47
+ "boolean",
48
+ "never",
49
+ "null",
50
+ "number",
51
+ "string",
52
+ "Symbol",
53
+ "undefined",
54
+ "unknown"
55
+ ]);
56
+ var DOM_EVENTS = /* @__PURE__ */ new Set([
57
+ "AnimationEvent",
58
+ "BeforeUnloadEvent",
59
+ "ClipboardEvent",
60
+ "DragEvent",
61
+ "Event",
62
+ "FocusEvent",
63
+ "HashChangeEvent",
64
+ "InputEvent",
65
+ "KeyboardEvent",
66
+ "MessageEvent",
67
+ "MouseEvent",
68
+ "MutationObserver",
69
+ "PageTransitionEvent",
70
+ "PointerEvent",
71
+ "PopStateEvent",
72
+ "ProgressEvent",
73
+ "StorageEvent",
74
+ "TouchEvent",
75
+ "TransitionEvent",
76
+ "UIEvent",
77
+ "WebGLContextEvent",
78
+ "WheelEvent"
79
+ ]);
43
80
  var definitionExports = /* @__PURE__ */ new Map();
44
81
  var components = [];
45
82
  var manifest;
@@ -86,6 +123,16 @@ function getComponentPublicProperties(component) {
86
123
  (member) => member.kind === "field" && member.privacy !== "private" && member.privacy !== "protected" && !member.static && !member.name.startsWith("#")
87
124
  ) || [];
88
125
  }
126
+ function getCustomEventDetailTypes(component, excludedTypes) {
127
+ if (!component || !component.events) {
128
+ return [];
129
+ }
130
+ const types = component?.events?.map((e) => {
131
+ const eventType = e.type?.text.replace("[]", "").replace(" | undefined", "");
132
+ 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;
133
+ })?.filter((e) => e !== void 0 && !e?.startsWith("HTML")) || [];
134
+ return types?.length ? [...new Set(types)] : [];
135
+ }
89
136
  function setAllDefinitionExports(customElementsManifest) {
90
137
  if (!customElementsManifest) {
91
138
  return;
@@ -339,6 +386,25 @@ function getImports(manifest2, options) {
339
386
  });
340
387
  });
341
388
  }
389
+ getAllComponents(manifest2, options.exclude).forEach((component) => {
390
+ if (!component.name || !component.events) return;
391
+ const componentModule = componentModules.get(component.name);
392
+ if (!componentModule) return;
393
+ const detailTypes = getEventDetailImportTypes(component);
394
+ if (detailTypes.length === 0) return;
395
+ const importPath = options.globalTypePath ? normalizeImportPath(options.globalTypePath, options) : normalizeImportPath(
396
+ getComponentImportPath(
397
+ component.name,
398
+ component.tagName,
399
+ componentModule.modulePath,
400
+ options
401
+ ),
402
+ options
403
+ );
404
+ detailTypes.forEach((typeName) => {
405
+ addImport(imports, importPath, typeName);
406
+ });
407
+ });
342
408
  return Array.from(imports.entries()).map(
343
409
  ([importPath, exportNames]) => `import type { ${Array.from(exportNames).join(", ")} } from "${importPath}";`
344
410
  ).join("\n");
@@ -350,8 +416,7 @@ function getTypeTemplate(manifest2, options) {
350
416
  ${imports}
351
417
  import type { HTMLAttributes, PublicProps, EmitFn, EmitsToProps } from "vue";
352
418
 
353
- /**
354
- * This file was autogenerated by @wc-toolkit/vuejs-types
419
+ /** This file was autogenerated by @wc-toolkit/vuejs-types */
355
420
  ${options.stronglyTypedEvents ? `/**
356
421
  * A generic type for strongly typing custom events with their targets
357
422
  * @template T - The type of the event target (extends EventTarget)
@@ -645,6 +710,20 @@ function getStronglyTypedEvents(component) {
645
710
  });
646
711
  return types.join("\n");
647
712
  }
713
+ function getEventDetailImportTypes(component) {
714
+ const bare = getCustomEventDetailTypes(component);
715
+ const wrapped = (component.events || []).map((event) => {
716
+ const text = event.type?.text?.trim();
717
+ if (!text) return void 0;
718
+ const match = /^CustomEvent<(.+)>$/.exec(text);
719
+ return match?.[1]?.trim();
720
+ }).filter(
721
+ (detail) => detail !== void 0 && /^[A-Z][\w$]*$/.test(detail)
722
+ );
723
+ return [.../* @__PURE__ */ new Set([...bare, ...wrapped])].filter(
724
+ (name) => name !== "CustomEvent" && name !== "Event"
725
+ );
726
+ }
648
727
  function createOutDir(outDir) {
649
728
  if (outDir !== "./" && !import_fs.default.existsSync(outDir)) {
650
729
  import_fs.default.mkdirSync(outDir, { recursive: true });
@@ -668,8 +747,17 @@ function vuejsTypesPlugin(options = {}) {
668
747
  }
669
748
  };
670
749
  }
750
+ function vuejsTypesGeneratorPlugin(options = {}) {
751
+ return {
752
+ name: "@wc-toolkit/vuejs-types:cem-generator",
753
+ afterGenerate(manifest2) {
754
+ generateVuejsTypes(manifest2, options);
755
+ }
756
+ };
757
+ }
671
758
  // Annotate the CommonJS export names for ESM import in node:
672
759
  0 && (module.exports = {
673
760
  generateVuejsTypes,
761
+ vuejsTypesGeneratorPlugin,
674
762
  vuejsTypesPlugin
675
763
  });
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ComponentDescriptionOptions } from '@wc-toolkit/cem-utilities';
2
+ import { Plugin } from '@wc-toolkit/cem-generator';
2
3
 
3
4
  type VuejsTypesOptions = {
4
5
  /** Used to get a specific path for a given component */
@@ -55,6 +56,8 @@ declare function vuejsTypesPlugin(options?: VuejsTypesOptions): {
55
56
  name: string;
56
57
  packageLinkPhase({ customElementsManifest }: any): void;
57
58
  };
59
+ /** Plugin for @wc-toolkit/cem-generator that generates Vue.js types from the finalized CEM. */
60
+ declare function vuejsTypesGeneratorPlugin(options?: VuejsTypesOptions): Plugin;
58
61
 
59
62
  /**
60
63
  * @license
@@ -811,4 +814,4 @@ interface Demo {
811
814
  */
812
815
  declare function generateVuejsTypes(manifest: Package, options?: VuejsTypesOptions): string | undefined;
813
816
 
814
- export { generateVuejsTypes, vuejsTypesPlugin };
817
+ export { generateVuejsTypes, vuejsTypesGeneratorPlugin, vuejsTypesPlugin };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ComponentDescriptionOptions } from '@wc-toolkit/cem-utilities';
2
+ import { Plugin } from '@wc-toolkit/cem-generator';
2
3
 
3
4
  type VuejsTypesOptions = {
4
5
  /** Used to get a specific path for a given component */
@@ -55,6 +56,8 @@ declare function vuejsTypesPlugin(options?: VuejsTypesOptions): {
55
56
  name: string;
56
57
  packageLinkPhase({ customElementsManifest }: any): void;
57
58
  };
59
+ /** Plugin for @wc-toolkit/cem-generator that generates Vue.js types from the finalized CEM. */
60
+ declare function vuejsTypesGeneratorPlugin(options?: VuejsTypesOptions): Plugin;
58
61
 
59
62
  /**
60
63
  * @license
@@ -811,4 +814,4 @@ interface Demo {
811
814
  */
812
815
  declare function generateVuejsTypes(manifest: Package, options?: VuejsTypesOptions): string | undefined;
813
816
 
814
- export { generateVuejsTypes, vuejsTypesPlugin };
817
+ export { generateVuejsTypes, vuejsTypesGeneratorPlugin, vuejsTypesPlugin };
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 });
@@ -631,7 +709,16 @@ function vuejsTypesPlugin(options = {}) {
631
709
  }
632
710
  };
633
711
  }
712
+ function vuejsTypesGeneratorPlugin(options = {}) {
713
+ return {
714
+ name: "@wc-toolkit/vuejs-types:cem-generator",
715
+ afterGenerate(manifest2) {
716
+ generateVuejsTypes(manifest2, options);
717
+ }
718
+ };
719
+ }
634
720
  export {
635
721
  generateVuejsTypes,
722
+ vuejsTypesGeneratorPlugin,
636
723
  vuejsTypesPlugin
637
724
  };
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.2",
4
4
  "description": "This package generates Vue.js types for custom elements / web components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -50,6 +50,7 @@
50
50
  "vitest": "^3.0.4"
51
51
  },
52
52
  "dependencies": {
53
+ "@wc-toolkit/cem-generator": "^0.1.1",
53
54
  "@prettier/sync": "^0.6.1"
54
55
  },
55
56
  "keywords": [