Sub topic 1
What a Lightning web component bundle is
A Lightning web component is a folder of files that share one name. Salesforce reads the whole folder as a single unit, called a bundle. This chapter covers what a bundle is, where it lives in a project, and the job of each file. Later chapters cover each file in detail.
Prerequisites
This chapter assumes you already know:
- Basic HTML and JavaScript, including ES modules and classes.
- Basic CSS selectors.
- What Salesforce and Lightning Experience are, in general terms.
- The Salesforce DX project layout, or you are willing to learn it as you go.
The bundle and its location
A bundle is a folder inside the lwc directory of a Salesforce DX project. The default location is force-app/main/default/lwc/. Each component gets one subfolder.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.css
├── greetingCard.js-meta.xml
├── greetingCard.svg
└── __tests__/
└── greetingCard.test.jsThe folder name is the component name. Every file in the folder uses that name as its base name. That is why the folder is greetingCard and the files are greetingCard.html, greetingCard.js and so on.
What each file does
Each file has one job. Together they describe one component.
| File | Job | Required |
|---|---|---|
| `greetingCard.html` | Markup: what the component renders | Yes, unless the component has only a JavaScript module or CSS |
| `greetingCard.js` | Behavior: a class that extends `LightningElement` | Yes |
| `greetingCard.css` | Styles that apply only to this component | No |
| `greetingCard.js-meta.xml` | Configuration: API version, where the component can be used | Yes |
| `greetingCard.svg` | Icon shown in Lightning App Builder | No |
| `__tests__/` | Jest tests | No |
The HTML and JavaScript files are linked by name. The template reads properties from the class, and the class supplies the data and handles events.
A minimal example
This component needs only three files: the template, the class and the configuration. The template shows a value from the class.
<template>
<lightning-card title="Greeting">
<p class="greeting">Hello, {name}!</p>
</lightning-card>
</template>The class supplies name. The @api decorator makes the property settable by a parent component or by App Builder.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}The configuration file tells Salesforce the component exists and where it can be placed.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Greeting Card</masterLabel>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>Other components use this one through the tag <c-greeting-card>. The c is the default namespace, and the camelCase folder name becomes kebab-case in markup.
Common errors at this stage
- Putting the component folder outside the
lwcdirectory, so the project never deploys it. - Renaming the folder without renaming the files inside it.
- Starting a folder name with an uppercase letter. Component folder names begin with a lowercase letter.
- Forgetting
isExposedand then wondering why App Builder does not list the component. Components used only inside other components do not need it.
Exercise
A folder named orderSummary contains orderSummary.html, orderSummary.js and summary.css. Which file breaks the naming rule, and what tag would another component use to include this component?
Show answer
summary.css breaks the rule, because its base name must match the folder name, so it should be orderSummary.css. Other components would use <c-order-summary>.