@sprlab/wccompiler 0.10.2 → 0.10.4

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/adapters/react.js CHANGED
@@ -105,11 +105,33 @@ export function useWccModel(propName, value, setValue, existingRef) {
105
105
  }
106
106
 
107
107
 
108
+ /**
109
+ * Converts a kebab-case event name to a React-idiomatic prop name.
110
+ *
111
+ * Rules:
112
+ * - 'change' → 'onChange'
113
+ * - 'count-changed' → 'onCountChange' (strips trailing 'd' from past tense)
114
+ * - 'value-updated' → 'onValueUpdate' (strips trailing 'd')
115
+ * - 'reset' → 'onReset'
116
+ * - 'item-click' → 'onItemClick'
117
+ *
118
+ * @param {string} eventName - kebab-case event name
119
+ * @returns {string} React prop name (onCamelCase)
120
+ */
121
+ function toReactEventProp(eventName) {
122
+ const parts = eventName.split('-')
123
+ const camel = parts.map(s => s[0].toUpperCase() + s.slice(1)).join('')
124
+ // Strip trailing 'd' from past tense verbs (changed→Change, updated→Update)
125
+ const normalized = camel.replace(/(Changed|Updated|Removed|Added|Closed|Opened|Submitted|Cancelled)$/, (m) => m.slice(0, -1))
126
+ return 'on' + normalized
127
+ }
128
+
108
129
  /**
109
130
  * Creates a React wrapper component for a WCC custom element.
110
131
  *
111
132
  * The wrapper provides idiomatic React DX:
112
- * - Event props: `onChange`, `onCountChanged` → automatically wired via addEventListener
133
+ * - Event props: `onChange`, `onCountChange` → automatically wired via addEventListener
134
+ * Handlers receive the unwrapped value (event.detail), not the raw CustomEvent.
113
135
  * - Model props: two-way binding via attribute + event listener
114
136
  * - Regular props: passed as attributes on the custom element
115
137
  * - Children: passed through as-is (use `<div slot="name">` for named slots)
@@ -118,9 +140,9 @@ export function useWccModel(propName, value, setValue, existingRef) {
118
140
  * @param {string} tagName - The custom element tag name (e.g., 'wcc-card')
119
141
  * @param {Object} [config] - Configuration for the wrapper
120
142
  * @param {string[]} [config.events] - Custom event names to expose as onEventName props
121
- * Event names are converted: 'count-changed' → onCountChanged prop
143
+ * Event names are converted: 'count-changed' → onCountChange prop (React convention)
122
144
  * @param {string[]} [config.models] - Model prop names for two-way binding
123
- * Each model 'name' creates: `name` prop (sets attribute) + `onNameChanged` event
145
+ * Each model 'name' creates: `name` prop (sets attribute) + `onNameChange` event
124
146
  * @returns {import('react').ForwardRefExoticComponent} A React component
125
147
  *
126
148
  * @example
@@ -134,8 +156,8 @@ export function useWccModel(propName, value, setValue, existingRef) {
134
156
  * return (
135
157
  * <WccCounter
136
158
  * count={count}
137
- * onCountChanged={(e) => setCount(e.detail)}
138
- * onChange={(e) => console.log('changed', e.detail)}
159
+ * onCountChange={(value) => setCount(value)}
160
+ * onChange={(value) => console.log('changed', value)}
139
161
  * label="Clicks"
140
162
  * >
141
163
  * <div slot="footer">Footer content</div>
@@ -147,19 +169,21 @@ export function createWccWrapper(tagName, config = {}) {
147
169
  const { events = [], models = [] } = config
148
170
 
149
171
  // Build a set of event prop names for quick lookup
150
- // 'count-changed' → 'onCountChanged'
172
+ // Convention: kebab-case event React onCamelCase (without trailing 'd' from 'changed')
173
+ // 'count-changed' → 'onCountChange' (not 'onCountChanged')
151
174
  // 'change' → 'onChange'
175
+ // 'value-updated' → 'onValueUpdate' (strips trailing 'd' from past tense)
152
176
  const eventPropMap = new Map()
153
177
  for (const eventName of events) {
154
- const propName = 'on' + eventName.split('-').map(s => s[0].toUpperCase() + s.slice(1)).join('')
178
+ const propName = toReactEventProp(eventName)
155
179
  eventPropMap.set(propName, eventName)
156
180
  }
157
181
 
158
- // Model events: 'count' → 'count-changed' → 'onCountChanged'
182
+ // Model events: 'count' → 'count-changed' → 'onCountChange'
159
183
  const modelEventMap = new Map()
160
184
  for (const modelName of models) {
161
185
  const eventName = `${modelName}-changed`
162
- const propName = 'on' + eventName.split('-').map(s => s[0].toUpperCase() + s.slice(1)).join('')
186
+ const propName = toReactEventProp(eventName)
163
187
  eventPropMap.set(propName, eventName)
164
188
  modelEventMap.set(modelName, eventName)
165
189
  }
@@ -207,7 +231,7 @@ export function createWccWrapper(tagName, config = {}) {
207
231
  for (const eventName of allEvents) {
208
232
  const listener = (e) => {
209
233
  const handler = handlersRef.current[eventName]
210
- if (handler) handler(e)
234
+ if (handler) handler(e instanceof CustomEvent ? e.detail : e)
211
235
  }
212
236
  el.addEventListener(eventName, listener)
213
237
  listeners.push([eventName, listener])
@@ -268,7 +292,8 @@ export function createWccWrapper(tagName, config = {}) {
268
292
  * const WccCounter = wrapWccComponent(customElements.get('wcc-counter'))
269
293
  *
270
294
  * // Use idiomatically — no manual config needed
271
- * <WccCounter count={count} onCountChanged={setCount} onChange={handler} />
295
+ * // Handlers receive the value directly (not the event)
296
+ * <WccCounter count={count} onCountChange={setCount} onChange={(val) => console.log(val)} />
272
297
  */
