lego-dom 1.5.0 → 2.1.1-alpha

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/.todo ADDED
@@ -0,0 +1,73 @@
1
+ P0
2
+
3
+ P1
4
+
5
+ P2
6
+ Add TypeScript definitions
7
+ Build DevTools extension
8
+ Implement proper plugin architecture
9
+
10
+ P3
11
+ SSR/hydration support
12
+ Code-splitting for routes
13
+ Integration with APM tools
14
+
15
+
16
+ A DevTools extension for LegoDOM would be a game-changer because it allows developers to "peek behind the plastic" of the Web Components. Since LegoDOM uses native Shadow DOM and Constructable Stylesheets, a standard "Elements" panel often hides the reactive logic.
17
+
18
+ Here is a vision for what Lego Studio DevTools could look like:
19
+
20
+ 1. The "Lego Tree" (Block Inspector)
21
+ Instead of a generic DOM tree, this panel would filter out the noise and show the structure of your application in terms of Blocks.
22
+
23
+ Visual Distinction: Lego blocks (Custom Elements) would be highlighted with a unique icon/color (e.g., a Lego brick icon).
24
+ Breadcrumbs: See the hierarchy of a block: App > Dashboard > Sidebar > UserProfile.
25
+ Select to Inspect: Click a block in the browser to automatically jump to its state and logic in the DevTools.
26
+ 2. Reactive State Monitor ($state & $db)
27
+ The "Heart" of the extension.
28
+
29
+ Live Store View: A real-time JSON tree of the current $db (persistent) and local $state.
30
+ Direct Modification: Edit a value in the DevTools (e.g., change this.user.name to "Admin") and watch the UI update instantly.
31
+ Update Flash: When a block re-renders because its state changed, it should briefly "flash" in the UI so you can track unnecessary renders.
32
+ 3. "Style Cascade" Explorer
33
+ Since LegoDOM has unique features like b-cascade and Constructable Stylesheets, tracking where a style came from is crucial.
34
+
35
+ Sheet Breakdown: For any selected block, show exactly which sheets are applied:
36
+ Explicit: Defined in the <style> tag of the
37
+ .lego
38
+ file.
39
+ External: Added via b-stylesheets.
40
+ Inherited: Passed down via b-cascade from a parent.
41
+ Toggle Switch: A checkbox next to each stylesheet to temporarily disable it for debugging.
42
+ 4. Directives Debugger
43
+ A specialized view to see how directives are behaving:
44
+
45
+ b-for: See the original array and the current "index" and "item" for the specific iteration you are hovering over.
46
+ b-if: See the expression that evaluated to true or false.
47
+ b-error: A "Heat Map" of error boundaries. If a block crashes, the DevTools should turn red and show you the exact $error object (message, stack trace) captured by the nearest boundary.
48
+ 5. "Open in Editor" Integration
49
+ Since LegoDOM maps blocks to
50
+ .lego
51
+ files:
52
+
53
+ Add a small button (or Ctrl+Click shortcut) on a block in DevTools that opens the corresponding file directly in VS Code or Lego Studio.
54
+ 6. Event Timeline (The "Radio" Tab)
55
+ LegoDOM uses
56
+ $emit
57
+ and
58
+ $ancestors
59
+ for communication.
60
+
61
+ A "Log" of all events flying through the app.
62
+ See who fired the event (
63
+ $emit
64
+ ) and who handled it.
65
+ Filter by event name to track specific user flows.
66
+ 7. Performance & "Snap" Profiler
67
+ LegoDOM uses the
68
+ snap()
69
+ function to sync state to DOM.
70
+ Snap Count: See how many times a block has "snapped". If a block is snapping 100 times in a second, you’ve found a reactivity loop!
71
+ Visual Aesthetic: The extension should feel like a "Command Center." Dark mode by default, vibrant primary colors for Lego bricks (Red for Errors, Blue for State, Yellow for Warnings), and smooth micro-animations when state updates.
72
+
73
+ How it would work technically: It would likely inject a small script (a "bridge") that hooks into Lego.registry and Lego.db to pipe data to the Extension's background script!
package/CHANGELOG.md CHANGED
@@ -1,8 +1,156 @@
1
1
  # Changelog
2
2
 
