LearnNewsExamplesServices
Frontmatter
id7069
titleEnhance LivePreview for Modern JavaScript and Functional Components
stateClosed
labels
enhancement
assigneestobiu
createdAtJul 16, 2025, 1:45 AM
updatedAtJul 16, 2025, 1:46 AM
githubUrlhttps://github.com/neomjs/neo/issues/7069
authortobiu
commentsCount0
parentIssue6992
subIssues[]
subIssuesCompleted0
subIssuesTotal0
blockedBy[]
blocking[]
closedAtJul 16, 2025, 1:46 AM

Enhance LivePreview for Modern JavaScript and Functional Components

Closed v10.0.0-beta.6 enhancement
tobiu
tobiu commented on Jul 16, 2025, 1:45 AM

Problem

The LivePreview component, which is essential for interactive documentation and tutorials, had several limitations that hindered its ability to run modern JavaScript examples, particularly those using functional components and named imports.

  1. No Named Import Support: The component could only parse default imports (e.g., import Foo from ...), failing on named imports (e.g., import { bar } from ...). This prevented the use of hooks and other modular utilities in live examples.

  2. Fragile Class Detection: The mechanism for identifying the main component to render relied on a regular expression (classDeclarationRegex) that searched for the class ... syntax. This approach failed for components created with factory functions, such as defineComponent(), which do not use the class keyword directly.

  3. Imprecise Component Validation: The check to ensure the identified class was a renderable component was either too broad (checking for Neo.core.Base, which could include data stores) or too narrow. This could lead to errors or prevent valid components from rendering.

Solution

To address these issues, the LivePreview.mjs component was significantly refactored:

  1. Enhanced Import Parsing: The importRegex was updated to correctly parse both default and named import syntax. The logic in doRunSource() was adjusted to generate the correct variable declarations (const Foo = ... for default, and const {bar} = ... for named).

  2. Robust Component Identification: The unreliable findLastClassName() method was replaced with findSetupClassName(). This new method uses a more robust regex (setupClassRegex) to find the variable assigned the result of Neo.setupClass(...). This is a far more reliable pattern for identifying the primary component, regardless of whether it was defined with class or a factory function.

  3. Specific Component Type Check: The validation logic was improved to explicitly check if the resolved class is a prototype of either Neo.component.Base or Neo.functional.component.Base. This ensures that only renderable UI components are passed to the container, preventing errors with other base classes.

tobiu assigned to @tobiu on Jul 16, 2025, 1:45 AM
tobiu added the enhancement label on Jul 16, 2025, 1:45 AM
tobiu added parent issue #6992 on Jul 16, 2025, 1:45 AM
tobiu referenced in commit b290bdf - "Enhance LivePreview for Modern JavaScript and Functional Components #7069" on Jul 16, 2025, 1:45 AM
tobiu closed this issue on Jul 16, 2025, 1:46 AM