Lightning Web Components

repository·master·Indexed 23 days ago

https://github.com/salesforce/lwc

The source code for the Lightning Web Components (LWC) Engine and Compiler, providing core infrastructure for high-performance web components. Includes the @lwc/compiler for transforming JS, HTML, and CSS; @lwc/babel-plugin-component for transforming decorators like @api, @wire, and @track; @lwc/engine-core for component base classes and reactivity; and @lwc/aria-reflection for ARIA string reflection polyfills.

Tokens
53.9K
Snippets
107
Records
306
Agent score
79%

What's inside lwc

  1. Introduction to Lightning Web Components (LWC)

    master
    Lightning Web Components (LWC) is an enterprise-grade web components foundation for building user interfaces. It provides a simple authoring format (HTML, JavaScript, and CSS) that is compiled into low-level Web Component APIs. Components are encapsulated using Shadow DOM, and the @lwc/synthetic-shadow package can be used as an optional polyfill for older browsers.
  2. How ARIA string reflection works

    master

    Once @lwc/aria-reflection is imported, it applies the polyfill globally to Element.prototype. This allows you to interact with ARIA attributes either via setAttribute/getAttribute or via direct property access. The two methods are reflected: changing the attribute updates the property, and changing the property updates the attribute.

    Example of bidirectional reflection:

    element.setAttribute('aria-pressed', 'true');
    console.log(element.ariaPressed); // true
    
    element.ariaPressed = false;
    console.log(element.getAttribute('aria-pressed')); // false
  3. Experimental APIs: Signals and Context

    master

    LWC includes several experimental APIs related to Signals and Context. These require the ENABLE_EXPERIMENTAL_SIGNALS feature to be enabled.

    Warning: Many of these APIs are marked as 'Not intended for external use' and may change or break without notice. Use them with caution in production environments.

  4. How the EventTarget polyfill works

    master

    The EventTarget polyfill ensures that event management methods are available across different browser environments.

    In modern browsers, addEventListener(), removeEventListener(), and dispatchEvent() are part of the EventTarget interface. However, in legacy environments like IE11, these methods are defined on the Node interface instead. To maintain compatibility, the polyfill detects if EventTarget is undefined and, if so, patches Node.prototype to provide these methods.

    Additionally, the polyfill intercepts listener invocations via addEventListener() to filter events based on their bubbles and composed configuration properties.

  5. Configure LWC module resolution

    master

    LWC module resolution is configured using a lwc.config.json file or an lwc key in package.json. The resolver iterates through the modules array and returns the first match.

    There are three types of module records:

    1. Alias module record: Maps a specific name to a specific file path.
    2. Directory module record: Specifies a folder where LWC modules are resolved based on an opinionated folder structure.
    3. NPM package module record: Indicates an NPM package that exposes LWC modules.

    Example configuration in lwc.config.json:

    {
        "modules": [
            {
                "name": "ui/button",
                "path": "src/modules/ui/button/button.js"
            },
            {
                "dir": "src/modules"
            },
            {
                "npm": "@ui/components"
            }
        ]
    }
  6. Be aware of Synthetic Shadow Root compromises and limitations

    master

    When using the Synthetic Shadow Root, be aware of the following behavioral differences compared to native Shadow DOM:

    • <slot> Default Content: The default content for <slot> elements is always empty.
    • slotchange Event Behavior: The slotchange event is only available directly on the <slot> element and does not bubble. This restriction is implemented to avoid the performance overhead of using MutationObserver for all slots. The engine only triggers this event when it detects interest, saving CPU cycles.
    • MutationObserver Memory Leaks: If you use MutationObserver to watch changes in a DOM tree, you must manually disconnect it to prevent memory leaks.
    • Observation Scope: A component can only observe mutations within its own template. It cannot observe mutations occurring within the shadow tree of other custom elements.
  7. What @lwc/babel-plugin-component transforms

    master

    The plugin performs several key transformations to prepare LWC components for the engine:

    Decorator Transformations

    • @api is transformed into publicProperties and publicMethods static properties.
    • @wire is transformed into a wire static property.
    • @track is transformed into a track static property.

    LWC Component Class Sugar

    • Lifecycle Hooks: Validates and checks for misspelled lifecycle hooks.
    • Template Injection: Automatically imports and injects a render method from a collocated template if the component class does not explicitly implement one.

    Optimizations

    • If the compiler injects a default template into a component, the plugin wires the template style to that component.
  8. Understand the Synthetic Shadow Root polyfill

    master
    The Synthetic Shadow Root is a specialized ShadowRoot polyfill designed specifically for Lightning Web Components (LWC) to meet the performance requirements of the Lightning Platform. While it can theoretically be used with other frameworks, it contains specific architectural compromises optimized for LWC performance that may differ from native browser implementations.