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.
Files changed (92) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/main.js +24 -3
  3. package/main.min.js +7 -0
  4. package/package.json +3 -1
  5. package/vite-plugin.js +0 -14
  6. package/.github/workflows/deploy-docs.yml +0 -56
  7. package/.legodom +0 -87
  8. package/docs/.vitepress/config.js +0 -162
  9. package/docs/api/config.md +0 -95
  10. package/docs/api/define.md +0 -58
  11. package/docs/api/directives.md +0 -50
  12. package/docs/api/globals.md +0 -29
  13. package/docs/api/index.md +0 -30
  14. package/docs/api/lifecycle.md +0 -40
  15. package/docs/api/route.md +0 -37
  16. package/docs/api/vite-plugin.md +0 -58
  17. package/docs/contributing/01-welcome.md +0 -38
  18. package/docs/contributing/02-registry.md +0 -133
  19. package/docs/contributing/03-batcher.md +0 -110
  20. package/docs/contributing/04-reactivity.md +0 -87
  21. package/docs/contributing/05-caching.md +0 -59
  22. package/docs/contributing/06-init.md +0 -136
  23. package/docs/contributing/07-observer.md +0 -72
  24. package/docs/contributing/08-snap.md +0 -140
  25. package/docs/contributing/09-diffing.md +0 -69
  26. package/docs/contributing/10-studs.md +0 -78
  27. package/docs/contributing/11-scanner.md +0 -117
  28. package/docs/contributing/12-render.md +0 -138
  29. package/docs/contributing/13-directives.md +0 -243
  30. package/docs/contributing/14-events.md +0 -57
  31. package/docs/contributing/15-router.md +0 -57
  32. package/docs/contributing/16-state.md +0 -47
  33. package/docs/contributing/17-legodom.md +0 -48
  34. package/docs/contributing/index.md +0 -24
  35. package/docs/examples/form.md +0 -42
  36. package/docs/examples/index.md +0 -104
  37. package/docs/examples/routing.md +0 -409
  38. package/docs/examples/sfc-showcase.md +0 -34
  39. package/docs/examples/todo-app.md +0 -383
  40. package/docs/guide/cdn-usage.md +0 -328
  41. package/docs/guide/components.md +0 -412
  42. package/docs/guide/directives.md +0 -539
  43. package/docs/guide/directory-structure.md +0 -248
  44. package/docs/guide/faq.md +0 -210
  45. package/docs/guide/getting-started.md +0 -262
  46. package/docs/guide/index.md +0 -88
  47. package/docs/guide/lifecycle.md +0 -525
  48. package/docs/guide/quick-start.md +0 -49
  49. package/docs/guide/reactivity.md +0 -415
  50. package/docs/guide/routing.md +0 -334
  51. package/docs/guide/server-side.md +0 -134
  52. package/docs/guide/sfc.md +0 -420
  53. package/docs/guide/templating.md +0 -388
  54. package/docs/index.md +0 -160
  55. package/docs/public/logo.svg +0 -17
  56. package/docs/router/basic-routing.md +0 -103
  57. package/docs/router/cold-entry.md +0 -91
  58. package/docs/router/history.md +0 -69
  59. package/docs/router/index.md +0 -73
  60. package/docs/router/resolver.md +0 -74
  61. package/docs/router/surgical-swaps.md +0 -134
  62. package/docs/tutorial/01-project-setup.md +0 -152
  63. package/docs/tutorial/02-your-first-component.md +0 -226
  64. package/docs/tutorial/03-adding-routes.md +0 -279
  65. package/docs/tutorial/04-multi-page-app.md +0 -329
  66. package/docs/tutorial/05-state-and-globals.md +0 -285
  67. package/docs/tutorial/index.md +0 -40
  68. package/examples/vite-app/README.md +0 -71
  69. package/examples/vite-app/index.html +0 -42
  70. package/examples/vite-app/package.json +0 -18
  71. package/examples/vite-app/src/app.css +0 -3
  72. package/examples/vite-app/src/app.js +0 -29
  73. package/examples/vite-app/src/components/app-navbar.lego +0 -34
  74. package/examples/vite-app/src/components/customers/customer-details.lego +0 -24
  75. package/examples/vite-app/src/components/customers/customer-orders.lego +0 -21
  76. package/examples/vite-app/src/components/customers/order-list.lego +0 -55
  77. package/examples/vite-app/src/components/greeting-card.lego +0 -41
  78. package/examples/vite-app/src/components/sample-component.lego +0 -75
  79. package/examples/vite-app/src/components/shells/customers-shell.lego +0 -21
  80. package/examples/vite-app/src/components/side-menu.lego +0 -46
  81. package/examples/vite-app/src/components/todo-list.lego +0 -239
  82. package/examples/vite-app/src/components/widgets/user-card.lego +0 -27
  83. package/examples/vite-app/vite.config.js +0 -22
  84. package/tests/error.test.js +0 -74
  85. package/tests/main.test.js +0 -103
  86. package/tests/memory.test.js +0 -68
  87. package/tests/monitoring.test.js +0 -74
  88. package/tests/naming.test.js +0 -74
  89. package/tests/parse-lego.test.js +0 -65
  90. package/tests/security.test.js +0 -67
  91. package/tests/server.test.js +0 -114
  92. package/tests/syntax.test.js +0 -67
