Sub topic 7
Anatomy of a component bundle
This chapter assumes you know basic HTML and CSS, JavaScript classes and ES module syntax, and how custom elements and shadow DOM work from the earlier chapters. It covers what files make up a Lightning Web Component, how the compiler and framework treat each one, and the errors people hit most often when they first create a bundle.
What a bundle is
A Lightning Web Component is not a single file. It is a folder, called a bundle, that holds the files describing one component. The folder name is the component name. The framework and compiler look for files inside the folder by matching the folder name, so naming matters.
For a component called helloGreeting, the bundle looks like this:
helloGreeting/
helloGreeting.html
helloGreeting.js
helloGreeting.css
helloGreeting.js-meta.xml| File | Required | Role |
|---|---|---|
| `helloGreeting.js` | Yes | The component class, written as an ES module |
| `helloGreeting.html` | No | The template that produces the component's DOM |
| `helloGreeting.css` | No | Styles scoped to this component |
| `helloGreeting.js-meta.xml` | Salesforce platform only | Configuration: API version and where the component can be placed |
Only the JavaScript file is strictly required. A component with no template is valid. It is typically a service module that exports functions or constants, or a component that renders nothing of its own.
How the bundle maps to web standards
Each file corresponds to a part of the standards you have already met.
- The JavaScript file is an ES module. Its default export is a class that extends
LightningElement. The compiler turns that class into a custom element, which is how the component gets a tag name. - The HTML file is a template. The compiler converts it into a JavaScript function that builds DOM, rather than shipping it as an HTML
<template>element to be cloned. - The CSS file is rewritten by the compiler so its rules apply only inside the component's shadow root.
- The meta file is not a web standard at all. It is Salesforce platform configuration and is ignored by the open-source LWC compiler.
Naming rules and the tag name
The folder name and file names use camelCase and must match exactly, including case. They must start with a lowercase letter and contain only letters, digits, and underscores. They cannot contain hyphens, spaces, or consecutive underscores, and cannot end with an underscore.
In templates, you refer to a component by a kebab-case tag name made of a namespace, a hyphen, and the component name converted from camelCase to kebab-case. On the Salesforce platform, components in your own org use the namespace c. So helloGreeting becomes <c-hello-greeting>.
This conversion is how LWC satisfies the custom element rule that tag names contain a hyphen. You write camelCase in the file system and kebab-case in markup.
| Folder name | Tag in a template |
|---|---|
| `helloGreeting` | `<c-hello-greeting>` |
| `todoList` | `<c-todo-list>` |
| `counterButton` | `<c-counter-button>` |
The JavaScript file
The class is the heart of the bundle. It must be the default export.
// helloGreeting.js
import { LightningElement } from 'lwc';
export default class HelloGreeting extends LightningElement {
name = 'World';
handleChange(event) {
this.name = event.target.value;
}
}Three points about this file:
lwcis not a package you install from a registry in the usual way for platform use. The framework and platform resolveimport ... from 'lwc'for you. In open-source projects, thelwcpackage provides it.- The class name does not need to match the folder name, but matching it is the convention. The compiler identifies the component by the folder and file names, not by the class name.
- Extending
LightningElementrather thanHTMLElementis what ties the class to the framework.LightningElementis itself a wrapper around the standardHTMLElementbehavior, adding the template rendering and reactivity covered in later chapters.
The HTML template
<!-- helloGreeting.html -->
<template>
<p>Hello, {name}!</p>
<input type="text" value={name} onchange={handleChange} />
</template>The root element must be <template>. Everything the component renders goes inside it. This element is the framework's marker for "this is the content of the component's shadow root". It does not appear in the final DOM.
The template and the class are linked by name. The expression {name} reads the class field name. The handler onchange={handleChange} points to the method handleChange. If a name in the template does not exist on the class, the compiler or the framework reports an error or renders nothing, depending on the case.
A component renders its template when the template file shares its base name with the JavaScript file. You do not import the template manually.
The CSS file
/* helloGreeting.css */
p {
color: rebeccapurple;
}This rule affects only <p> elements inside the component's shadow root. A <p> in a parent component or elsewhere on the page is unaffected, and styles from outside do not reach in. The standards-based reason is shadow DOM encapsulation. Later chapters cover the details of scoping and how to style across boundaries.
The meta file on the Salesforce platform
<?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>The fields shown here do the following:
apiVersionsets the Salesforce API version the component runs against, which can affect platform behavior.isExposedcontrols whether the component can be used outside your own code, for example in a builder tool where an admin drags it onto a page.targetslists the places the component can be placed.lightning__AppPageallows it on an app page.
A component that is only used inside other components needs no exposure. Leaving isExposed as false is a valid and common choice for these internal building blocks.
Putting the bundle together
A parent component uses a child by its tag name. Here is a small parent that includes helloGreeting:
<!-- greetingPage.html -->
<template>
<h1>Welcome</h1>
<c-hello-greeting></c-hello-greeting>
</template>// greetingPage.js
import { LightningElement } from 'lwc';
export default class GreetingPage extends LightningElement {}Notice that greetingPage.js has an empty class body. This is a complete, valid component. The class must exist so the bundle has a custom element to define, even when all of the behavior lives in the template and its children.
The parent does not import helloGreeting. The framework resolves the tag name c-hello-greeting to the bundle by the naming convention above.
Common errors
| Symptom | Likely cause |
|---|---|
| The component does not render at all | The template's root element is not `<template>`, or the file base name does not match the JavaScript file |
| A tag renders as an unknown element | The tag is written in camelCase, or the namespace prefix is missing |
| The compiler reports a missing default export | The class is declared without `export default` |
| A value shows as blank | The template expression names a field that does not exist, or is misspelled |
| Styles leak in or fail to apply | Expectation of global CSS. Rules apply only within this component's shadow root |
| The component cannot be placed in a builder | `isExposed` is `false`, or `targets` does not include the location |
What to remember
- The bundle is a folder. Its name defines the component.
- The JavaScript file is required and exports the class. HTML and CSS are optional. The meta file is a Salesforce platform concern.
- The compiler turns the class into a custom element, the template into a render function, and the CSS into shadow-scoped rules.
- You write camelCase in files and kebab-case in tags.
Exercise
You create a folder named order_Summary with files order_Summary.js and order_Summary.html. You also want to use it in a parent as <c-order_summary>. List the problems with the names, and give a corrected folder name and the tag you would write in the parent template.
Show answer
There are two problems. First, the name mixes an underscore with a capital letter, while LWC names should be camelCase beginning with a lowercase letter and without that kind of separator. Second, tag names in templates are kebab-case, so <c-order_summary> is not the right form. A corrected folder and file base name is orderSummary (files orderSummary.js and orderSummary.html), and the parent uses <c-order-summary></c-order-summary>.
Exercise
A bundle contains weatherCard.js and weatherCard.html. The template starts with <div> instead of <template>. What happens, and what is the fix? Also, which file in the bundle is not part of any web standard?
Show answer
The template is invalid because the root element must be <template>, so the component will not compile or render as intended. The fix is to wrap the markup in a root <template> element, and put the <div> inside it. The file that is not part of any web standard is weatherCard.js-meta.xml, which is Salesforce platform configuration.