3
- ## [1.5.0] - 2026-01-19
3
+ ## [2.1.1] - 2026-01-23
4
+ Welcome to **LegoDOM v2**! This release represents a massive architectural shift to align the codebase and mental model with the "Lego" metaphor. We have moved away from Vue-centric terminology ("SFC", "Component", "Data") to Lego-centric terminology ("Lego File", "Block", "Logic").
5
+
6
+
7
+ ### Features
8
+
9
+ - **Reactive Persistence (`$db`):** New helper for persisting state to `localStorage` with automatic hydration, debouncing, and cross-tab synchronization.
10
+ ```html
11
+ <user-prefs b-logic="{
12
+ theme: $db('app.theme').default('light'),
13
+ volume: $db('app.vol').default(50).debounce(300)
14
+ }"></user-prefs>
15
+ ```
16
+ - **Auto-Load:** Values are automatically hydrated from `localStorage` on block creation.
17
+ - **Auto-Save:** Changes are automatically persisted (with optional debounce).
18
+ - **Cross-Tab Sync:** Updates in one tab automatically reflect in other tabs.
19
+
20
+ - **Vite Plugin DX:** Default `blocksDir` is now `./src` (was `./src/blocks`), allowing auto-discovery of `.lego` files anywhere in the source tree (as long as they follow the kebab-case hyphenation rule).
21
+
22
+ - **`$index` in `b-for`:** Access the current item's 0-based index in loops.
23
+ ```html
24
+ <li b-for="item in items">
25
+ #[[ $index + 1 ]]: [[ item.name ]]
26
+ </li>
27
+ ```
28
+
29
+ - **`b-key` Directive:** Optimized rendering for lists by providing stable identities for elements.
30
+
31
+ - **Error Boundaries (`b-error`):** Gracefully handle block crashes with cascading error boundaries!
32
+ ```html
33
+ <template b-error="error-card">
34
+ <user-profile></user-profile>
35
+ <activity-feed></activity-feed>
36
+ </template>
37
+ ```
38
+ - **Cascading:** Errors bubble up to the nearest parent boundary (template or instance level).
39
+ - **Error Context:** Error blocks receive `$error` object with `message`, `stack`, and `component` properties.
40
+ - **Automatic Recovery:** Replace crashed blocks with custom fallback UI instead of blank screens.
41
+ - **Global Fallback:** Falls back to `config.onError` if no boundary is found.
42
+
43
+ - **Cascading Stylesheets (`b-cascade`):** New attribute for selective stylesheet inheritance!
44
+ ```html
45
+ <body b-stylesheets="theme" b-cascade="theme">
46
+ <!-- All children inherit 'theme' automatically -->
47
+ <app-root></app-root>
48
+ </body>
49
+ ```
50
+ - **Inheritance:** Child blocks inherit stylesheets listed in `b-cascade` from their ancestors.
51
+ - **Explicit Override:** Adding `b-stylesheets` to a child merges with inherited styles (child-specific styles take precedence).
52
+ - **Debug API:** inspect inheritance with `Lego.debug.stylesheets($0)`.
53
+
54
+ - **New CLI Tooling:** Released `create-legodom` for instant project scaffolding.
55
+ ```bash
56
+ npm create legodom@latest
57
+ ```
58
+
59
+ - **Scoped Props (`b-logic`):** `b-logic` attributes now inherit the parent block's scope! You can pass state naturally to children without global variables.
60
+ ```html
61
+ <!-- 'user' is resolved from the parent block's state -->
62
+ <user-avatar b-logic="{ user: user }"></user-avatar>
63
+ ```
64
+
65
+ - **`$parent` Helper:** Added `this.$parent` (and template access) to easily find the nearest ancestor Block. It intelligently skips non-block elements (divs, spans) and handles Shadow DOM boundaries.
66
+ ```javascript
67
+ const parentBlock = this.$parent;
68
+
69
+ - **Public State API:**
70
+ - `element.state` is now the official public API for accessing the reactive proxy.
71
+ - `element._studs` is considered internal/private.
72
+
73
+ - **Lego Studio Audit:**
74
+ - `lego-studio` has been refactored to use "Block" terminology throughout the UI and codebase.
75
+
4
76
 
5
- ### Breaking Changes 🚨
77
+ ### Fixes
78
+
79
+ - **Init Optimization:** Fixed double render on initialization. Route is now initialized before DOM scan, preventing reactive updates from triggering a second render pass.
80
+ - **Before:** Elements rendered twice (once during `snap()`, once when `_matchRoute()` updated `Lego.globals.$route`).
81
+ - **After:** Elements render once with correct route data already available.
82
+ - **Impact:** Faster initial load, cleaner error logs (no duplicate errors).
83
+
84
+ - **Vite Plugin `b-cascade`/`b-error`:** Fixed a bug where `b-cascade` and `b-error` attributes defined in `.lego` files were not passed to `Lego.block()`, causing stylesheet inheritance and error boundaries to fail.
85
+
86
+ - **Lego Studio Scaffolding:** Fixed an issue where `.lego` files were expected to be in `src/block`, causing a blank screen upon scaffolding.
87
+
88
+ - **Global State Subscription Tracking:** Optimized global state updates from O(N) to O(K) complexity.
89
+ - Previously scanned all active blocks to check for global dependencies.
90
+ - Now maintains a dedicated `globalSubscribers` Set for instant lookups.
91
+ - Significantly faster for apps with thousands of blocks.
92
+
93
+ - **Fixed Circular Reference Leaks:** Enhanced `unsnap()` to clear all closure-captured references.
94
+ - Explicitly nulls `$emit`, and deletes `$parent`, `$route`, `$go` getters.
95
+ - Prevents memory leaks from closures capturing element references.
96
+ - Ensures complete garbage collection of removed blocks.
97
+
98
+ - **Auto-Discovery Logic:** Fixed a bug where elements present in the initial HTML were not detected by the `config.loader`. The discovery logic now scans the DOM during `Lego.init()` as well as on subsequent mutations.
99
+ - **b-stylesheets Support:** Fully implemented `b-stylesheets` to allow blocks to adopt constructable stylesheets.
100
+ - **SSR/Node Compatibility:** Added checks for `window`, `document`, and `Node` throughout the core to ensure LegoDOM can be imported and executed in Node.js/SSR environments without crashing.
101
+ - **Global Export:** `Lego` is now correctly exported to `global` in Node.js environments.
102
+
103
+ - **SSR/Node Compatibility:** Added checks for `window`, `document`, and `Node` throughout the core to ensure LegoDOM can be imported and executed in Node.js/SSR environments without crashing.
104
+ - **Global Export:** `Lego` is now correctly exported to `global` in Node.js environments.
105
+
106
+ ### Breaking Changes
107
+
108
+ - **`b-for` Sibling Rendering:** The element with `b-for` is now **replaced** by its clones as siblings, instead of nesting clones inside itself. This fixes invalid HTML like `<p><p>...</p></p>` and ensures correct CSS behavior (hover, focus, etc.).
109
+ ```html
110
+ <!-- Template -->
111
+ <ul>
112
+ <li b-for="item in items">[[ item ]]</li>
113
+ </ul>
114
+
115
+ <!-- Now renders as: -->
116
+ <ul>
117
+ <!--b-for: item in items-->
118
+ <li>Apple</li>
119
+ <li>Banana</li>
120
+ </ul>
121
+ ```
122
+
123
+ - **Terminological Refactor (Runtime & Docs):**
124
+ - **Components → Blocks:** The fundamental unit of UI is now a "Block".
125
+ - **SFC → Lego File:** Protocol-agnostic Single File Blocks.
126
+
127
+ - **API Renames:**
128
+ - `Lego.define()` is now **`Lego.block()`**. (Legacy alias maintained)
129
+ - `Lego.defineSFC()` is now **`Lego.defineLegoFile()`**.
130
+ - Internal: `deriveComponentName` is now `deriveBlockName`.
131
+
132
+ - **Attribute Renames:**
133
+ - `b-data` is now **`b-logic`**. (Legacy alias maintained)
134
+ - `b-styles` is now **`b-stylesheets`**.
135
+
136
+
137
+ ### Developer Experience
138
+
139
+ - **Error Logging:** Fixed 4 silent catch blocks that swallowed errors without any console output:
140
+ - `parseJSObject` now logs `b-data`/`b-logic` syntax errors.
141
+ - `b-key` warns when it resolves to `undefined`.
142
+ - localStorage hydration errors are now logged.
143
+ - Cross-tab sync errors are now logged.
144
+
145
+ - **b-cascade Guide:** Added documentation for cascading stylesheets, best practices for global themes vs block styles.
146
+ - **Directives:** Merged cascade docs into the main Directives guide.
147
+
148
+ - Added `docs/guide/persistence.md` covering the `$db` API, namespacing best practices, and performance warnings.
149
+
150
+
151
+ ## [1.5.1] - 2026-01-19
152
+
153
+ ### Breaking Changes
6
154
 
7
155
  - **Renamed `b-styles` to `b-stylesheets`:** The directive for injecting shared stylesheets into shadow roots has been renamed from `b-styles` to `b-stylesheets` to avoid confusion with the `style` attribute and better reflect its purpose (injecting Constructable Stylesheets).
8
156
  ```html