273
298
  export function wrapWccComponent(WccClass) {
274
299
  const meta = WccClass?.__meta
@@ -745,3 +745,120 @@ export function wccReactPlugin(options = {}) {
745
745
  }
746
746
  }
747
747
  }
748
+
749
+
750
+ /**
751
+ * Vite plugin that generates a virtual module `@wcc/react` (or custom id)
752
+ * exporting pre-built React wrapper components for all WCC components found
753
+ * in the project's compiled output directory.
754
+ *
755
+ * This enables the ideal import experience:
756
+ * import { WccCounter, WccCard } from '@wcc/react'
757
+ *
758
+ * The plugin scans the output directory for compiled .js files, reads their
759
+ * `static __meta` declarations, and generates wrapper code using createWccWrapper.
760
+ *
761
+ * @param {Object} [options]
762
+ * @param {string} [options.moduleId='@wcc/react'] - Virtual module ID for imports
763
+ * @param {string} [options.componentsDir='./dist'] - Directory containing compiled WCC .js files
764
+ * @param {string} [options.prefix='wcc-'] - Tag prefix filter
765
+ * @returns {import('vite').Plugin}
766
+ *
767
+ * @example vite.config.js
768
+ * ```js
769
+ * import { wccReactPlugin, wccReactComponents } from '@sprlab/wccompiler/integrations/react'
770
+ * export default {
771
+ * plugins: [
772
+ * wccReactPlugin(),
773
+ * wccReactComponents({ componentsDir: './src/wcc' })
774
+ * ]
775
+ * }
776
+ * ```
777
+ *
778
+ * @example Component.jsx
779
+ * ```jsx
780
+ * import { WccCounter, WccCard } from '@wcc/react'
781
+ *
782
+ * <WccCounter count={count} onCountChange={setCount} />
783
+ * <WccCard><div slot="header">Title</div></WccCard>
784
+ * ```
785
+ */
786
+ export function wccReactComponents(options = {}) {
787
+ const {
788
+ moduleId = '@wcc/react',
789
+ componentsDir = './dist',
790
+ prefix = 'wcc-'
791
+ } = options
792
+
793
+ const resolvedId = '\0' + moduleId
794
+
795
+ return {
796
+ name: 'vite-plugin-wcc-react-components',
797
+ resolveId(id) {
798
+ if (id === moduleId) return resolvedId
799
+ return null
800
+ },
801
+ async load(id) {
802
+ if (id !== resolvedId) return null
803
+
804
+ // Scan componentsDir for .js files and extract __meta
805
+ const fs = await import('fs')
806
+ const path = await import('path')
807
+
808
+ const dir = path.default.resolve(componentsDir)
809
+ if (!fs.default.existsSync(dir)) {
810
+ this.warn(`[wcc-react-components] Directory not found: ${dir}`)
811
+ return 'export {}'
812
+ }
813
+
814
+ const files = fs.default.readdirSync(dir).filter(f => f.endsWith('.js'))
815
+ const components = []
816
+
817
+ for (const file of files) {
818
+ const content = fs.default.readFileSync(path.default.join(dir, file), 'utf-8')
819
+ const metaMatch = content.match(/static __meta\s*=\s*(\{[^}]+\})/)
820
+ if (!metaMatch) continue
821
+
822
+ try {
823
+ // Parse the meta object (it's a JS object literal, evaluate safely)
824
+ const metaStr = metaMatch[1]
825
+ .replace(/'/g, '"')
826
+ .replace(/(\w+):/g, '"$1":')
827
+ .replace(/,\s*}/g, '}')
828
+ .replace(/,\s*]/g, ']')
829
+ const meta = JSON.parse(metaStr)
830
+
831
+ if (!meta.tag || !meta.tag.startsWith(prefix)) continue
832
+
833
+ const pascalName = meta.tag.split('-').map(s => s[0].toUpperCase() + s.slice(1)).join('')
834
+ components.push({ meta, pascalName, file })
835
+ } catch (e) {
836
+ // Skip files with unparseable meta
837
+ }
838
+ }
839
+
840
+ if (components.length === 0) {
841
+ return 'export {}'
842
+ }
843
+
844
+ // Generate the virtual module code
845
+ let code = `import { createWccWrapper } from '@sprlab/wccompiler/adapters/react';\n`
846
+
847
+ // Import each component file to ensure registration
848
+ for (const comp of components) {
849
+ code += `import '${path.default.resolve(dir, comp.file)}';\n`
850
+ }
851
+
852
+ code += '\n'
853
+
854
+ // Generate wrapper exports
855
+ for (const comp of components) {
856
+ const eventsArr = JSON.stringify(comp.meta.events || [])
857
+ const modelsArr = JSON.stringify(comp.meta.models || [])
858
+ code += `export const ${comp.pascalName} = createWccWrapper('${comp.meta.tag}', { events: ${eventsArr}, models: ${modelsArr} });\n`
859
+ }
860
+
861
+ return code
862
+ }
863
+ }
864
+ }
@@ -1,37 +1,39 @@
1
1
  /**
2
2
  * Vue Vite plugin for WCC custom elements.
3
- * Configures isCustomElement and enables v-model:propName on custom elements.
3
+ * Configures isCustomElement and provides enhanced DX for v-model modifiers and scoped slots.
4
4
  *
5
5
  * @module @sprlab/wccompiler/integrations/vue
6
6
  *
7
- * IMPORTANT: This file is for vite.config.js (Node.js context).
8
- * For browser-side, use app.use(wccVue) from '@sprlab/wccompiler/adapters/vue'.
7
+ * IMPORTANT: This plugin is OPTIONAL for basic usage.
8
+ * WCC components work in Vue with zero WCC-specific config:
9
+ * - Props: <wcc-counter :count="val"></wcc-counter>
10
+ * - Events: <wcc-counter @count-changed="handler($event.detail)"></wcc-counter>
11
+ * - v-model: <wcc-counter v-model:count="val"></wcc-counter> (Vue 3.4+ CE support)
12
+ * - Named slots: <div slot="header">...</div>
9
13
  *
10
- * @example vite.config.js
14
+ * The only Vue-specific config needed (same as Lit, Shoelace, FAST):
15
+ * vue({ template: { compilerOptions: { isCustomElement: tag => tag.includes('-') } } })
16
+ *
17
+ * This plugin adds:
18
+ * 1. isCustomElement config (so you don't need to write it manually)
19
+ * 2. v-model modifier support (.trim, .number, .lazy)
20
+ * 3. Scoped slot syntax: <template #item="{ name }">{{name}}</template>
21
+ *
22
+ * @example vite.config.js (with plugin — full DX)
11
23
  * ```js
12
24
  * import { wccVuePlugin } from '@sprlab/wccompiler/integrations/vue'
13
25
  * export default { plugins: [wccVuePlugin()] }
14
26
  * ```
15
27
  *
16
- * @example main.js (optionalonly needed if NOT using wccVuePlugin)
28
+ * @example vite.config.js (without plugin still works for basic usage)
17
29
  * ```js
18
- * import { wccVue } from '@sprlab/wccompiler/adapters/vue'
19
- * app.use(wccVue)
20
- * ```
21
- *
22
- * With wccVuePlugin(), v-model:propName works natively on WCC custom elements:
23
- * ```vue
24
- * <wcc-input v-model="text"></wcc-input>
25
- * <wcc-form v-model:count="countRef" v-model:title="titleRef"></wcc-form>
30
+ * import vue from '@vitejs/plugin-vue'
31
+ * export default {
32
+ * plugins: [vue({
33
+ * template: { compilerOptions: { isCustomElement: tag => tag.includes('-') } }
34
+ * })]
35
+ * }
26
36
  * ```
27
- *
28
- * How it works:
29
- * The plugin runs BEFORE @vitejs/plugin-vue and rewrites the template string:
30
- * v-model:count="expr" → :count="expr" @count-changed="expr = $event.detail"
31
- * v-model="expr" → :model-value="expr" @model-value-changed="expr = $event.detail"
32
- *
33
- * The WCC component emits `propName-changed` CustomEvent with detail=value on internal writes.
34
- * Vue compiles @propName-changed as a normal event listener (not filtered like update:*).
35
37
  */
