Sub topic 1
What a Component Bundle Is
Prerequisites
You should know basic HTML, JavaScript classes, ES modules, and CSS selectors. You should also recognize a Salesforce DX project and the force-app/main/default directory.
What a Component Bundle Is
A Lightning web component (LWC) bundle is a folder containing the files for one reusable user-interface component. The folder normally lives under:
force-app/main/default/lwc/Each file has a specific role. The HTML file defines markup, the JavaScript file controls behavior, and optional files provide styles, tests, icons, or configuration. Salesforce treats the folder as one deployable component.
Naming the Folder and Files
The folder name uses camel case and becomes part of the component's local name. The main files use the same base name:
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
└── greetingCard.js-meta.xmlThe HTML and JavaScript base names must match the folder name. In markup, the component name becomes a kebab-case tag:
<c-greeting-card></c-greeting-card>A mismatch, such as GreetingCard.js or greeting-card.html, prevents Salesforce from resolving the bundle correctly.
The HTML Template
The HTML file must contain one root <template> element. It describes what the component renders. Expressions in braces read properties from the JavaScript class.
<template>
<lightning-card title="Greeting">
<p>Hello, {name}!</p>
</lightning-card>
</template>{name} is a template expression. During rendering, LWC reads the name property from the component instance.
The JavaScript Class
The JavaScript file exports a default class that extends LightningElement, the base class for LWC components.
import { LightningElement } from 'lwc';
export default class GreetingCard extends LightningElement {
name = 'World';
}The class supplies state and behavior. The template reads name, so it initially displays Hello, World!. When tracked component state changes, LWC can render the affected template content again.
Exercise
Which file should you change to alter the text produced by {name}?
Show answer
Change the JavaScript class, because it owns the name property. Change the HTML file only when the markup or expression itself must change.