Sub topic 14
Choosing which files a component needs
This chapter assumes you know what each bundle file does: the HTML template, the JavaScript class, the CSS file, the js-meta.xml configuration file, the SVG icon, helper modules, extra templates, shared CSS modules and the __tests__ folder. Here you decide which of them a new component needs and which it can omit.
Start from the minimum
A component with a template needs two files that share the folder name: an HTML file and a JavaScript file. The JavaScript file default-exports a class that extends LightningElement. The configuration file is also required for deployment, because it sets the API version and controls whether the component is exposed in tools such as Lightning App Builder.
The smallest useful bundle looks like this:
force-app/main/default/lwc/greetingCard/
├── greetingCard.html
├── greetingCard.js
└── greetingCard.js-meta.xmlEverything else is added only when a need appears.
Decide file by file
| File | Add it when | Omit it when |
|---|---|---|
| `.html` | The component renders markup of its own | The bundle is a service module or a CSS-only module |
| `.js` | Always for a component | Never for a component. Only a CSS-only module has no JavaScript |
| `.css` | You need styles scoped to this component | Base Lightning components and your parent's styling are enough |
| `.js-meta.xml` | Always, so the bundle deploys | Never |
| `.svg` | Admins will see the component in App Builder and a custom icon helps them find it | The component is used only inside other components |
| Helper `.js` modules | Logic is large, shared, or easier to test apart from the class | The class stays short and nothing else needs the code |
| Extra `.html` files | One component must show clearly different layouts, chosen by `render()` | A single template with `lwc:if` blocks is enough |
| `__tests__/` | You want automated Jest checks | Rarely. Skip it only for throwaway prototypes |
Work through three cases
Case 1: a child component used only by other components
A child that shows a formatted price needs .html, .js and the configuration file. Leave isExposed as false and omit targets. Skip the SVG icon, because no admin will ever see it in App Builder.
Case 2: a component an admin places on a page
Set isExposed to true and list the places it can go. Add targetConfigs only for properties an admin should edit, and mark each with @api in the class. Add the SVG icon so the component is easy to spot in the component list.
<?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>Case 3: a shared style sheet
If several components need the same rules, create a bundle that holds only sharedStyles.css. Each component then imports it with @import 'c/sharedStyles'; in its own CSS file. This avoids copying rules between bundles.
Common mistakes
- Adding a CSS file that is empty. It does no harm but adds noise, so create it when you write the first rule.
- Creating a second HTML file and forgetting
render(). Without it, only the file matching the folder name is used. - Exposing every component. Each exposed component appears to admins, so expose only those meant for them.
- Naming a helper module like the component. The bundle's main JavaScript file must match the folder name, so give helpers distinct names such as
formatters.js.
Exercise
You are building priceBadge, which only other components use. It needs one colour rule and a shared date-format function that two other bundles also need. Which files should the priceBadge bundle contain, and where does the date function go?
Show answer
Include priceBadge.html, priceBadge.js, priceBadge.js-meta.xml (with isExposed false and no targets) and priceBadge.css for the colour rule. Skip the SVG icon because admins never see the component. Put the date function in a separate service module bundle that has only a JavaScript file, so all three components import it by module name. Add a __tests__ folder if you want Jest checks.