lego-dom 1.3.3 → 1.4.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.
- package/CHANGELOG.md +61 -0
- package/main.js +24 -3
- package/main.min.js +7 -0
- package/package.json +3 -1
- package/vite-plugin.js +0 -14
- package/.github/workflows/deploy-docs.yml +0 -56
- package/.legodom +0 -87
- package/docs/.vitepress/config.js +0 -162
- package/docs/api/config.md +0 -95
- package/docs/api/define.md +0 -58
- package/docs/api/directives.md +0 -50
- package/docs/api/globals.md +0 -29
- package/docs/api/index.md +0 -30
- package/docs/api/lifecycle.md +0 -40
- package/docs/api/route.md +0 -37
- package/docs/api/vite-plugin.md +0 -58
- package/docs/contributing/01-welcome.md +0 -38
- package/docs/contributing/02-registry.md +0 -133
- package/docs/contributing/03-batcher.md +0 -110
- package/docs/contributing/04-reactivity.md +0 -87
- package/docs/contributing/05-caching.md +0 -59
- package/docs/contributing/06-init.md +0 -136
- package/docs/contributing/07-observer.md +0 -72
- package/docs/contributing/08-snap.md +0 -140
- package/docs/contributing/09-diffing.md +0 -69
- package/docs/contributing/10-studs.md +0 -78
- package/docs/contributing/11-scanner.md +0 -117
- package/docs/contributing/12-render.md +0 -138
- package/docs/contributing/13-directives.md +0 -243
- package/docs/contributing/14-events.md +0 -57
- package/docs/contributing/15-router.md +0 -57
- package/docs/contributing/16-state.md +0 -47
- package/docs/contributing/17-legodom.md +0 -48
- package/docs/contributing/index.md +0 -24
- package/docs/examples/form.md +0 -42
- package/docs/examples/index.md +0 -104
- package/docs/examples/routing.md +0 -409
- package/docs/examples/sfc-showcase.md +0 -34
- package/docs/examples/todo-app.md +0 -383
- package/docs/guide/cdn-usage.md +0 -328
- package/docs/guide/components.md +0 -412
- package/docs/guide/directives.md +0 -539
- package/docs/guide/directory-structure.md +0 -248
- package/docs/guide/faq.md +0 -210
- package/docs/guide/getting-started.md +0 -262
- package/docs/guide/index.md +0 -88
- package/docs/guide/lifecycle.md +0 -525
- package/docs/guide/quick-start.md +0 -49
- package/docs/guide/reactivity.md +0 -415
- package/docs/guide/routing.md +0 -334
- package/docs/guide/server-side.md +0 -134
- package/docs/guide/sfc.md +0 -420
- package/docs/guide/templating.md +0 -388
- package/docs/index.md +0 -160
- package/docs/public/logo.svg +0 -17
- package/docs/router/basic-routing.md +0 -103
- package/docs/router/cold-entry.md +0 -91
- package/docs/router/history.md +0 -69
- package/docs/router/index.md +0 -73
- package/docs/router/resolver.md +0 -74
- package/docs/router/surgical-swaps.md +0 -134
- package/docs/tutorial/01-project-setup.md +0 -152
- package/docs/tutorial/02-your-first-component.md +0 -226
- package/docs/tutorial/03-adding-routes.md +0 -279
- package/docs/tutorial/04-multi-page-app.md +0 -329
- package/docs/tutorial/05-state-and-globals.md +0 -285
- package/docs/tutorial/index.md +0 -40
- package/examples/vite-app/README.md +0 -71
- package/examples/vite-app/index.html +0 -42
- package/examples/vite-app/package.json +0 -18
- package/examples/vite-app/src/app.css +0 -3
- package/examples/vite-app/src/app.js +0 -29
- package/examples/vite-app/src/components/app-navbar.lego +0 -34
- package/examples/vite-app/src/components/customers/customer-details.lego +0 -24
- package/examples/vite-app/src/components/customers/customer-orders.lego +0 -21
- package/examples/vite-app/src/components/customers/order-list.lego +0 -55
- package/examples/vite-app/src/components/greeting-card.lego +0 -41
- package/examples/vite-app/src/components/sample-component.lego +0 -75
- package/examples/vite-app/src/components/shells/customers-shell.lego +0 -21
- package/examples/vite-app/src/components/side-menu.lego +0 -46
- package/examples/vite-app/src/components/todo-list.lego +0 -239
- package/examples/vite-app/src/components/widgets/user-card.lego +0 -27
- package/examples/vite-app/vite.config.js +0 -22
- package/tests/error.test.js +0 -74
- package/tests/main.test.js +0 -103
- package/tests/memory.test.js +0 -68
- package/tests/monitoring.test.js +0 -74
- package/tests/naming.test.js +0 -74
- package/tests/parse-lego.test.js +0 -65
- package/tests/security.test.js +0 -67
- package/tests/server.test.js +0 -114
- package/tests/syntax.test.js +0 -67
package/docs/guide/sfc.md
DELETED
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
# Single File Components (.lego)
|
|
2
|
-
|
|
3
|
-
Single File Components (SFCs) let you define components in dedicated `.lego` files when using Vite as your build tool.
|
|
4
|
-
|
|
5
|
-
::: tip 🚀 New to LegoDOM?
|
|
6
|
-
Start with our **[Step-by-Step Tutorial](/tutorial/)** to build a complete multi-page app with SFCs!
|
|
7
|
-
:::
|
|
8
|
-
|
|
9
|
-
## Where Does My Config Go?
|
|
10
|
-
|
|
11
|
-
The #1 question developers ask: **"I have a `.lego` file – now where do I put my routes?"**
|
|
12
|
-
|
|
13
|
-
**Answer: Everything goes in your entry file (`app.js` or `main.js`):**
|
|
14
|
-
|
|
15
|
-
```javascript
|
|
16
|
-
// src/app.js – The control center of your app
|
|
17
|
-
|
|
18
|
-
import { Lego } from 'lego-dom';
|
|
19
|
-
import registerComponents from 'virtual:lego-components';
|
|
20
|
-
|
|
21
|
-
// 1. Register all .lego files automatically
|
|
22
|
-
registerComponents();
|
|
23
|
-
|
|
24
|
-
// 2. Define your routes
|
|
25
|
-
Lego.route('/', 'home-page'); // home-page.lego
|
|
26
|
-
Lego.route('/login', 'login-page'); // login-page.lego
|
|
27
|
-
Lego.route('/users/:id', 'user-profile'); // user-profile.lego
|
|
28
|
-
|
|
29
|
-
// 3. Initialize global state (optional)
|
|
30
|
-
Lego.globals.user = null;
|
|
31
|
-
|
|
32
|
-
// 4. Start the engine
|
|
33
|
-
await Lego.init();
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Your `index.html` just needs:
|
|
37
|
-
```html
|
|
38
|
-
<lego-router></lego-router>
|
|
39
|
-
<script type="module" src="/src/app.js"></script>
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
That's the complete pattern! 🎉
|
|
43
|
-
|
|
44
|
-
---
|
|
45
|
-
|
|
46
|
-
## Why SFCs?
|
|
47
|
-
|
|
48
|
-
When your project grows, keeping components in separate files makes your codebase more organized and maintainable.
|
|
49
|
-
|
|
50
|
-
### Benefits
|
|
51
|
-
|
|
52
|
-
✅ **Better Organization** - One file per component
|
|
53
|
-
✅ **Syntax Highlighting** - Proper editor support
|
|
54
|
-
✅ **Auto-discovery** - Vite plugin finds and registers components automatically
|
|
55
|
-
✅ **Hot Reload** - Changes reflect instantly during development
|
|
56
|
-
✅ **Familiar Format** - Similar to Vue SFCs if you've used them
|
|
57
|
-
|
|
58
|
-
## File Format
|
|
59
|
-
|
|
60
|
-
A `.lego` file contains three optional sections:
|
|
61
|
-
|
|
62
|
-
```html
|
|
63
|
-
<template>
|
|
64
|
-
<!-- Your component markup -->
|
|
65
|
-
</template>
|
|
66
|
-
|
|
67
|
-
<script>
|
|
68
|
-
// Your component logic
|
|
69
|
-
export default {
|
|
70
|
-
// reactive state and methods
|
|
71
|
-
}
|
|
72
|
-
</script>
|
|
73
|
-
|
|
74
|
-
<style>
|
|
75
|
-
/* Scoped styles */
|
|
76
|
-
</style>
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Example Component
|
|
80
|
-
|
|
81
|
-
Here's a complete example (`user-card.lego`):
|
|
82
|
-
|
|
83
|
-
```html
|
|
84
|
-
<template>
|
|
85
|
-
<img class="avatar" src="[[ avatarUrl ]]" alt="[[ name ]]">
|
|
86
|
-
<h2 class="name">[[ name ]]</h2>
|
|
87
|
-
<p class="bio">[[ bio ]]</p>
|
|
88
|
-
<p>Followers: [[ followers ]]</p>
|
|
89
|
-
<button @click="follow()">
|
|
90
|
-
[[ isFollowing ? 'Unfollow' : 'Follow' ]]
|
|
91
|
-
</button>
|
|
92
|
-
</template>
|
|
93
|
-
|
|
94
|
-
<style>
|
|
95
|
-
self {
|
|
96
|
-
display: block;
|
|
97
|
-
padding: 1.5rem;
|
|
98
|
-
border: 2px solid #ddd;
|
|
99
|
-
border-radius: 8px;
|
|
100
|
-
max-width: 300px;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
.avatar {
|
|
104
|
-
width: 80px;
|
|
105
|
-
height: 80px;
|
|
106
|
-
border-radius: 50%;
|
|
107
|
-
object-fit: cover;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
.name {
|
|
111
|
-
font-size: 1.5rem;
|
|
112
|
-
font-weight: bold;
|
|
113
|
-
margin: 0.5rem 0;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
.bio {
|
|
117
|
-
color: #666;
|
|
118
|
-
margin: 0.5rem 0;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
button {
|
|
122
|
-
background: #4CAF50;
|
|
123
|
-
color: white;
|
|
124
|
-
border: none;
|
|
125
|
-
padding: 0.5rem 1rem;
|
|
126
|
-
border-radius: 4px;
|
|
127
|
-
cursor: pointer;
|
|
128
|
-
}
|
|
129
|
-
</style>
|
|
130
|
-
|
|
131
|
-
<script>
|
|
132
|
-
export default {
|
|
133
|
-
name: 'John Doe',
|
|
134
|
-
bio: 'Web developer & coffee enthusiast',
|
|
135
|
-
avatarUrl: 'https://api.dicebear.com/7.x/avataaars/svg?seed=John',
|
|
136
|
-
followers: 1234,
|
|
137
|
-
isFollowing: false,
|
|
138
|
-
|
|
139
|
-
follow() {
|
|
140
|
-
if (this.isFollowing) {
|
|
141
|
-
this.followers--;
|
|
142
|
-
this.isFollowing = false;
|
|
143
|
-
} else {
|
|
144
|
-
this.followers++;
|
|
145
|
-
this.isFollowing = true;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
</script>
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
## Vite Plugin Setup
|
|
153
|
-
|
|
154
|
-
### Installation
|
|
155
|
-
|
|
156
|
-
```bash
|
|
157
|
-
npm install -D vite lego-dom
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### Configuration
|
|
161
|
-
|
|
162
|
-
Create or update `vite.config.js`:
|
|
163
|
-
|
|
164
|
-
```js
|
|
165
|
-
import { defineConfig } from 'vite';
|
|
166
|
-
import legoPlugin from 'lego-dom/vite-plugin';
|
|
167
|
-
|
|
168
|
-
export default defineConfig({
|
|
169
|
-
plugins: [
|
|
170
|
-
legoPlugin({
|
|
171
|
-
componentsDir: './src/components', // Where to look
|
|
172
|
-
include: ['**/*.lego'] // What to match
|
|
173
|
-
})
|
|
174
|
-
]
|
|
175
|
-
});
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### Project Structure
|
|
179
|
-
|
|
180
|
-
```
|
|
181
|
-
my-app/
|
|
182
|
-
├── src/
|
|
183
|
-
│ ├── components/
|
|
184
|
-
│ │ ├── user-card.lego
|
|
185
|
-
│ │ ├── post-list.lego
|
|
186
|
-
│ │ └── comment-item.lego
|
|
187
|
-
│ └── main.js
|
|
188
|
-
├── index.html
|
|
189
|
-
├── package.json
|
|
190
|
-
└── vite.config.js
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
### Entry Point
|
|
194
|
-
|
|
195
|
-
In your `src/main.js`:
|
|
196
|
-
|
|
197
|
-
```js
|
|
198
|
-
import { Lego } from 'lego-dom/main.js';
|
|
199
|
-
import registerComponents from 'virtual:lego-components';
|
|
200
|
-
|
|
201
|
-
// Auto-register all discovered components
|
|
202
|
-
registerComponents();
|
|
203
|
-
|
|
204
|
-
// Now all .lego components are available!
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### Use Components
|
|
208
|
-
|
|
209
|
-
In your `index.html`:
|
|
210
|
-
|
|
211
|
-
```html
|
|
212
|
-
<!DOCTYPE html>
|
|
213
|
-
<html>
|
|
214
|
-
<head>
|
|
215
|
-
<title>My Lego App</title>
|
|
216
|
-
</head>
|
|
217
|
-
<body>
|
|
218
|
-
<div id="app">
|
|
219
|
-
<user-card></user-card>
|
|
220
|
-
<post-list></post-list>
|
|
221
|
-
</div>
|
|
222
|
-
|
|
223
|
-
<script type="module" src="/src/main.js"></script>
|
|
224
|
-
</body>
|
|
225
|
-
</html>
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## Component Naming
|
|
229
|
-
|
|
230
|
-
Component names are automatically derived from filenames:
|
|
231
|
-
|
|
232
|
-
| Filename | Component Tag |
|
|
233
|
-
|----------|--------------|
|
|
234
|
-
| `user-card.lego` | `<user-card>` |
|
|
235
|
-
| `post-list.lego` | `<post-list>` |
|
|
236
|
-
| `comment-item.lego` | `<comment-item>` |
|
|
237
|
-
|
|
238
|
-
::: warning Naming Rules
|
|
239
|
-
Component names **must**:
|
|
240
|
-
- Be kebab-case (lowercase with hyphens)
|
|
241
|
-
- Contain at least one hyphen
|
|
242
|
-
- Match the pattern: `[a-z][a-z0-9]*(-[a-z0-9]+)+`
|
|
243
|
-
|
|
244
|
-
✅ Good: `user-card`, `post-list`, `nav-menu`
|
|
245
|
-
❌ Bad: `UserCard`, `postlist`, `card`
|
|
246
|
-
:::
|
|
247
|
-
|
|
248
|
-
## Section Details
|
|
249
|
-
|
|
250
|
-
### Template Section
|
|
251
|
-
|
|
252
|
-
Contains your component's HTML markup with Lego directives:
|
|
253
|
-
|
|
254
|
-
```html
|
|
255
|
-
<template>
|
|
256
|
-
<h1>[[ title ]]</h1>
|
|
257
|
-
<p b-show="showContent">[[ content ]]</p>
|
|
258
|
-
<ul>
|
|
259
|
-
<li b-for="item in items">[[ item ]]</li>
|
|
260
|
-
</ul>
|
|
261
|
-
</template>
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
### Script Section
|
|
265
|
-
|
|
266
|
-
Exports the component's reactive state and methods:
|
|
267
|
-
|
|
268
|
-
```html
|
|
269
|
-
<script>
|
|
270
|
-
export default {
|
|
271
|
-
// Reactive properties
|
|
272
|
-
title: 'Welcome',
|
|
273
|
-
count: 0,
|
|
274
|
-
items: ['apple', 'banana'],
|
|
275
|
-
|
|
276
|
-
// Methods
|
|
277
|
-
increment() {
|
|
278
|
-
this.count++;
|
|
279
|
-
},
|
|
280
|
-
|
|
281
|
-
// Lifecycle hooks
|
|
282
|
-
mounted() {
|
|
283
|
-
console.log('Component mounted!');
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
</script>
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
### Style Section
|
|
290
|
-
|
|
291
|
-
Scoped styles using Shadow DOM. Use `self` to target the component root:
|
|
292
|
-
|
|
293
|
-
```html
|
|
294
|
-
<style>
|
|
295
|
-
self {
|
|
296
|
-
display: block;
|
|
297
|
-
padding: 1rem;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
h1 {
|
|
301
|
-
color: #333;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
button {
|
|
305
|
-
background: blue;
|
|
306
|
-
color: white;
|
|
307
|
-
}
|
|
308
|
-
</style>
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
Styles are automatically scoped to your component—they won't affect other components or global styles.
|
|
312
|
-
|
|
313
|
-
## Hot Module Replacement
|
|
314
|
-
|
|
315
|
-
During development, changes to `.lego` files trigger a full page reload. Your changes appear instantly!
|
|
316
|
-
|
|
317
|
-
```bash
|
|
318
|
-
npm run dev
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
Edit your component, save, and see the result immediately.
|
|
322
|
-
|
|
323
|
-
## Passing Props
|
|
324
|
-
|
|
325
|
-
Pass data to components via the `b-data` attribute:
|
|
326
|
-
|
|
327
|
-
```html
|
|
328
|
-
<user-card b-data="{
|
|
329
|
-
name: 'Jane Smith',
|
|
330
|
-
bio: 'Designer',
|
|
331
|
-
followers: 5678
|
|
332
|
-
}"></user-card>
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
Or define defaults in the script section and override as needed.
|
|
336
|
-
|
|
337
|
-
## Best Practices
|
|
338
|
-
|
|
339
|
-
### 1. Keep Components Small
|
|
340
|
-
|
|
341
|
-
Each `.lego` file should represent a single, focused component.
|
|
342
|
-
|
|
343
|
-
✅ Good: `user-avatar.lego`, `user-name.lego`, `user-bio.lego`
|
|
344
|
-
❌ Bad: `entire-profile-page.lego`
|
|
345
|
-
|
|
346
|
-
### 2. Use Semantic Names
|
|
347
|
-
|
|
348
|
-
Name components after what they represent, not how they look:
|
|
349
|
-
|
|
350
|
-
✅ Good: `article-preview.lego`, `comment-list.lego`
|
|
351
|
-
❌ Bad: `blue-box.lego`, `flex-container.lego`
|
|
352
|
-
|
|
353
|
-
### 3. Organize by Feature
|
|
354
|
-
|
|
355
|
-
```
|
|
356
|
-
components/
|
|
357
|
-
├── user/
|
|
358
|
-
│ ├── user-card.lego
|
|
359
|
-
│ ├── user-avatar.lego
|
|
360
|
-
│ └── user-profile.lego
|
|
361
|
-
├── posts/
|
|
362
|
-
│ ├── post-item.lego
|
|
363
|
-
│ └── post-list.lego
|
|
364
|
-
└── shared/
|
|
365
|
-
├── app-button.lego
|
|
366
|
-
└── app-modal.lego
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
## Limitations
|
|
370
|
-
|
|
371
|
-
- `.lego` files require Vite—they don't work with direct `<script>` tag inclusion
|
|
372
|
-
- Each file creates exactly one component
|
|
373
|
-
- Component name is derived from filename (cannot be customized)
|
|
374
|
-
|
|
375
|
-
## Comparison: SFC vs Traditional
|
|
376
|
-
|
|
377
|
-
### Traditional (HTML Template)
|
|
378
|
-
|
|
379
|
-
```html
|
|
380
|
-
<template b-id="my-component">
|
|
381
|
-
<style>self { padding: 1rem; }</style>
|
|
382
|
-
<h1>[[ title ]]</h1>
|
|
383
|
-
</template>
|
|
384
|
-
|
|
385
|
-
<my-component b-data="{ title: 'Hello' }"></my-component>
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### SFC (.lego file)
|
|
389
|
-
|
|
390
|
-
```html
|
|
391
|
-
<!-- my-component.lego -->
|
|
392
|
-
<template>
|
|
393
|
-
<h1>[[ title ]]</h1>
|
|
394
|
-
</template>
|
|
395
|
-
|
|
396
|
-
<style>
|
|
397
|
-
self { padding: 1rem; }
|
|
398
|
-
</style>
|
|
399
|
-
|
|
400
|
-
<script>
|
|
401
|
-
export default {
|
|
402
|
-
title: 'Hello'
|
|
403
|
-
}
|
|
404
|
-
</script>
|
|
405
|
-
```
|
|
406
|
-
|
|
407
|
-
```html
|
|
408
|
-
<!-- index.html -->
|
|
409
|
-
<my-component></my-component>
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
Both work perfectly! Choose based on your project needs:
|
|
413
|
-
- **Small projects / prototypes** → HTML templates
|
|
414
|
-
- **Medium/large projects** → SFCs with Vite
|
|
415
|
-
|
|
416
|
-
## Next Steps
|
|
417
|
-
|
|
418
|
-
- See [complete SFC examples](/examples/sfc-showcase)
|
|
419
|
-
- Learn about the [Vite plugin API](/api/vite-plugin)
|
|
420
|
-
- Check out the [routing guide](/guide/routing) for building apps
|