@@ -1,91 +0,0 @@
1
-
2
- # The Cold Start: Self-Healing Layouts
3
-
4
- In the previous section, we learned how to use `b-target` to surgically swap fragments while the app is running. But what happens when a user types `myapp.com/messaging/123` directly into the address bar or hits **Refresh (F5)**?
5
-
6
- In a traditional nested router, the framework handles this automatically. In LegoDOM, we use a more powerful, explicit pattern called **Self-Healing Layouts**.
7
-
8
- ## The Cold Entry/Start Challenge
9
-
10
- When a "Cold Start" occurs:
11
-
12
- 1. The browser requests the URL from the server.
13
-
14
- 2. The server returns the base `index.html`.
15
-
16
- 3. LegoDOM matches the route `/messaging/:id` to the `messaging-shell` component.
17
-
18
- 4. The `messaging-shell` mounts, but the `#chat-window` target is empty (or showing its default "Select a conversation" text).
19
-
20
-
21
- The user is at the correct URL, but the **fragment** (the specific email or chat) is missing.
22
-
23
- ## The Solution: The "Self-Healing" Mounted Hook
24
-
25
- To fix this, the Parent Shell must be responsible for "healing" its own internal state if it detects a parameter in the URL on mount.
26
-
27
- ### Step 1: Define the Shared Route
28
-
29
- First, ensure your route configuration points both the list and the detail view to the same Shell component.
30
-
31
- ```js
32
- Lego.route('/messaging', 'messaging-shell');
33
- Lego.route('/messaging/:id', 'messaging-shell');
34
-
35
- ```
36
-
37
- ### Step 2: Implement the Healing Logic
38
-
39
- Inside your `messaging-shell` component, use the `mounted()` hook to check for the presence of a parameter.
40
-
41
- ```js
42
- Lego.define('messaging-shell', `
43
- <div class="layout">
44
- <aside class="sidebar">
45
- <!-- List of threads -->
46
- </aside>
47
-
48
- <main id="chat-window">
49
- <!-- Default content -->
50
- <p>Select a conversation.</p>
51
- </main>
52
- </div>
53
- `, {
54
- mounted() {
55
- // Check if we arrived here via a deep-link (e.g., /messaging/123)
56
- if (this.$route.params.id) {
57
- // SURGICAL HEALING:
58
- // Tell Lego to render the current URL into the local target.
59
- // This pulls the 'messaging-details' fragment into the main window.
60
- this.$go(window.location.pathname, '#chat-window').get();
61
- }
62
- }
63
- });
64
-
65
- ```
66
-
67
- ## Why This Pattern is Superior
68
-
69
- 1. **Explicit Control**: You decide exactly when and how the child fragment appears.
70
-
71
- 2. **Layout Persistence**: The Sidebar and Header are rendered immediately. The user sees the "Shell" instantly, while the specific data fragment "pops" in a few milliseconds later. This improves the **Perceived Performance**.
72
-
73
- 3. **No Nested Config Hell**: You don't need a complex tree of routes. The DOM structure of your shell defines the nesting naturally.
74
-
75
-
76
- ## Using Slots for Better Defaults
77
-
78
- You can use the native `<slot>` tag inside your target area to provide a better "Loading" or "Empty" experience before the healing happens.
79
-
80
- ```html
81
- <main id="chat-window">
82
- <slot>
83
- <div class="skeleton-loader">Loading your message...</div>
84
- </slot>
85
- </main>
86
-
87
- ```
88
-
89
- ## Summary
90
-
91
- "Self-Healing" is the bridge between a static website and a dynamic application. It ensures that your surgical routing works perfectly even on hard refreshes. By using the `mounted()` hook and the `$go` helper, your components become intelligent enough to manage their own internal layout based on the global URL state.
@@ -1,69 +0,0 @@
1
-
2
- # Smart History & The Back Button
3
-
4
- One of the biggest frustrations in modern web development is "breaking the back button." When you use JavaScript to update only part of a page, the browser often doesn't realize a navigation event occurred. If the user hits "Back," they might be booted out of your app entirely.
5
-
6
- LegoDOM solves this using a system called **Smart History**. It ensures that even surgical, fragment-level updates are recorded and reversible.
7
-
8
- ## How Traditional Routers Fail
9
-
10
- In a standard SPA, the router usually manages a single "current view." When you navigate:
11
-
12
- 1. The URL changes.
13
-
14
- 2. The old component is destroyed.
15
-
16
- 3. The new component is mounted.
17
-
18
-
19
- Because the whole page (or the main outlet) is replaced, the browser's history stack is simple: Page A -> Page B. But in a complex layout like LinkedIn, you might have changed the chat window 5 times while the sidebar stayed exactly the same. Users expect the "Back" button to cycle through those chats, not take them back to the login screen.
20
-
21
- ## The LegoDOM Approach: Target Tracking
22
-
23
- When you use `b-link` with a `b-target`, LegoDOM does two things simultaneously:
24
-
25
- 1. It updates the browser URL using the History API.
26
-
27
- 2. It stores a "snapshot" of the target information in the history state.
28
-
29
-
30
- ### Inside the History State
31
-
32
- Every time a surgical swap happens, LegoDOM saves the target selector in the `history.state`. It looks something like this:
33
-
34
- ```js
35
- // Internal representation
36
- history.pushState({
37
- legoTargets: '#chat-window'
38
- }, '', '/messaging/124');
39
-
40
- ```
41
-
42
- ## The "Popstate" Magic
43
-
44
- When a user hits the **Back** or **Forward** button, the browser triggers a `popstate` event. LegoDOM intercepts this event and checks if the incoming state contains `legoTargets`.
45
-
46
- - **If `legoTargets` exists:** LegoDOM performs a surgical swap. It takes the URL the browser is moving to and renders it _only_ into the specified target (e.g., `#chat-window`).
47
-
48
- - **If no target exists:** It performs a global swap in the `<lego-router>`.
49
-
50
-
51
- ## Why This Matters for User Experience
52
-
53
- ### 1. Persistence of Sibling State
54
-
55
- Because only the target is swapped during history navigation, your sidebar remains untouched. If the user had scrolled halfway down a list of 500 emails, they stay exactly at that scroll position as they hit "Back" and "Forward" to view different email details.
56
-
57
- ### 2. Zero-Config History
58
-
59
- You don't have to write a single line of code to make the back button work. As long as you are using `b-link` and `b-target`, the framework handles the history reconciliation automatically.
60
-
61
- ### 3. Deep Link Consistency
62
-
63
- Because the history state uses the same URLs as your standard links, a "Back" navigation and a "Hard Refresh" result in the same UI state (thanks to the Self-Healing logic we covered in the previous section).
64
-
65
- ## Summary
66
-
67
- Smart History is the "glue" that makes a multi-fragment interface feel like a single, cohesive application. It respects the user's intent by making the browser's navigation tools work for specific parts of the page, not just the whole page.
68
-
69
- Next, we'll dive into the mechanics of how LegoDOM finds these targets in complex, nested DOM trees in **Target Resolver: Scoping and Logic**.
@@ -1,73 +0,0 @@
1
-
2
- # LegoDOM vs Traditional SPA Router
3
-
4
- Traditional Single Page Application (SPA) routers (like React Router, Vue Router, or Angular's Router) rely on a **Centralized Configuration Tree**. As your project grows, this JSON or JavaScript object becomes a "source of truth" that is fragile, hard to maintain, and forces a rigid hierarchy on your UI.
5
-
6
- **LegoDOM breaks this pattern.**
7
-
8
- In Lego, we don't nest routes in a config file. Instead, we use the **URL as the Data Source** and the **DOM as the Layout Engine**.
9
-
10
- ## The Shift in Thinking
11
-
12
- |Concept | Traditional SPAs | LegoDOM |
13
- |-----------|---------------|--------|
14
- | **Route Map** | Nested JSON Objects | Flat Component List |
15
- | **Nesting** | Defined in JS | Defined by your HTML structure |
16
- | **UI Updates** | "Nuclear" (Re-renders parent + children) | "Surgical" (Swaps exactly one fragment"|
17
- | **State** | Lost unless lifted to Global Store | Persists naturally in unaffected siblings |
18
-
19
-
20
- ## The "Flat Route" Philosophy
21
-
22
- In a large project, your route definitions should remain a flat list of "Shells" (Layouts).
23
-
24
- ```js
25
- // A flat list of structural shells
26
- Lego.route('/', 'home-shell');
27
- Lego.route('/messaging', 'messaging-shell');
28
- Lego.route('/messaging/:id', 'messaging-shell');
29
- Lego.route('/settings', 'settings-shell');
30
-
31
- ```
32
-
33
- By pointing `/messaging` and `/messaging/:id` to the same **Shell**, you are telling Lego: _"I want the same layout for both URLs, but I'll decide how to fill the holes inside the component based on the URL parameters."_
34
-
35
- ## Why This Scales
36
-
37
- In a LinkedIn-sized project, a traditional router would need to know that the `ChatWindow` is a child of the `MessagingLayout`. If you wanted to move that `ChatWindow` to the `HomeLayout`, you’d have to refactor your entire route tree.
38
-
39
- In Lego, you just change the **Target**. Because the components are agnostic, they don't care who their parent is. They just care about where the `b-target` tells them to go.
40
-
41
- ### Defining with SFCs
42
-
43
- LegoDOM supports the use of Single File Components and this works seamlessly with the Router. You can define your Shell components via the `<template>` tag. This keeps our architectural "Shells" clean and readable.
44
-
45
- ```html
46
- <style></style>
47
- <template b-id="home-shell">
48
- <div class="layout">
49
- <nav-bar></nav-bar>
50
- <lego-router></lego-router> <!-- The global outlet -->
51
- </div>
52
- </template>
53
- <script>
54
- export default {
55
- name: 'HomeShell'
56
- }
57
- </script>
58
- ```
59
-
60
- ### Key Vocabulary for the Tutorial
61
-
62
- Throughout this guide, we will refer to three primary concepts:
63
-
64
- 1. **The Shell**: The high-level SFC that provides the grid, navigation, and sidebar.
65
-
66
- 2. **The Fragment**: A self-contained SFC (like a chat thread or a profile header) that is surgically injected into a shell.
67
-
68
- 3. **The Target**: A CSS selector, Tag Name, or ID that acts as the "destination hole" for a fragment.
69
-
70
-
71
- ## What's Next?
72
-
73
- We will move away from the theory and look at **Basic Routing**. We'll see how the `<lego-router>` element acts as the default gateway for your application and how Lego identifies which component to mount when the page first loads.
@@ -1,74 +0,0 @@
1
-
2
- # 06. Target Resolver: Scoping and Logic
3
-
4
- In the previous chapters, we used `b-target` to send content to different parts of the page. But how does LegoDOM actually find those "holes" in a complex, nested DOM? This is where the **Target Resolver** comes in.
5
-
6
- The Target Resolver is a prioritized logic engine that ensures your links always find the correct destination, even in massive applications with thousands of components.
7
-
8
- ## The Problem with Global Selectors
9
-
10
- In traditional JavaScript, if you use `document.querySelector('#detail-view')`, the browser searches the entire page. This is fine for small sites, but in a "Web Component First" architecture, it causes two major issues:
11
-
12
- 1. **ID Collisions**: If you accidentally use the same ID in two different components, your link might update the wrong part of the page.
13
-
14
- 2. **Tight Coupling**: Your sidebar link has to know the exact global ID of the main content area, making it harder to move components around.
15
-
16
-
17
- ## The Resolver Hierarchy
18
-
19
- When you click a `b-link` with a `b-target`, LegoDOM follows a strict search order to resolve the target:
20
-
21
- ### 1. Local Component Scope (The Primary Search)
22
-
23
- LegoDOM first looks for the target **inside the component that contains the link**.
24
-
25
- If you use `b-target="email-view"`, Lego looks for an `<email-view>` tag _within the current parent shell_. This allows you to have multiple instances of the same layout on one screen without them interfering with each other.
26
-
27
- ### 2. Tag Name Resolution (The Web Component Way)
28
-
29
- If your target doesn't start with a `#`, Lego treats it as a **Component Tag Name**.
30
-
31
- ```
32
- <!-- Lego looks for a <thread-view> tag nearby -->
33
- <a href="/chat/1" b-link b-target="thread-view">Open Chat</a>
34
-
35
- <thread-view></thread-view>
36
-
37
- ```
38
-
39
- This is the most "Enterprise" way to build. It makes your HTML self-documenting. You aren't targeting a generic `div`; you are targeting a specific functional slot.
40
-
41
- ### 3. Global ID Fallback
42
-
43
- If no local tag or element matches, the resolver expands its search to the entire `document` using ID selectors.
44
-
45
- ```
46
- <!-- Lego looks for any element with id="global-sidebar" -->
47
- <a href="/menu" b-link b-target="#global-sidebar">Menu</a>
48
-
49
- ```
50
-
51
- ### 4. The Router Default
52
-
53
- If the resolver exhausts all options and still can't find the target, it defaults to the `<lego-router>`. This ensures that even if you make a typo in your target name, the user still sees the content (it just might take over the whole page instead of a small fragment).
54
-
55
- ## Advanced: The Functional Target
56
-
57
- For highly dynamic UIs, `b-target` can even be a function or a dynamic expression.
58
-
59
- ```
60
- <!-- Logic decides the target based on screen size -->
61
- <a href="/settings" b-link b-target="[[ isMobile ? '#main' : 'settings-pane' ]]">
62
- Settings
63
- </a>
64
-
65
- ```
66
-
67
- ## Why This Matters
68
-
69
- By prioritizing local tags over global IDs, LegoDOM encourages **Encapsulation**. Your components become "Black Boxes" that manage their own internal routing targets. This means you can take a complex "Messaging Shell" and drop it into a "Dashboard Shell" without changing a single line of routing code—the targets will still resolve correctly because they are scoped to their parents.
70
-
71
- ## Summary
72
-
73
- The Target Resolver turns string attributes into intelligent DOM navigation. It respects the boundaries of your Web Components while providing a robust fallback system.
74
-
@@ -1,134 +0,0 @@
1
- # Surgical Swaps: Mastering b-target
2
-
3
- The true power of LegoDOM lies in its ability to perform **Surgical Swaps**. In a traditional application, clicking a link often causes the entire page to re-render, destroying the state of your sidebar, header, or scroll position.
4
-
5
- With `b-target` (and optionally `b-link`), we can choose to update only a specific "fragment" of the page.
6
-
7
- ## The Problem with "Nuclear" Navigation
8
-
9
- Imagine a messaging app (like LinkedIn or Slack). You have a sidebar full of conversations. When you click a message, you want the chat window to update, but you **don't** want the sidebar to reload.
10
-
11
- If the sidebar reloads:
12
-
13
- 1. The scroll position is lost.
14
-
15
- 2. Any search text in the sidebar is cleared.
16
-
17
- 3. The UI "flickers," making the app feel slow.
18
-
19
-
20
- ## The Solution: `b-target`
21
-
22
- The `b-target` directive allows a link to specify exactly where the new component should be rendered. It implies `b-link` (history update) by default.
23
-
24
-
25
- ### Example: messaging-shell.html
26
-
27
- In this SFC, we define a layout with a sidebar and a main content area. Clicking a contact updates _only_ the `<main>` area.
28
-
29
- ```html
30
- <!-- messaging-shell.html -->
31
- <template>
32
- <div class="messaging-layout">
33
- <aside class="sidebar">
34
- <h2>Contacts</h2>
35
- <nav>
36
- <a href="/chat/alice" b-target="#chat-window">Alice</a>
37
- <a href="/chat/bob" b-target="#chat-window">Bob</a>
38
- </nav>
39
- </aside>
40
-
41
- <main id="chat-window">
42
- <p>Select a contact to start chatting.</p>
43
- </main>
44
- </div>
45
- </template>
46
-
47
- <script>
48
- export default {
49
- mounted() {
50
- console.log("Messaging shell ready.");
51
- }
52
- }
53
- </script>
54
-
55
- <style>
56
- .messaging-layout {
57
- display: flex;
58
- height: 100vh;
59
- }
60
- .sidebar {
61
- width: 300px;
62
- border-right: 1px solid #ccc;
63
- }
64
- #chat-window {
65
- flex: 1;
66
- padding: 20px;
67
- }
68
- </style>
69
-
70
- ```
71
-
72
- ### 1. Targeting by ID
73
-
74
- You can tell Lego to find a specific element by its ID and replace its contents.
75
-
76
- ```html
77
- <!-- messaging-shell.html -->
78
- <template>
79
- <div class="layout">
80
- <aside class="sidebar">
81
- <div b-for="chat in threads">
82
- <!-- Parent component (this shell) binds data to these links -->
83
- <a href="/messaging/[[chat.id]]" b-target="#chat-window">
84
- [[chat.userName]]
85
- </a>
86
- </div>
87
- </aside>
88
-
89
- <main id="chat-window">
90
- <!-- Only this area will change when a link is clicked -->
91
- <p>Select a conversation to begin.</p>
92
- </main>
93
- </div>
94
- </template>
95
-
96
- ```
97
-
98
- ### 2. Targeting by Component Tag (The Web Component Way)
99
-
100
- Because Lego is built on Custom Elements, you can target a component tag directly. The framework will find that tag and swap its internal content.
101
-
102
- ```html
103
- <a href="/profile/settings" b-target="settings-view">Edit Settings</a>
104
-
105
- <settings-view>
106
- <!-- Content gets swapped here -->
107
- </settings-view>
108
-
109
- ```
110
-
111
- ## How the Target Resolver Works
112
-
113
- When you click a link with a `b-target`, the LegoDOM **Target Resolver** follows a specific hierarchy:
114
-
115
- 1. **Local Scope**: It looks for the target inside the current component first. This prevents "ID collisions" if you have multiple instances of a layout.
116
-
117
- 2. **Component Match**: If the target doesn't start with `#`, it treats it as a tag name (e.g., `thread-view`).
118
-
119
- 3. **Global Fallback**: If it can't find a local match, it searches the entire document.
120
-
121
- 4. **Router Fallback**: If no target is found, it defaults back to the `<lego-router>`.
122
-
123
-
124
- ## Smart History
125
-
126
- Even though we are only swapping a small part of the DOM, LegoDOM is smart enough to update the browser's address bar.
127
-
128
- When a surgical swap happens, Lego saves the "target" information into the browser's history state (`history.state.legoTargets`). This means that when a user hits the **Back Button**, Lego knows exactly which fragment needs to be swapped back to its previous state.
129
-
130
- ## Summary
131
-
132
- `b-target` turns your web app into a high-performance workspace. By keeping the "Shell" alive and only swapping "Fragments," you maintain state, eliminate flickers, and provide a desktop-like experience.
133
-
134
- Next, we will tackle the most common question: **"What happens if I refresh the page while looking at a surgical fragment?"** We'll explore the **Cold Start: Self-Healing Layouts**.
@@ -1,152 +0,0 @@
1
- # Step 1: Project Setup
2
-
3
- Let's create a new LegoDOM project from scratch. By the end of this page, you'll have a fully configured development environment ready to build components.
4
-
5
- ## Create a New Project
6
-
7
- Open your terminal and run:
8
-
9
- ```bash
10
- # Create a new Vite project
11
- npm create vite@latest my-lego-app -- --template vanilla
12
-
13
- # Enter the project
14
- cd my-lego-app
15
-
16
- # Install LegoDOM
17
- npm install lego-dom
18
-
19
- # Install dependencies
20
- npm install
21
- ```
22
-
23
- ## Configure Vite
24
-
25
- Create or replace `vite.config.js` in your project root:
26
-
27
- ```javascript
28
- import { defineConfig } from 'vite';
29
- import legoPlugin from 'lego-dom/vite-plugin';
30
-
31
- export default defineConfig({
32
- plugins: [
33
- legoPlugin({
34
- componentsDir: './src/components', // Where your .lego files live
35
- include: ['**/*.lego'] // File pattern to match
36
- })
37
- ]
38
- });
39
- ```
40
-
41
- ## Project Structure
42
-
43
- Create this folder structure:
44
-
45
- ```
46
- my-lego-app/
47
- ├── index.html ← Your app's entry HTML
48
- ├── src/
49
- │ ├── app.js ← Your app's entry JavaScript ⭐
50
- │ └── components/ ← Your .lego files go here
51
- │ └── (empty for now)
52
- ├── vite.config.js ← Vite configuration
53
- └── package.json
54
- ```
55
-
56
- ::: warning The Magic File: `app.js`
57
- This is where **everything comes together**. Your routes, global state, and initialization all happen here. We'll build it in the next section.
58
- :::
59
-
60
- ## Set Up Your Entry Point
61
-
62
- Replace the contents of `src/app.js` (create it if it doesn't exist):
63
-
64
- ```javascript
65
- // 1. Import the Lego core
66
- import { Lego } from 'lego-dom';
67
-
68
- // 2. Import the virtual module that auto-discovers .lego files
69
- import registerComponents from 'virtual:lego-components';
70
-
71
- // 3. Register all discovered components
72
- registerComponents();
73
-
74
- // 4. Define your routes (we'll add these soon!)
75
- // Lego.route('/', 'home-page');
76
- // Lego.route('/login', 'login-page');
77
-
78
- // 5. Initialize the engine
79
- await Lego.init();
80
- ```
81
-
82
- > **What's happening here?**
83
- > - Lines 1-3: Import LegoDOM and auto-register every `.lego` file in your `components/` folder
84
- > - Line 4: Routes map URLs to components (commented out until we create them)
85
- > - Line 5: `Lego.init()` starts the reactivity engine, routing, and DOM observation
86
-
87
- ## Set Up Your HTML
88
-
89
- Replace `index.html`:
90
-
91
- ```html
92
- <!DOCTYPE html>
93
- <html lang="en">
94
- <head>
95
- <meta charset="UTF-8">
96
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
97
- <title>My Lego App</title>
98
- <style>
99
- * { margin: 0; padding: 0; box-sizing: border-box; }
100
- body {
101
- font-family: system-ui, -apple-system, sans-serif;
102
- background: #f5f5f5;
103
- min-height: 100vh;
104
- }
105
- </style>
106
- </head>
107
- <body>
108
- <!-- The router renders your page components here -->
109
- <lego-router></lego-router>
110
-
111
- <!-- Load your app -->
112
- <script type="module" src="/src/app.js"></script>
113
- </body>
114
- </html>
115
- ```
116
-
117
- ## Run It
118
-
119
- ```bash
120
- npm run dev
121
- ```
122
-
123
- Open `http://localhost:5173` in your browser. You'll see a blank page—that's expected! We haven't created any components yet.
124
-
125
- ## What You've Done
126
-
127
- ✅ Created a Vite project
128
- ✅ Installed LegoDOM
129
- ✅ Configured the Vite plugin
130
- ✅ Set up `app.js` with the Lego initialization pattern
131
- ✅ Added `<lego-router>` to your HTML
132
-
133
- ## The Key Insight
134
-
135
- ::: tip Where Config Goes: The Golden Rule
136
- **Everything related to your app's setup goes in `app.js`:**
137
- - Component registration → `registerComponents()`
138
- - Route definitions → `Lego.route(...)`
139
- - Global state → `Lego.globals.user = ...`
140
- - Initialization → `Lego.init()`
141
-
142
- Your `index.html` just needs `<lego-router>` and a script tag. That's it.
143
- :::
144
-
145
- ---
146
-
147
- <div style="display: flex; justify-content: space-between; margin-top: 3rem;">
148
- <span></span>
149
- <a href="./02-your-first-component" style="display: inline-block; background: #4CAF50; color: white; padding: 0.75rem 1.5rem; border-radius: 6px; text-decoration: none; font-weight: 600;">
150
- Next: Your First Component →
151
- </a>
152
- </div>