36
38
 
37
39
  import vue from '@vitejs/plugin-vue'
@@ -63,6 +65,13 @@ export function wccVuePlugin(options = {}) {
63
65
 
64
66
  let result = code
65
67
 
68
+ // NOTE: As of WCC 0.10.3+, v-model:propName works WITHOUT this plugin
69
+ // because the compiled component emits 'update:propName' natively (Vue 3.4+ CE support).
70
+ // This transform is still useful for:
71
+ // 1. v-model modifiers (.trim, .number) — Vue doesn't apply modifiers to CE v-model natively
72
+ // 2. Older Vue versions (< 3.4) that don't support v-model on CE via update:propName
73
+ // 3. Scoped slot syntax transformation ({{prop}} → {%prop%})
74
+
66
75
  // Transform v-model:propName="expr" on custom elements (tags with hyphens)
67
76
  // Also handles modifiers: v-model:propName.trim.number="expr"
68
77
  // → :propName="expr" @propName-changed="expr = $event.detail"
package/lib/codegen.js CHANGED
@@ -1773,9 +1773,25 @@ export function generateComponent(parseResult, options = {}) {
1773
1773
  }
1774
1774
 
1775
1775
  // _emit method (if emits declared)
1776
+ // Emits the event in multiple formats for cross-framework compatibility:
1777
+ // 1. Original name (kebab-case) — for direct addEventListener and Angular (event-name)
1778
+ // 2. camelCase — for Angular WccEvents directive
1779
+ // 3. lowercase — for React 19 (onEventName → addEventListener('eventname'))
1776
1780
  if (emits.length > 0) {
1777
1781
  lines.push(' _emit(name, detail) {');
1778
- lines.push(' this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));');
1782
+ lines.push(' const evt = { detail, bubbles: true, composed: true };');
1783
+ lines.push(' this.dispatchEvent(new CustomEvent(name, evt));');
1784
+ // camelCase version (only if name contains hyphens)
1785
+ lines.push(" if (name.includes('-')) {");
1786
+ lines.push(" const camel = name.replace(/-([a-z])/g, (_, c) => c.toUpperCase());");
1787
+ lines.push(' this.dispatchEvent(new CustomEvent(camel, evt));');
1788
+ // lowercase version for React 19
1789
+ lines.push(' this.dispatchEvent(new CustomEvent(camel.toLowerCase(), evt));');
1790
+ lines.push(' } else {');
1791
+ // If no hyphens, just emit lowercase (for React 19)
1792
+ lines.push(' const lower = name.toLowerCase();');
1793
+ lines.push(' if (lower !== name) this.dispatchEvent(new CustomEvent(lower, evt));');
1794
+ lines.push(' }');
1779
1795
  lines.push(' }');
1780
1796
  lines.push('');
1781
1797
  }
@@ -1783,10 +1799,14 @@ export function generateComponent(parseResult, options = {}) {
1783
1799
  // _modelSet methods (one per defineModel prop — emits events on internal write)
1784
1800
  // Emits:
1785
1801
  // 1. wcc:model — generic event for vanilla JS and WCC-to-WCC binding
1786
- // 2. propName-changed — for Vue v-model (Vue doesn't filter this name)
1787
- // 3. propNameChange — for Angular [(prop)] banana-box syntax
1802
+ // 2. propName-changed — kebab-case for direct addEventListener
1803
+ // 3. propNameChangedcamelCase for Angular WccEvents / direct binding
1804
+ // 4. propnamechanged — lowercase for React 19 (onPropnameChanged → 'propnamechanged')
1805
+ // 5. propNameChange — for Angular [(prop)] banana-box syntax
1806
+ // 6. update:propName — for Vue v-model:propName on custom elements (Vue 3.4+)
1788
1807
  for (const md of modelDefs) {
1789
1808
  const kebabName = camelToKebab(md.name);
1809
+ const camelChanged = `${md.name}Changed`;
1790
1810
  lines.push(` _modelSet_${md.name}(newVal) {`);
1791
1811
  lines.push(` const oldVal = this._m_${md.name}();`);
1792
1812
  lines.push(` this._m_${md.name}(newVal);`);
@@ -1795,10 +1815,16 @@ export function generateComponent(parseResult, options = {}) {
1795
1815
  lines.push(` bubbles: true,`);
1796
1816
  lines.push(` composed: true`);
1797
1817
  lines.push(` }));`);
1798
- // Vue: propName-changed (not filtered by Vue's isModelListener)
1818
+ // Kebab-case: prop-name-changed
1799
1819
  lines.push(` this.dispatchEvent(new CustomEvent('${kebabName}-changed', { detail: newVal, bubbles: true }));`);
1800
- // Angular: propNameChange (Angular's [(prop)] listens for propChange)
1820
+ // camelCase: propNameChanged
1821
+ lines.push(` this.dispatchEvent(new CustomEvent('${camelChanged}', { detail: newVal, bubbles: true }));`);
1822
+ // lowercase: propnamechanged (React 19)
1823
+ lines.push(` this.dispatchEvent(new CustomEvent('${camelChanged.toLowerCase()}', { detail: newVal, bubbles: true }));`);
1824
+ // Angular banana-box: propNameChange
1801
1825
  lines.push(` this.dispatchEvent(new CustomEvent('${md.name}Change', { detail: newVal, bubbles: true }));`);
1826
+ // Vue v-model: update:propName
1827
+ lines.push(` this.dispatchEvent(new CustomEvent('update:${md.name}', { detail: newVal, bubbles: true }));`);
1802
1828
  lines.push(' }');
1803
1829
  lines.push('');
1804
1830
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sprlab/wccompiler",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "description": "Zero-runtime compiler that transforms .wcc single-file components into native web components with signals-based reactivity",
5
5
  "type": "module",
6
6
  "exports": {