@lytjs/core 5.0.0 → 6.0.0

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.
Files changed (44) hide show
  1. package/README.md +328 -288
  2. package/dist/index.cjs +3482 -1
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/index.d.cts +1473 -0
  5. package/dist/index.d.ts +1473 -0
  6. package/dist/index.mjs +3342 -1
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +43 -57
  9. package/dist/error-entry.cjs +0 -1
  10. package/dist/error-entry.mjs +0 -1
  11. package/dist/plugin-entry.cjs +0 -1
  12. package/dist/plugin-entry.mjs +0 -1
  13. package/dist/shared-entry.cjs +0 -1
  14. package/dist/shared-entry.mjs +0 -1
  15. package/dist/types/create-app.d.ts +0 -161
  16. package/dist/types/create-app.d.ts.map +0 -1
  17. package/dist/types/dev-error.d.ts +0 -32
  18. package/dist/types/dev-error.d.ts.map +0 -1
  19. package/dist/types/error-codes.d.ts +0 -8
  20. package/dist/types/error-codes.d.ts.map +0 -1
  21. package/dist/types/error-entry.d.ts +0 -14
  22. package/dist/types/error-entry.d.ts.map +0 -1
  23. package/dist/types/error-handling.d.ts +0 -264
  24. package/dist/types/error-handling.d.ts.map +0 -1
  25. package/dist/types/h.d.ts +0 -96
  26. package/dist/types/h.d.ts.map +0 -1
  27. package/dist/types/index.d.ts +0 -13
  28. package/dist/types/index.d.ts.map +0 -1
  29. package/dist/types/lyt-error.d.ts +0 -8
  30. package/dist/types/lyt-error.d.ts.map +0 -1
  31. package/dist/types/plugin-entry.d.ts +0 -9
  32. package/dist/types/plugin-entry.d.ts.map +0 -1
  33. package/dist/types/plugin.d.ts +0 -119
  34. package/dist/types/plugin.d.ts.map +0 -1
  35. package/dist/types/shared-entry.d.ts +0 -8
  36. package/dist/types/shared-entry.d.ts.map +0 -1
  37. package/dist/types/warn.d.ts +0 -8
  38. package/dist/types/warn.d.ts.map +0 -1
  39. package/dist/types/web-component-entry.d.ts +0 -9
  40. package/dist/types/web-component-entry.d.ts.map +0 -1
  41. package/dist/types/web-component.d.ts +0 -127
  42. package/dist/types/web-component.d.ts.map +0 -1
  43. package/dist/web-component-entry.cjs +0 -6
  44. package/dist/web-component-entry.mjs +0 -6
