Sub topic 17
What the Compiler and Framework Add
This chapter assumes you know custom elements, shadow DOM, ES modules, and the LWC bundle layout from earlier chapters. It explains what LWC does on top of the browser standards, and why the source you write is not the code the browser runs.
Why a layer exists
A plain web component, like the HelloGreeting example earlier, works with no tooling. You have to do the following yourself:
- Write
innerHTMLstrings. - Re-render by hand when data changes.
- Wire up event listeners.
LWC removes that work in two places. A compiler runs at build time. A small framework runtime runs in the browser.
What the compiler does
The compiler reads each bundle and produces plain JavaScript modules. Three jobs matter most:
- Templates become render functions. Your
<template>is parsed once. The output is a function that builds virtual DOM nodes. Expressions such as{name}become reads of class fields. - Styles are scoped. The CSS file is rewritten so its rules apply only to that component's markup.
- Decorators and imports are resolved. Syntax such as
@apiis turned into code the engine understands. Imports likelwcare mapped to the runtime.
Because templates are compiled, errors surface early. A missing key in a for:each loop is a build error. It is not a silent runtime bug.
What the runtime does
The engine, loaded in the browser, handles the work that you would otherwise write by hand:
- It defines the custom element for each component. Your class extends
LightningElement, notHTMLElementdirectly. - It attaches the shadow root for you. You never call
attachShadow. - It tracks which fields the template reads. When you reassign one, it schedules a re-render and patches only the changed DOM.
- It wires
onchange={handleChange}to real event listeners and bindsthiscorrectly.
import { LightningElement } from 'lwc';
export default class Greeting extends LightningElement {
name = 'World';
rename() {
// The engine sees this reassignment and re-renders.
this.name = 'LWC';
}
}Compare this with the plain version. There is no attachShadow call and no innerHTML. The template file supplies the markup, and the engine supplies the rendering.
Standard output, framework behavior
The result is still a custom element with a shadow root. Other code can use it like any element. The framework adds convenience on top. It does not replace the standards.
| Concern | Plain web component | LWC |
|---|---|---|
| Registering the element | `customElements.define` | The engine does it |
| Shadow root | `attachShadow` by hand | Created automatically |
| Updating the UI | Manual DOM writes | Reactive re-render |
| Template syntax | Template strings | Compiled `<template>` file |
Exercise
Name two jobs the LWC compiler does at build time, and one job the runtime engine does in the browser.
Show answer
Compiler: it turns templates into render functions, and it scopes CSS to the component. Runtime: it re-renders the component when a class field is reassigned. Creating the shadow root and registering the custom element are also valid runtime answers.