@@ -18,6 +166,11 @@
18
166
  - **New "Helper Utilities" Guide:** Added a dedicated page at `/docs/guide/helpers.md` documenting core instance methods like `$ancestors`, `$emit`, `$route`, `$go`, `$vars`, `$element`, and `$registry`.
19
167
  - **Performance Tips Updated:** Revised the "Performance Tips" section in the Directives guide to correctly compare `b-show` (toggle visibility) vs `b-if` (DOM insertion/removal) instead of the outdated `b-show` vs CSS comparison.
20
168
 
169
+ ## [1.5.0] - 2026-01-19
170
+
171
+ > [!WARNING] DEPRECATED
172
+ > This version has been deprecated due to an incomplete rename of `b-styles`. Please use `1.5.1`.
173
+
21
174
  ## [1.4.0] - 2026-01-16
22
175
 
23
176
  ### Breaking Changes
package/README.md CHANGED
@@ -19,6 +19,21 @@ Lego embraces the web platform. It turns standard HTML `<template>` tags into re
19
19
 
20
20
  ---
21
21
 
22
+ ## Quick Start
23
+
24
+ The fastest way to create a new LegoDOM project:
25
+
26
+ ```bash
27
+ npm create legodom@latest my-app
28
+ cd my-app
29
+ npm install
30
+ npm run dev
31
+ ```
32
+
33
+ This scaffolds a complete project with Vite, the LegoDOM plugin, and a sample component!
34
+
35
+ ---
36
+
22
37
  ## Quick Start (No Build Required)
23
38
 
24
39
  ```html
package/lego.js CHANGED
@@ -1,2 +1,2 @@
1
1
  import './main.js';
2
- export const Lego = window.Lego;
2
+ export const Lego = (typeof window !== 'undefined' ? window.Lego : global.Lego);