package/README.md CHANGED
@@ -1,288 +1,328 @@
1
- # @lytjs/core
2
-
3
- Lyt.js 核心入口 - 整合响应式、编译器、虚拟 DOM、渲染器和组件系统,提供完整的应用开发能力。
4
-
5
- ## 安装
6
-
7
- ```bash
8
- npm install @lytjs/core
9
-
10
- # 或使用 pnpm
11
- pnpm add @lytjs/core
12
- ```
13
-
14
- ## 特性
15
-
16
- - 🚀 零运行时依赖,极致轻量
17
- - 🎨 所见即代码,无 `v-` 前缀的模板语法
18
- - 🔄 完整的 Composition API
19
- - 📦 内置组件系统、插槽、KeepAlive、Suspense
20
- - 🌐 Web Component 支持
21
- - 🔌 插件系统
22
-
23
- ## 快速开始
24
-
25
- ### 创建应用
26
-
27
- ```javascript
28
- import { createApp, defineComponent, h } from '@lytjs/core';
29
-
30
- const App = defineComponent({
31
- setup() {
32
- return { count: 0 };
33
- },
34
- render() {
35
- return h('div', [
36
- h('h1', `Count: ${this.count}`),
37
- h('button', {
38
- onClick: () => this.count++
39
- }, 'Increment')
40
- ]);
41
- }
42
- });
43
-
44
- const app = createApp(App);
45
- app.mount('#app');
46
- ```
47
-
48
- ### 使用模板
49
-
50
- ```javascript
51
- import { createApp, defineComponent } from '@lytjs/core';
52
-
53
- const App = defineComponent({
54
- setup() {
55
- return { count: 0 };
56
- },
57
- template: `
58
- <div>
59
- <h1>Count: {{ count }}</h1>
60
- <button @click="count++">Increment</button>
61
- </div>
62
- `
63
- });
64
-
65
- const app = createApp(App);
66
- app.mount('#app');
67
- ```
68
-
69
- ### 使用插件
70
-
71
- ```javascript
72
- import { createApp, defineComponent } from '@lytjs/core';
73
-
74
- const myPlugin = {
75
- install(app, options) {
76
- app.config.globalProperties.$hello = 'Hello from plugin!';
77
- }
78
- };
79
-
80
- const app = createApp(App);
81
- app.use(myPlugin, { someOption: true });
82
- app.mount('#app');
83
- ```
84
-
85
- ### Web Component
86
-
87
- ```javascript
88
- import { defineCustomElement, defineComponent } from '@lytjs/core';
89
-
90
- const MyComponent = defineComponent({
91
- props: ['name'],
92
- template: `<div>Hello, {{ name }}!</div>`
93
- });
94
-
95
- customElements.define('my-component', defineCustomElement(MyComponent));
96
- ```
97
-
98
- ## API 参考
99
-
100
- ### 应用 API
101
-
102
- | API | 说明 |
103
- |------|------|
104
- | `createApp(rootComponent, rootProps)` | 创建应用实例 |
105
- | `createSSRApp(rootComponent, rootProps)` | 创建 SSR 应用实例 |
106
-
107
- ### 应用实例
108
-
109
- | API | 说明 |
110
- |------|------|
111
- | `app.mount(container)` | 挂载应用到 DOM 容器 |
112
- | `app.unmount()` | 卸载应用 |
113
- | `app.use(plugin, options)` | 安装插件 |
114
- | `app.component(name, component)` | 注册全局组件 |
115
- | `app.directive(name, directive)` | 注册全局指令 |
116
- | `app.provide(key, value)` | 全局提供值 |
117
- | `app.config` | 应用配置对象 |
118
-
119
- ### 组件定义
120
-
121
- | API | 说明 |
122
- |------|------|
123
- | `defineComponent(options)` | 定义组件 |
124
- | `defineAsyncComponent(loader)` | 定义异步组件 |
125
- | `resolveComponent(name)` | 解析组件 |
126
-
127
- ### 渲染 API
128
-
129
- | API | 说明 |
130
- |------|------|
131
- | `h(type, props, children)` | 创建虚拟节点 |
132
- | `render(vnode, container)` | 渲染虚拟节点 |
133
-
134
- ### 依赖注入
135
-
136
- | API | 说明 |
137
- |------|------|
138
- | `provide(key, value)` | 提供值给后代组件 |
139
- | `inject(key, defaultValue)` | 注入祖先组件提供的值 |
140
-
141
- ### 内置组件
142
-
143
- | 组件 | 说明 |
144
- |------|------|
145
- | `KeepAlive` | 缓存失活组件实例 |
146
- | `Teleport` | 将内容渲染到指定 DOM 位置 |
147
- | `Transition` | 单个元素/组件的过渡动画 |
148
- | `TransitionGroup` | 列表的过渡动画 |
149
- | `Suspense` | 等待异步依赖 |
150
-
151
- ## 模板语法
152
-
153
- Lyt.js 的模板语法与原生 HTML 更接近,去掉了 `v-` 前缀:
154
-
155
- ### 插值
156
-
157
- ```html
158
- <span>Text: {{ message }}</span>
159
- ```
160
-
161
- ### 指令
162
-
163
- ```html
164
- <!-- 条件渲染 -->
165
- <div if="visible">显示</div>
166
- <div else-if="pending">加载中...</div>
167
- <div else>隐藏</div>
168
-
169
- <!-- 列表渲染 -->
170
- <ul>
171
- <li each="(item, index) in items" :key="item.id">
172
- {{ index }}: {{ item.text }}
173
- </li>
174
- </ul>
175
-
176
- <!-- 事件绑定 -->
177
- <button @click="handleClick">点击</button>
178
- <input @input="handleInput" />
179
-
180
- <!-- 属性绑定 -->
181
- <img :src="imageUrl" :alt="altText" />
182
- <div :class="{ active: isActive }"></div>
183
- ```
184
-
185
- ## 组件生命周期
186
-
187
- ```javascript
188
- import { defineComponent, onMounted, onUpdated, onUnmounted } from '@lytjs/core';
189
-
190
- const MyComponent = defineComponent({
191
- setup() {
192
- onMounted(() => {
193
- console.log('组件已挂载');
194
- });
195
-
196
- onUpdated(() => {
197
- console.log('组件已更新');
198
- });
199
-
200
- onUnmounted(() => {
201
- console.log('组件已卸载');
202
- });
203
-
204
- return {};
205
- }
206
- });
207
- ```
208
-
209
- | 生命周期 | 说明 |
210
- |----------|------|
211
- | `onBeforeMount` | 组件挂载前 |
212
- | `onMounted` | 组件挂载后 |
213
- | `onBeforeUpdate` | 组件更新前 |
214
- | `onUpdated` | 组件更新后 |
215
- | `onBeforeUnmount` | 组件卸载前 |
216
- | `onUnmounted` | 组件卸载后 |
217
-
218
- ## 示例
219
-
220
- ### Todo List
221
-
222
- ```javascript
223
- import { createApp, defineComponent, ref } from '@lytjs/core';
224
-
225
- const App = defineComponent({
226
- setup() {
227
- const todos = ref([{ id: 1, text: 'Learn Lyt.js', done: false }]);
228
- const newTodo = ref('');
229
-
230
- function addTodo() {
231
- if (newTodo.value.trim()) {
232
- todos.value.push({
233
- id: Date.now(),
234
- text: newTodo.value,
235
- done: false
236
- });
237
- newTodo.value = '';
238
- }
239
- }
240
-
241
- function toggleTodo(todo) {
242
- todo.done = !todo.done;
243
- }
244
-
245
- return { todos, newTodo, addTodo, toggleTodo };
246
- },
247
- template: `
248
- <div>
249
- <h1>Todo List</h1>
250
- <div>
251
- <input v-model="newTodo" @keyup.enter="addTodo" />
252
- <button @click="addTodo">Add</button>
253
- </div>
254
- <ul>
255
- <li
256
- each="todo in todos"
257
- :key="todo.id"
258
- :class="{ done: todo.done }"
259
- @click="toggleTodo(todo)"
260
- >
261
- {{ todo.text }}
262
- </li>
263
- </ul>
264
- </div>
265
- `
266
- });
267
-
268
- createApp(App).mount('#app');
269
- ```
270
-
271
- ## 性能
272
-
273
- - 核心体积:2.13 KB (ESM gzip)
274
- - 零运行时依赖
275
- - Block Tree 虚拟 DOM 优化
276
- - Patch Flag 差异化更新
277
-
278
- ## 兼容性
279
-
280
- - Node.js >= 18.0.0
281
- - Chrome 64+
282
- - Firefox 63+
283
- - Safari 12+
284
- - Edge 79+
285
-
286
- ## License
287
-
288
- MIT
1
+ # @lytjs/core
2
+
3
+ > LytJS 框架核心入口,整合响应式、编译器、虚拟 DOM、渲染器和组件系统
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @lytjs/core
9
+ ```
10
+
11
+ ## 核心 API
12
+
13
+ ### createApp
14
+
15
+ 创建应用实例,挂载根组件
16
+
17
+ ```typescript
18
+ import { createApp, defineComponent } from '@lytjs/core';
19
+
20
+ const RootComponent = defineComponent({
21
+ setup() {
22
+ const count = ref(0);
23
+ return () => h('div', count.value);
24
+ },
25
+ });
26
+
27
+ const app = createApp(RootComponent);
28
+ app.mount('#app');
29
+ ```
30
+
31
+ ### h / createElement
32
+
33
+ 创建虚拟节点(JSX 工厂函数)
34
+
35
+ ```typescript
36
+ import { h } from '@lytjs/core';
37
+
38
+ // 创建元素
39
+ const div = h('div', { class: 'container' }, 'Hello');
40
+
41
+ // 创建组件
42
+ const component = h(MyComponent, { prop: 'value' });
43
+
44
+ // 嵌套子节点
45
+ const nested = h('div', null, [h('span', null, 'Child 1'), h('span', null, 'Child 2')]);
46
+ ```
47
+
48
+ ### defineComponent / defineAsyncComponent
49
+
50
+ 定义组件和异步组件
51
+
52
+ ```typescript
53
+ import { defineComponent, defineAsyncComponent } from '@lytjs/core';
54
+
55
+ // 同步组件
56
+ const MyComponent = defineComponent({
57
+ name: 'MyComponent',
58
+ props: {
59
+ title: String,
60
+ },
61
+ setup(props) {
62
+ return () => h('div', props.title);
63
+ },
64
+ });
65
+
66
+ // 异步组件
67
+ const AsyncComponent = defineAsyncComponent(() => import('./HeavyComponent.vue'));
68
+ ```
69
+
70
+ ### nextTick
71
+
72
+ 在下一个 DOM 更新周期后执行回调
73
+
74
+ ```typescript
75
+ import { nextTick, ref } from '@lytjs/core';
76
+
77
+ const count = ref(0);
78
+ count.value++;
79
+
80
+ // 等待 DOM 更新
81
+ await nextTick();
82
+ // 或使用回调
83
+ nextTick(() => {
84
+ console.log('DOM updated');
85
+ });
86
+ ```
87
+
88
+ ## 响应式 API
89
+
90
+ @lytjs/reactivity 重导出
91
+
92
+ ```typescript
93
+ import {
94
+ ref,
95
+ reactive,
96
+ shallowRef,
97
+ shallowReactive,
98
+ readonly,
99
+ shallowReadonly,
100
+ computed,
101
+ watch,
102
+ watchEffect,
103
+ watchPostEffect,
104
+ watchSyncEffect,
105
+ effect,
106
+ stop,
107
+ toRef,
108
+ toRefs,
109
+ unref,
110
+ toValue,
111
+ customRef,
112
+ isRef,
113
+ isReactive,
114
+ isReadonly,
115
+ isProxy,
116
+ toRaw,
117
+ markRaw,
118
+ triggerRef,
119
+ provide,
120
+ inject,
121
+ // Signal API
122
+ signal,
123
+ computed as signalComputed,
124
+ writableComputedSignal,
125
+ readonlySignal,
126
+ set,
127
+ update,
128
+ valueOf,
129
+ signalBatch,
130
+ signalUntrack,
131
+ // 类型守卫
132
+ isShallowRef,
133
+ isComputedRef,
134
+ } from '@lytjs/core';
135
+ ```
136
+
137
+ ## 生命周期钩子
138
+
139
+ ```typescript
140
+ import {
141
+ onMounted,
142
+ onUnmounted,
143
+ onUpdated,
144
+ onBeforeMount,
145
+ onBeforeUpdate,
146
+ onBeforeUnmount,
147
+ onErrorCaptured,
148
+ onActivated,
149
+ onDeactivated,
150
+ onRenderTracked,
151
+ onRenderTriggered,
152
+ } from '@lytjs/core';
153
+ ```
154
+
155
+ ## 编译 API
156
+
157
+ 从 @lytjs/compiler 重导出
158
+
159
+ ```typescript
160
+ import { compile, parse, transform, generate } from '@lytjs/core';
161
+
162
+ const { code, ast } = compile('<div>{{ message }}</div>');
163
+ ```
164
+
165
+ ## 内置组件
166
+
167
+ ```typescript
168
+ import {
169
+ KeepAlive,
170
+ Suspense,
171
+ Transition,
172
+ TransitionGroup,
173
+ Teleport,
174
+ ErrorBoundary,
175
+ } from '@lytjs/core';
176
+ ```
177
+
178
+ ### KeepAlive
179
+
180
+ 缓存非活动组件实例
181
+
182
+ ```typescript
183
+ import { KeepAlive, ref } from '@lytjs/core';
184
+
185
+ const current = ref('ComponentA');
186
+
187
+ <KeepAlive include="ComponentA,ComponentB" :max="10">
188
+ <component :is="current" />
189
+ </KeepAlive>
190
+ ```
191
+
192
+ ### Suspense
193
+
194
+ 处理异步组件加载
195
+
196
+ ```typescript
197
+ import { Suspense, defineAsyncComponent } from '@lytjs/core';
198
+
199
+ const AsyncComponent = defineAsyncComponent(() => import('./Heavy.vue'));
200
+
201
+ <Suspense>
202
+ <template #default>
203
+ <AsyncComponent />
204
+ </template>
205
+ <template #fallback>
206
+ <div>Loading...</div>
207
+ </template>
208
+ </Suspense>
209
+ ```
210
+
211
+ ### Transition / TransitionGroup
212
+
213
+ 过渡动画
214
+
215
+ ```typescript
216
+ import { Transition, TransitionGroup } from '@lytjs/core';
217
+
218
+ <Transition name="fade" mode="out-in">
219
+ <div :key="id">Content</div>
220
+ </Transition>
221
+
222
+ <TransitionGroup name="list" tag="ul">
223
+ <li v-for="item in items" :key="item.id">{{ item.name }}</li>
224
+ </TransitionGroup>
225
+ ```
226
+
227
+ ### Teleport
228
+
229
+ 传送内容到指定位置
230
+
231
+ ```typescript
232
+ import { Teleport } from '@lytjs/core';
233
+
234
+ <Teleport to="body">
235
+ <div class="modal">Modal content</div>
236
+ </Teleport>
237
+ ```
238
+
239
+ ### ErrorBoundary
240
+
241
+ 错误边界,捕获子组件错误
242
+
243
+ ```typescript
244
+ import { ErrorBoundary } from '@lytjs/core';
245
+
246
+ <ErrorBoundary :on-error="handleError" :capture-promise-rejections="true">
247
+ <MyComponent />
248
+ <template #fallback="{ error }">
249
+ <div>Error: {{ error?.message }}</div>
250
+ </template>
251
+ </ErrorBoundary>
252
+ ```
253
+
254
+ ## 渲染 API
255
+
256
+ ```typescript
257
+ import {
258
+ createRenderer,
259
+ createDOMRenderer,
260
+ renderToString,
261
+ renderToStream,
262
+ createSignalRenderer,
263
+ createVaporRenderer,
264
+ createVaporApp,
265
+ defineVaporComponent,
266
+ } from '@lytjs/core';
267
+ ```
268
+
269
+ ## 工具函数
270
+
271
+ ```typescript
272
+ import {
273
+ mergeProps,
274
+ cloneVNode,
275
+ normalizeClass,
276
+ normalizeStyle,
277
+ normalizeProps,
278
+ } from '@lytjs/core';
279
+ ```
280
+
281
+ ## 类型定义
282
+
283
+ ```typescript
284
+ import type {
285
+ // 组件类型
286
+ ComponentOptions,
287
+ ComponentPublicInstance,
288
+ ComponentInternalInstance,
289
+ SetupContext,
290
+ PropOptions,
291
+ // VNode 类型
292
+ VNode,
293
+ VNodeTypes,
294
+ VNodeChildren,
295
+ // 响应式类型
296
+ Ref,
297
+ ShallowRef,
298
+ ComputedRef,
299
+ ReactiveEffectRunner,
300
+ WatchOptions,
301
+ WatchCallback,
302
+ WatchHandle,
303
+ // 编译类型
304
+ CompilerOptions,
305
+ CodegenResult,
306
+ // 渲染类型
307
+ RendererOptions,
308
+ RendererPlugin,
309
+ } from '@lytjs/core';
310
+ ```
311
+
312
+ ## 子包
313
+
314
+ @lytjs/core 整合以下子包:
315
+
316
+ | 包名 | 说明 |
317
+ | ---------------------------------- | ---------------- |
318
+ | [@lytjs/reactivity](../reactivity) | 响应式系统 |
319
+ | [@lytjs/vdom](../vdom) | 虚拟 DOM 实现 |
320
+ | [@lytjs/compiler](../compiler) | 模板编译器 |
321
+ | [@lytjs/renderer](../renderer) | DOM/SSR 渲染后端 |
322
+ | [@lytjs/component](../component) | 组件系统 |
323
+ | [@lytjs/common](../common) | 公共工具库 |
324
+
325
+ ## 相关包
326
+
327
+ - [@lytjs/adapter-web](../adapter-web) - Web 平台适配器
328
+ - [@lytjs/host-contract](../host-contract) - 渲染器宿主抽象