Sub topic 7
Anatomy of a component bundle
This chapter assumes you know basic HTML and CSS, JavaScript classes and ES module imports, and the idea of a custom element from earlier chapters. It covers the files that make up one Lightning Web Component and how they fit together.
One folder, one component
An LWC component is a folder called a bundle. The folder name is the component name, written in camelCase. Every file inside uses the same name as its base.
helloGreeting/
helloGreeting.html
helloGreeting.js
helloGreeting.css
helloGreeting.js-meta.xmlThe compiler finds the files by this naming convention, so a mismatched name breaks the component. Other components refer to it by a tag name in kebab-case with a namespace prefix. On Salesforce the default namespace is c, so helloGreeting becomes <c-hello-greeting>.
The files
| File | Required | Purpose |
|---|---|---|
| `.js` | Yes | An ES module whose default export is a class extending `LightningElement`. |
| `.html` | No | The template, wrapped in a root `<template>` element. |
| `.css` | No | Styles that apply only inside this component's shadow root. |
| `.js-meta.xml` | On Salesforce | Sets the API version and where the component can be used. |
A component with no template has no UI of its own. It can still be useful as a service module that other components use.
The JavaScript class
The class is the component's behavior. The compiler turns the module into a custom element and gives it a shadow root. The template renders into that root.
// helloGreeting.js
import { LightningElement } from 'lwc';
export default class HelloGreeting extends LightningElement {
name = 'World';
}The export must be default. Without it, the framework has no class to register for the folder name.
The template and the stylesheet
<!-- helloGreeting.html -->
<template>
<p class="greeting">Hello, {name}!</p>
</template>/* helloGreeting.css */
.greeting {
color: #0b5cab;
}The .greeting rule cannot leak out to the rest of the page. Page styles also cannot reach into this template, except for inherited properties such as color and font-family. Shadow DOM provides this scoping.
The configuration file
The .js-meta.xml file is a Salesforce addition. Browsers ignore it, and the open-source LWC project does not use it. It tells the platform how to deploy the component.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
</targets>
</LightningComponentBundle>isExposed makes the component available in tools such as the App Builder. Each target names a page type where it may be placed.
Common errors
- Renaming one file but not the others, which leaves the bundle without a matching class or template.
- Using
export classinstead ofexport default class. - Writing the tag as
<c-helloGreeting>. HTML tag names are lowercase, so camelCase must become kebab-case. - Putting markup outside the root
<template>element.
Exercise
A bundle folder is named orderSummary. What is the tag name another component uses to include it, and which file must it contain at minimum?
Show answer
The tag is <c-order-summary>. At minimum the bundle needs orderSummary.js, whose default export is a class extending LightningElement. On Salesforce you also add orderSummary.js-meta.xml to deploy it.