Sub topic 1
What a Component Bundle Is
This chapter introduces the Lightning web component bundle: what it is, where it lives in a Salesforce DX project, and which files can appear inside it. Later chapters cover each file in detail.
Prerequisites
This chapter assumes you know the following:
- Basic HTML and JavaScript, including ES modules and classes.
- Basic CSS selectors.
- A general idea of what Salesforce and Lightning Experience are.
- The layout of a Salesforce DX project, or a willingness to learn it as you go.
You do not need to have written a Lightning web component before.
The bundle in one sentence
A Lightning web component (LWC) is a folder of files that share one name. That folder is called a bundle. Each file in the bundle has a defined job: markup, behavior, styling, configuration, an icon, or tests.
Salesforce deploys the whole folder as a single unit. When the component runs, the framework combines the HTML, JavaScript and CSS files into one custom element that you can place on a page or inside another component.
Where a bundle lives
In a Salesforce DX project, component bundles live in the lwc directory of a package directory. By default the package directory is force-app/main/default. Each component gets its own subfolder under lwc.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.css
├── greetingCard.js-meta.xml
├── greetingCard.svg
└── __tests__/
└── greetingCard.test.jsThe folder greetingCard is the bundle. Its name is the component name. Every file inside uses the same base name, so the HTML file is greetingCard.html, the class file is greetingCard.js, and so on.
Your project may use a different package directory, for example when it has several packages. The rule stays the same: the bundle sits in a folder named lwc, and one component folder holds one component.
The files and their jobs
Not every file is required. The table lists each file and what it contributes.
| File | Required | Job |
|---|---|---|
| `greetingCard.html` | Required for most components | Defines the markup the component renders |
| `greetingCard.js` | Yes | Defines the component class, its data and its behavior |
| `greetingCard.js-meta.xml` | Yes | Sets the API version and where the component can be used |
| `greetingCard.css` | No | Styles that apply only to this component |
| `greetingCard.svg` | No | Icon shown for the component in Lightning App Builder |
| `__tests__/` folder | No | Jest unit tests for the component |
| Extra `.js`, `.html`, `.css` files | No | Helper modules, alternate templates, shared styles |
A component with no visible markup can omit the HTML file. A "service" component that only exports functions is a common example. The JavaScript file and the configuration file are the two files every bundle needs.
A minimal working bundle
Here is the smallest useful set of files for a visible component. The HTML file has a single root template element and reads the name property from the class.
<template>
<lightning-card title="Greeting">
<p class="greeting">Hello, {name}!</p>
</lightning-card>
</template>The JavaScript file default-exports a class that extends LightningElement. The @api decorator makes name settable by a parent component or by App Builder.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}The optional CSS file styles only this component's template.
.greeting {
color: #032d60;
font-weight: bold;
}The configuration file sets the API version, exposes the component to Lightning App Builder, lists where it can be placed, and declares an editable property.
<?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>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__AppPage">
<property name="name" type="String" default="World" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>You do not need to understand every line yet. The point for now is how the four files divide the work.
How the pieces connect
The files connect through names and through the class.
- Template and class. The template reads properties and calls methods on the class. In the example,
{name}in the HTML reads thenamefield ofGreetingCard. - Stylesheet and template. The CSS file applies only to the elements in this component's template. A
.greetingrule here does not affect.greetingelements in other components. - Configuration and class. The configuration file declares which public properties App Builder may set. The
nameproperty in the XML matches the@api namefield in the class. - Folder name and markup tag. The folder name becomes the tag used in other components' templates. This is why naming matters, as the next section shows.
Naming rules that affect deployment
The folder name and every file's base name must match. If they do not, the framework cannot link the files into one component. The deploy fails, or the component never resolves in markup.
The folder name also controls the tag you write in other templates. The default namespace is c, and the framework converts the camelCase folder name to kebab-case.
| Folder name | Tag in another template |
|---|---|
| `greetingCard` | `<c-greeting-card>` |
| `loginPanel` | `<c-login-panel>` |
| `accountList` | `<c-account-list>` |
Follow these rules when you name a bundle:
- Start with a lowercase letter.
- Use only alphanumeric characters and underscores.
- Do not include spaces or hyphens.
- Do not end with an underscore.
- Do not contain two consecutive underscores.
- Do not use a name that is a reserved word on the platform.
Camel case, such as greetingCard, is the usual convention because it maps cleanly to the kebab-case tag.
Optional files at a glance
The remaining files extend what a bundle can do. Each gets its own chapter, so only a short description is given here.
- SVG icon. A
greetingCard.svgfile sets the icon that represents the component in Lightning App Builder. - Helper modules. Extra JavaScript files in the bundle can hold shared logic. The component class imports them with a relative path.
- Multiple templates. A bundle can hold several HTML files. The
render()method picks which one to show. - Shared CSS modules. A component whose bundle contains only a CSS file can be imported into other components' CSS files by its module name.
- Jest tests. Files in the
__tests__folder run with Jest and check the component without a Salesforce org.
As a preview, this class chooses between two templates:
import { LightningElement } from 'lwc';
import signedIn from './signedIn.html';
import signedOut from './signedOut.html';
export default class LoginPanel extends LightningElement {
isSignedIn = false;
render() {
return this.isSignedIn ? signedIn : signedOut;
}
}And this CSS file imports a shared stylesheet from a CSS-only component named sharedStyles:
@import 'c/sharedStyles';
.card-body {
padding: 1rem;
}A Jest test sits in the bundle's __tests__ folder. It creates the component, sets a public property, and checks the rendered text:
import { createElement } from 'lwc';
import GreetingCard from 'c/greetingCard';
describe('c-greeting-card', () => {
afterEach(() => {
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('renders the name', () => {
const element = createElement('c-greeting-card', { is: GreetingCard });
element.name = 'Ada';
document.body.appendChild(element);
return Promise.resolve().then(() => {
const p = element.shadowRoot.querySelector('p');
expect(p.textContent).toBe('Hello, Ada!');
});
});
});Common errors
- Mismatched names. The folder is
greetingCardbut a file isgreeting-card.jsorGreetingCard.js. Names are case-sensitive in the bundle, so match them exactly. - Wrong location. The folder sits outside the
lwcdirectory, so the project tooling does not treat it as a component. - Missing configuration file. Without
js-meta.xml, the bundle cannot deploy. - Missing
isExposed. The component deploys and works inside other components, but it does not appear in Lightning App Builder. - Hyphens in the folder name. A name such as
greeting-cardis invalid. UsegreetingCardand write<c-greeting-card>in markup.
Summary
A bundle is a folder under lwc whose name and file base names match. The JavaScript and configuration files are required. The HTML file is needed for any component that renders markup. The CSS file, SVG icon, helper modules, extra templates and tests are optional. The folder name also defines the tag other components use.
Exercise
You create a folder lwc/orderSummary/ and put these files in it: orderSummary.html, order-summary.js, and orderSummary.js-meta.xml. What is wrong, and what tag would you use for the component once it is fixed?
Show answer
The JavaScript file's base name does not match the folder, because order-summary.js should be orderSummary.js. Once all base names match, other templates use the tag <c-order-summary>.
Exercise
A teammate says a component deploys without errors but never shows up in the Lightning App Builder component list. Which file would you check first, and which setting in it?
Show answer
Check js-meta.xml and confirm that isExposed is true and that targets lists at least one App Builder target, such as lightning__AppPage.
Exercise
Which two files must every bundle contain, and which file can a bundle that renders no markup leave out?
Show answer
Every bundle needs the JavaScript file and the js-meta.xml configuration file. A bundle that renders no markup, such as a service component that only exports functions, can omit the HTML file.