@ynor/ynor 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/README.md +16 -0
- package/bin/ynor.js +519 -0
- package/package.json +93 -0
- package/src/lib/core/compiler.d.ts +294 -0
- package/src/lib/core/compiler.js +472 -0
- package/src/lib/core/index.js +43 -0
- package/src/lib/core/plugin.js +114 -0
- package/src/lib/core/runtime.js +138 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// src/lib/core/runtime.js
|
|
2
|
+
export class YnorRuntime {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.components = new Map();
|
|
5
|
+
this.stores = new Map();
|
|
6
|
+
this.mounted = new Set();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
registerComponent(name, component) {
|
|
10
|
+
this.components.set(name, component);
|
|
11
|
+
if (typeof window !== 'undefined') {
|
|
12
|
+
window[`Ynor${name}`] = component;
|
|
13
|
+
}
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
createStore(key, initialValue) {
|
|
18
|
+
if (this.stores.has(key)) {
|
|
19
|
+
return this.stores.get(key);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let value = initialValue;
|
|
23
|
+
const subscribers = new Set();
|
|
24
|
+
const store = {
|
|
25
|
+
get: () => value,
|
|
26
|
+
set: (newValue) => {
|
|
27
|
+
if (newValue !== value) {
|
|
28
|
+
value = newValue;
|
|
29
|
+
subscribers.forEach(fn => fn(value));
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
subscribe: (fn) => {
|
|
33
|
+
subscribers.add(fn);
|
|
34
|
+
fn(value);
|
|
35
|
+
return () => subscribers.delete(fn);
|
|
36
|
+
},
|
|
37
|
+
update: (fn) => {
|
|
38
|
+
const newValue = fn(value);
|
|
39
|
+
store.set(newValue);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
this.stores.set(key, store);
|
|
44
|
+
return store;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
mount(Component, target, props = {}) {
|
|
48
|
+
console.log('🔧 Mounting component:', Component.name || 'Unknown');
|
|
49
|
+
console.log('📦 Props:', props);
|
|
50
|
+
console.log('🎯 Target:', target);
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
const instance = new Component({
|
|
54
|
+
target,
|
|
55
|
+
props
|
|
56
|
+
});
|
|
57
|
+
console.log('✅ Component instance created:', instance);
|
|
58
|
+
console.log('✅ Instance has render?', typeof instance.render === 'function');
|
|
59
|
+
console.log('✅ Instance has $mount?', typeof instance.$mount === 'function');
|
|
60
|
+
|
|
61
|
+
if (instance && instance.$mount) {
|
|
62
|
+
instance.$mount(target);
|
|
63
|
+
console.log('✅ Component mounted');
|
|
64
|
+
console.log('📦 Element after mount:', instance._element);
|
|
65
|
+
if (instance._element) {
|
|
66
|
+
console.log('📦 Element innerHTML:', instance._element.innerHTML);
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
console.warn('⚠️ Component has no $mount method');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
this.mounted.add(instance);
|
|
73
|
+
return instance;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('❌ Error mounting component:', error);
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
destroy(instance) {
|
|
81
|
+
if (instance && instance.$destroy) {
|
|
82
|
+
instance.$destroy();
|
|
83
|
+
this.mounted.delete(instance);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const runtime = new YnorRuntime();
|
|
89
|
+
export const store = (key, initial) => runtime.createStore(key, initial);
|
|
90
|
+
export const mount = (Component, target, props) => runtime.mount(Component, target, props);
|
|
91
|
+
|
|
92
|
+
export class YnorComponent {
|
|
93
|
+
constructor(options = {}) {
|
|
94
|
+
this.target = options.target || document.body;
|
|
95
|
+
this.props = options.props || {};
|
|
96
|
+
this._destroyed = false;
|
|
97
|
+
this._element = null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
render() {
|
|
101
|
+
throw new Error('Component must implement render()');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
onMount() {}
|
|
105
|
+
onDestroy() {}
|
|
106
|
+
|
|
107
|
+
_update() {
|
|
108
|
+
if (this._destroyed) return;
|
|
109
|
+
const newElement = this.render();
|
|
110
|
+
if (this._element && this._element.parentNode) {
|
|
111
|
+
this._element.parentNode.replaceChild(newElement, this._element);
|
|
112
|
+
} else if (this.target) {
|
|
113
|
+
this.target.appendChild(newElement);
|
|
114
|
+
}
|
|
115
|
+
this._element = newElement;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
$mount(target = this.target) {
|
|
119
|
+
if (this._destroyed) return;
|
|
120
|
+
this.target = target;
|
|
121
|
+
const element = this.render();
|
|
122
|
+
if (element && this.target) {
|
|
123
|
+
this.target.appendChild(element);
|
|
124
|
+
}
|
|
125
|
+
this._element = element;
|
|
126
|
+
this.onMount();
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
$destroy() {
|
|
131
|
+
if (this._destroyed) return;
|
|
132
|
+
this._destroyed = true;
|
|
133
|
+
this.onDestroy();
|
|
134
|
+
if (this._element && this._element.parentNode) {
|
|
135
|
+
this._element.parentNode.removeChild(this._element);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|