Sub topic 2
Naming the folder and files
This chapter assumes you know basic HTML and JavaScript modules, and that you can find the force-app/main/default/lwc folder in a Salesforce DX project. Naming is the first rule to get right. If a name is wrong, the bundle fails to deploy, or the component deploys and its tag never resolves.
One name, repeated
A bundle is a folder. The folder name is the component name, and every main file inside uses that same name as its base name.
force-app/main/default/lwc/
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
├── greetingCard.css
└── greetingCard.js-meta.xmlThe platform finds each file by combining the folder name with an extension. If greetingCard.js were renamed greeting.js, the platform would no longer find the class for this component.
Match the case exactly. greetingCard and GreetingCard are different names, and the folder uses a lowercase first letter.
Rules for the folder name
- Start with a lowercase letter.
- Use only letters, digits, and underscores.
- Do not use spaces or hyphens.
- Do not end with an underscore.
- Do not use two underscores in a row.
The convention is camelCase: greetingCard, accountList, orderSummary2. Names such as greeting-card, Greeting_Card, and card_ are invalid or break the rules above.
From folder name to markup tag
When one component uses another in its HTML, it does not write the folder name. It writes a tag made from the namespace and the folder name. The namespace is c for components in your own org. The camelCase folder name becomes kebab-case, which means lowercase words joined by hyphens.
A parent template uses the child like this:
<template>
<c-greeting-card name="Ada"></c-greeting-card>
</template>The JavaScript import path uses the folder name unchanged, with c/ in front:
import GreetingCard from 'c/greetingCard';Note the two forms. Markup uses c-greeting-card, and JavaScript uses c/greetingCard. Writing <c-greetingCard> in a template is a common mistake, because HTML tag names must be lowercase.
Names for the other files
The required files follow the pattern <folderName>.<extension>:
| File | Name pattern | Required |
|---|---|---|
| Template | `greetingCard.html` | Yes, unless `render()` returns other templates |
| Class | `greetingCard.js` | Yes |
| Configuration | `greetingCard.js-meta.xml` | Yes |
| Styles | `greetingCard.css` | No |
| Icon | `greetingCard.svg` | No |
| Tests | `__tests__/greetingCard.test.js` | No |
The meta file ends in .js-meta.xml, not .xml. The tests folder is named __tests__ with two underscores on each side. This is the one place the underscore rules are bent, because the folder is a Jest convention and is not a component name. Test files inside it end in .test.js.
Files that serve extra purposes, such as helper modules or additional templates, can have other base names. Those cases come later in this tutorial.
Quick check list
- Is the folder name camelCase, starting lowercase, with no hyphens?
- Does each of
.html,.js,.css, and.js-meta.xmlstart with the folder name? - Does the parent use the kebab-case tag, and does JavaScript use the camelCase import path?
Exercise
A folder is named orderSummary. What tag do you write in a parent template, and what import path do you use in JavaScript? Is order-summary a valid folder name?
Show answer
The tag is <c-order-summary> and the import is c/orderSummary. order-summary is not valid, because folder names cannot contain hyphens.