Your setup and the course project
A simple web components setup with no build step: module scripts, a dev server with live reload, import maps for packages, and DevTools for shadow roots.
- 01Start a project with no buildMake a folder with an HTML page and a kit of module files, and serve it with a dev server that reloads when you save.
- 02Load packages by nameGive a bare name like
"lit"an address with an import map, or let the dev server resolve it. - 03Look inside your componentsFind shadow roots, badges, and the class behind a tag in DevTools, and know how the course's tea shop UI kit is laid out.
How do you set up a web components project?
Ravi looks after the websites of Leaf and Kettle, the tea shop from the last lesson. He has made up his mind. The star rating will be a web component. So will the shop's labels, cards, and prices. He opens his laptop and an empty folder. Where does he start?
Other tools he has tried came with a build step, a pile of config files, and a long install. A build step is a tool that turns your files into other files before the browser sees them. Does a web component need one?
No. A web components setup needs no build step. You need an HTML page that loads your components with a module script. You need a small dev server that sends the page and reloads it when you save. And you need DevTools to look inside your elements.
Ravi starts with step zero. Before he writes any component, he puts the shop's header on a page:
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Leaf and Kettle</title> <script type="module" src="tea-kit/index.js"></script> </head> <body> <tea-shop-header>Leaf and Kettle</tea-shop-header> </body></html>- Lines 1 to 5 are the start of every HTML page: the doctype, the language, the text's encoding, and the title.
- Line 6 loads
tea-kit/index.jsas a module: a JavaScript file that can load other files. It's the front door of the kit Ravi will build. For now, it holds one line. - Line 9 uses
<tea-shop-header>. Nothing defines it yet. - In the
tea-kit/index.jstab, line 1 printsThe tea kit loaded.
The result shows Leaf and Kettle, and the console prints The tea kit loaded. The browser keeps a tag it doesn't know and shows the text inside it, as lesson 1.1 showed. Step zero proves two things: the page loads, and the script runs. Everything else grows from these two files.
The picture shows the whole setup. The rest of this lesson takes each part in turn.
Some drinks go to a plant first. They are mixed and bottled before anyone can drink them. Tea at Leaf and Kettle is different. You pour it straight from the pot. If it's too strong, you add water and pour again.
- In real life: The pot of tea
- In JavaScript: Your HTML and JavaScript files
- In real life: Pouring a cup
- In JavaScript: The browser loading the files as they are
- In real life: A bottling plant
- In JavaScript: A build step that turns files into other files first
- In real life: Adding water and pouring again
- In JavaScript: Saving a file and seeing the page reload
A web components setup is a folder, a page, and a dev server. The page loads your components with a module script. The dev server sends the files and reloads the page when you save. DevTools lets you look inside. Build tools are optional.
Before you start, have these ready:
- a code editor, such as VS Code;
- Node.js 22 or newer, which comes with npm (the dev server needs it);
- Chrome, for the DevTools steps in this lesson.
How does a module script load your components?
A module is a JavaScript file that can use import and export. You tell the browser a script is a module with type="module". The JavaScript path covers import and export in full. For components, three things matter.
It waits for the page
Look at step zero again. Line 6 loads the kit in the <head>, above every tag in the page. That's fine. A module script waits until the browser has read the whole page, so it never runs too early. You don't need the defer attribute.
One module loads the others
A kit has many components. A page shouldn't need a script tag for each one. So one module imports the rest:
<p><tea-label name="Green tea" minutes="3"></tea-label></p><p><tea-price amount="4"></tea-price></p> <script type="module" src="tea-kit/index.js"></script>- Lines 1 and 2 use two tags.
- Line 4 loads only
tea-kit/index.js. - In the
tea-kit/index.jstab, line 1 imports./tea-label.jsand line 2 imports./tea-price.js. Each import runs that file../means "in the same folder as this file". - Each component file ends with
customElements.define(), so it registers its own tag.
The result shows Green tea: brew for 3 minutes and Price: 4. Both classes are the ones from lesson 1.1, each now in its own file.
Each module runs once
What if two places load the same file? Switch between the two choices and watch for an error:
<tea-label name="Green tea" minutes="3"></tea-label> <script src="tea-kit/tea-label.js"></script><script src="tea-kit/tea-label.js"></script>Each classic script runs the file again, and all classic scripts share one global scope. The first copy makes TeaLabel and registers the tag. The second copy tries to make TeaLabel again, and the browser stops it with a SyntaxError.
With classic scripts (no type), the file runs twice. All classic scripts share one global scope. So the second copy can't make TeaLabel again, and Chrome stops it: SyntaxError: Identifier 'TeaLabel' has already been declared.
Modules don't have that problem. Each module has its own scope, so its names never clash with other files. And the browser runs each module once per page, however many times it's loaded.
Two staff members at Leaf and Kettle each ask for the tasting poster to go up. The first request gets it pinned. For the second, someone checks the board first. The poster is already there, so it isn't pinned again.
- In real life: The notice board
- In JavaScript: The page
- In real life: The tasting poster
- In JavaScript: The file
tea-kit/tea-label.js - In real life: A second person asking for the same poster
- In JavaScript: A second import of the same module
- In real life: Checking the board before pinning
- In JavaScript: The browser running each module once
Why it matters: any page or component can import the files it needs. It never has to worry about who loaded them first.
Why do you need a dev server?
Ravi saves step zero in his folder: the page as index.html, and its script as tea-kit/index.js. Then he double-clicks index.html. The browser opens it from the disk, with an address that starts with file://. The header shows. But the console never says The tea kit loaded. Instead, it shows a red error:
Access to script at 'file:///…/leaf-and-kettle/tea-kit/index.js'
from origin 'null' has been blocked by CORS policyLine 1 names the file Chrome refused to load. Line 2 says why: a safety rule called CORS blocked it.
Browsers load every module script under CORS. The rule decides which addresses may load files from which others. A page opened from the disk has no real address of its own. Chrome calls its origin null, so the rule blocks the module. MDN gives the fix: serve the folder from a small web server, and open the page at an address like http://localhost:8000/.
A dev server is that small web server, made for working on a site. Ravi uses Web Dev Server, the one Lit's docs recommend. Open a terminal and run these commands, one at a time:
mkdir leaf-and-kettle
cd leaf-and-kettle
npm init -y
npm install --save-dev @web/dev-server
npx wds --watch --open- Line 1 makes the project's folder,
leaf-and-kettle. Line 2 moves into it. - Line 3 makes
package.json, the file where npm lists the project's tools. - Line 4 installs Web Dev Server into the folder.
--save-devlists it as a tool for working on the site, not as part of the site. - Line 5 starts it.
wdsis its short name.--watchreloads the page when a file changes.--openopens the page in your browser.
Put step zero's two files in the folder before line 5. Keep index.html a whole page, with <html> and <body>: the dev server only adds its reload script to whole pages. When it starts, it prints where to find your page:
Web Dev Server started...
Root dir: /Users/ravi/leaf-and-kettle
Local: http://localhost:8000/
Network: http://192.168.1.20:8000/- Line 1 says it started.
- Line 3 is the folder it serves. Yours shows your own path.
- Line 4 is the address to open.
localhostmeans "this computer". If port 8000 is busy, the server picks the next free one, so read the address it prints. - Line 5 is the same server at your computer's address on your local network.
Now the page loads from http://localhost:8000/, and the console prints The tea kit loaded. The CORS error is gone.
Live reload
Change the message in tea-kit/index.js and save. Don't touch the browser. The page reloads by itself and prints the new message. This is live reload.
At Leaf and Kettle, a small hatch joins the kitchen to the counter, with a bell beside it. When a fresh pot is ready, the cook rings the bell. Someone at the counter hears it and brings the new pot out. Nobody keeps walking back to check.
- In real life: The hatch, always open
- In JavaScript: The connection the page keeps open to the dev server
- In real life: A fresh pot of tea
- In JavaScript: A file you just saved
- In real life: The cook ringing the bell
- In JavaScript: The dev server telling the page a file changed
- In real life: Bringing the new pot to the table
- In JavaScript: The page reloading
How does it work? With --watch, the dev server adds two lines to the end of every whole page it sends:
<tea-shop-header>Leaf and Kettle</tea-shop-header>
<!-- injected by web-dev-server -->
<script type="module" src="/__web-dev-server__web-socket.js"></script></body>
</html>Line 1 is your own tag. Line 2 is a comment that marks what the server added. Line 3 loads the server's own script. That script opens a WebSocket: a connection that stays open, so the server can send messages at any time. When you save a file, the server sends one message, and the page reloads.
Why it matters: you never press refresh. And every reload starts a fresh page, so each customElements.define() runs on an empty list of tags. Chrome may also print a message about unload in the console. It comes from the dev server's script, not from your code.
A start script
You don't have to remember the flags. Save them in package.json as a script:
{
"name": "leaf-and-kettle",
"version": "1.0.0",
"scripts": {
"start": "wds --watch --open"
},
"devDependencies": {
"@web/dev-server": "^1.0.1"
}
}Line 5 adds a script called start. Now npm start runs wds --watch --open. Line 8 is the dev server npm installed. ^1.0.1 means "this version, or a newer one with the same first number".
How do you use import maps for packages?
Later, the kit will use packages from npm, like the Lit library in stage 8. Lit's docs tell you to write imports like this one:
import { LitElement } from "lit";Line 1 asks for LitElement from the package lit. The text after from is the module specifier. Here it's "lit", a bare name: it doesn't start with /, ./, or ../, and it isn't a full address. Put this line in a module on a plain server, and Chrome stops with this error:
TypeError: Failed to resolve module specifier "lit". Relative references must start with either "/", "./", or "../".
The browser has to turn every specifier into an address, and it can't guess where lit lives. The JavaScript lesson on module resolution and import maps explains the rules. There are two ways out: tell the browser where the name lives, or let the dev server rewrite the name.
An import map gives a name an address
An import map is a small piece of JSON in a <script type="importmap"> tag. It tells the browser which address each name stands for. Here, the kit gets a name of its own:
<script type="importmap"> { "imports": { "tea-kit": "./tea-kit/index.js" } }</script> <p><tea-label name="Green tea" minutes="3"></tea-label></p><p><tea-price amount="4"></tea-price></p> <script type="module"> import "tea-kit";</script>- Lines 1 to 7 are the import map.
- Line 4 says: the name
tea-kitmeans the file./tea-kit/index.js. - Lines 9 and 10 use two tags.
- Line 13 imports
"tea-kit", a bare name. The map turns it into./tea-kit/index.js.
The result shows Green tea: brew for 3 minutes and Price: 4. Now every page can load the kit with the same short line.
Ravi doesn't type the tea supplier's number each time he calls. He saved it once, under "Supplier". He taps the name, and the phone dials the number. If the supplier gets a new number, he changes one contact, and every call still reaches them.
- In real life: Your phone's contacts
- In JavaScript: The import map
- In real life: A saved name, like Supplier
- In JavaScript: A bare name, like
"tea-kit" - In real life: The full phone number
- In JavaScript: The file's address,
./tea-kit/index.js - In real life: Changing one contact
- In JavaScript: Changing one line of the map, and every import follows
A name for a folder
A key that ends in / names a whole folder. The JavaScript lesson calls it a prefix key. Switch between the two maps, and watch the price:
<script type="importmap"> { "imports": { "tea-kit": "./tea-kit/index.js" } }</script> <p><tea-label name="Green tea" minutes="3">Green tea</tea-label></p><p><tea-price amount="4">(price not loaded)</tea-price></p> <script type="module"> import "tea-kit";</script>Line 4 names one file, tea-kit/index.js. Line 13 imports that name, so the whole kit loads: the label and the price.
Why it matters: a folder key lets a page load just one component, like tea-kit/tea-card.js, when it doesn't need the whole kit.
A package from a CDN
A package works the same way. A CDN is a server that hosts files for many sites. esm.sh is one that sends npm packages as modules, with no build step. This map gives lit an address there:
{
"imports": {
"lit": "https://esm.sh/lit@3.3.3"
}
}Line 3 maps the name lit to Lit 3.3.3 on esm.sh. The version is part of the address, so the page always gets the same Lit. One line is enough, because esm.sh rewrites Lit's own imports into addresses on esm.sh.
A package from node_modules, with the dev server
You can also install a package with npm: npm install lit. It lands in node_modules/lit/. Could the map point there? It could, but one line isn't enough. Lit's own entry file imports three more packages by bare names: @lit/reactive-element, lit-html, and lit-element. Each one needs map lines of its own.
This is where the dev server helps. Start it with --node-resolve, and it rewrites bare names in each file it sends:
// What you write, in a file in tea-kit/
import { LitElement } from "lit";
// What the dev server sends with --node-resolve
import { LitElement } from "./../node_modules/lit/index.js";Line 2 is what you write. Line 5 is what the browser gets: a path into node_modules. The server does the same inside Lit's own files. Vite, another popular dev server, rewrites bare imports too.
Read each row across: the question, then the answer for each way.
| What you want to know | With an import map | With --node-resolve |
|---|---|---|
| Where does the name get its address? | In the browser, from the map in your page. | On the dev server, before the file reaches the browser. |
| What do you add? | A <script type="importmap"> above your module scripts. | The --node-resolve flag, and the package in node_modules. |
| Does it work on any web server? | Yes. The map is part of the page. | No. It works only while this dev server sends the files. |
| How many lines does a package need? | One for each bare name the package imports, or one line to a CDN like esm.sh. | None. The server finds every name. |
The course's kit uses an import map. It works on any web server, with no extra step. Three rules keep a map working:
- Put the map above every module script that imports a name from it.
- It changes names in
importandimport()only, never a script'ssrc. - A key that ends in
/needs an address that ends in/too.
What will you build in this course?
Leaf and Kettle has three sites, as the last lesson showed: Asha's React shop, a Vue staff page, and a plain HTML blog. All three need the same labels, cards, and prices. So Ravi keeps them in one UI kit: a set of components in one folder that every site loads. This course builds that kit with you, one lesson at a time. Here is its folder:
leaf-and-kettle/
index.html the demo page, with the import map
package.json made by npm init; lists the dev server
node_modules/ made by npm install; never edit it
tea-kit/
index.js loads every component
tea-card.js <tea-card>
tea-label.js <tea-label>
tea-price.js <tea-price>- Line 2 is the demo page. It shows every component, so you can see them while you work.
- Lines 3 and 4 come from npm. You never edit
node_modules/. - Line 6,
tea-kit/index.js, loads every component. A page that wants the whole kit loads this one file. - Lines 7 to 9 hold one component each. Each file is named after its tag.
The kit follows five rules:
- Every tag starts with
tea-, so the kit's tags stay apart from other libraries' tags. - One tag per file, and the file has the tag's name.
- Each file registers its own tag with
customElements.define(). tea-kit/index.jsimports every file.- Pages load the kit through the import map, by the name
"tea-kit".
tea-kit/index.js imports the three component files, one file per tag.A good kitchen keeps its spices in a rack. Each spice has its own jar, with its name on the front. A cook can take one jar, or carry the whole rack to the stove.
- In real life: The rack
- In JavaScript: The
tea-kit/folder - In real life: One jar, with the spice's name on it
- In JavaScript: One file, named after its tag
- In real life: Carrying the whole rack to the stove
- In JavaScript: Importing
tea-kit, which loads every file - In real life: Taking one jar
- In JavaScript: Importing one file, like
tea-kit/tea-card.js
Here is the demo page with all its files. Each tab is a file to copy into your folder:
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Leaf and Kettle UI kit</title> <script type="importmap"> { "imports": { "tea-kit": "./tea-kit/index.js", "tea-kit/": "./tea-kit/" } } </script> <script type="module"> import "tea-kit"; </script> </head> <body> <tea-shop-header name="Leaf and Kettle">Leaf and Kettle</tea-shop-header> <tea-card> <tea-label name="Green tea" minutes="3"></tea-label> </tea-card> <p><tea-price amount="4"></tea-price></p> </body></html>- Lines 6 to 13 are the import map. Line 9 names the whole kit. Line 10 names its folder, so a page can load one component on its own.
- Lines 14 to 16 load the whole kit by its name.
- Line 19 is the header from step zero. Nothing defines it yet, so its text shows. Its
nameattribute is for the class you write in exercise 4. - Lines 20 to 22 put a label inside a card. Line 23 shows a price.
- In the tabs,
tea-kit/index.jsimports the three component files.tea-label.jsandtea-price.jsare the classes from lesson 1.1. Intea-card.js, line 4 gives the card a shadow root, a private tree of its own. Line 5 puts a heading and a<slot>in it.
The result shows Leaf and Kettle, Today's tea, Green tea: brew for 3 minutes, and Price: 4. The label appears inside the card, where the slot is. These files are first drafts. Lesson 3.1 rebuilds them properly.
What each stage adds to the kit
Read each row across: the stage, then what its lessons add to the kit.
| Which stage | What it adds to the kit |
|---|---|
| Stage 2: Custom elements | The kit's first real components, in lesson 3.1. Then attributes (lesson 3.4) and events (lesson 5.3) for each one. |
| Stage 3: Shadow DOM, templates, and slots | Shadow roots (lesson 6.1) and slots (lesson 8.1) inside the components, and a tabs component (lessons 10.1 and 10.2). |
| Stage 4: Styling web components | Styles, and a styling API of custom properties and parts (lessons 12.1 and 12.2). |
| Stage 5: Accessible components | Accessible tabs, an accordion, and dialogs (lessons 14.3 to 14.5). |
| Stage 6: Forms | Form controls: a switch, a button, and an input (lessons 17.1 to 17.3). |
| Stage 7: Building components well | A base class that every component shares (lesson 19.1). |
| Stage 11: Testing, tooling, and publishing | Tests (lesson 27.1), a manifest that describes each component (lesson 28.2), and a package on npm (lesson 29.1). |
| Stage 12: Design systems | Design tokens and themes (lessons 30.3 and 30.4). |
| Stage 16: Projects | Guided projects: the finished UI kit (module 43), more form controls (module 44), and a tea shop app built with it (module 45). |
By the end, the kit is a tested package on npm, and a tea shop app is built with it. Every stage keeps the same folder and the same rules.
How do you inspect shadow roots in DevTools?
Open the demo page and you see text. Open DevTools and you see the parts. DevTools is the set of tools built into Chrome for looking inside a page.
From the front, a watch shows only the time. Some watches have a glass back. Turn one over, and you see the gears moving inside. Looking doesn't change how the watch works.
- In real life: The watch's face
- In JavaScript: The page you see
- In real life: The glass back
- In JavaScript: DevTools' Elements panel
- In real life: The gears inside
- In JavaScript: Your tags, their shadow roots, and their slots
Open your own demo page from the dev server, then:
- Right-click
Green teaand choose Inspect. DevTools opens the Elements panel with that tag selected. The shortcut is Cmd+Option+C on a Mac, or Ctrl+Shift+C on Windows and Linux. - Find
<tea-card>and open it with the small arrow next to it. - Open
#shadow-root (open)just under it.
Here is what the Elements panel shows for the header and the card:
<tea-shop-header name="Leaf and Kettle">Leaf and Kettle</tea-shop-header>
<tea-card>
#shadow-root (open)
<p>Today's tea</p>
<slot></slot>
<tea-label name="Green tea" minutes="3">Green tea: brew for 3 minutes</tea-label>
</tea-card>- Line 3 is the card's shadow root.
(open)is its mode, fromattachShadow({ mode: "open" }). - Lines 4 and 5 are inside the shadow root: the heading and the slot.
- Line 6 is the label. It still lives under
<tea-card>, outside the shadow root. The slot only shows it in its place.
Your own shadow roots always show up. Only the browser's own ones need the Show user agent shadow DOM setting, as lesson 1.1 showed.
DevTools also adds small labels called badges next to some tags:
custom-elementsits next to every tag with a hyphen in its name. Click it, and DevTools opens the tag's class in the Sources panel.<tea-shop-header>gets one too, because DevTools only looks at the name. It has no class to open yet.slotsits next to<tea-label>, because the card shows it in a slot. Click it to jump to that<slot>. There, arevealbadge jumps back.
Now ask the console. Select <tea-card> in the Elements panel, open the Console tab, and type these lines one at a time:
$0.localName
$0.constructor.name
$0.shadowRoot.innerHTML$0is the element you selected last in the Elements panel.- Line 1 gives back
tea-card, the tag's name. - Line 2 gives back
TeaCard, the class behind the tag. - Line 3 gives back
<p>Today's tea</p><slot></slot>, the HTML inside its shadow root.
Why it matters: when a component shows the wrong thing, these are the first places to look. Is the tag defined? Which class is behind it? What's in its shadow root? Lesson 10.3, Debugging web components, goes further, with breakpoints.
Which file does each piece go in?
A setup stays easy when every piece has one home. Sort each piece into the file where it belongs. Then check your answers.
- The import map
<tea-card>with a<tea-label>insideimport "tea-kit";in a module scriptimport "./tea-card.js";class TeaCard extends HTMLElementcustomElements.define("tea-card", TeaCard);"start": "wds --watch --open""@web/dev-server": "^1.0.1"
Put each piece in the file where it belongs in the course project.
With every piece in its place, a working session has four steps:
- Run
npm start. The demo page opens. - Change a file in
tea-kit/and save it. - Look at the page. It has already reloaded.
- If something looks wrong, open DevTools. Is the tag defined? Which class is it? What's in its shadow root?
Common mix-ups
- "Web components need a build step." No. Browsers run module files as they are. The dev server only sends the files and reloads the page.
- "You can open index.html straight from the disk." No. Module scripts don't load on a
file://page. Serve the folder with a dev server. - "An import map also works for a script's src." No. It only changes names in
importandimport().<script type="module" src="tea-kit">asks the server for a file calledtea-kit. - "One import map line is enough for any npm package." No. A package can import other packages by bare names. Map each one, use a CDN like esm.sh, or let the dev server resolve them.
- "Loading a module twice runs it twice." No. A module runs once per page. Classic scripts are the ones that run again.
- "The custom-element badge means the tag is defined." No. DevTools adds it to every tag with a hyphen. Click it to find out whether a class is behind it.
- "You need the webcomponentsjs polyfills." No. Every current browser has custom elements and shadow DOM, as lesson 1.2 showed.
Practice exercises
Answer first, then use the hints. The results run live, so you can check yourself.
tea-kit/index.js imports ./tea-label.js, and tea-kit/tea-label.js prints tea-label.js ran. How many times does the console print it? Decide first, then check the result.
<script type="module" src="tea-kit/index.js"></script><script type="module" src="tea-kit/tea-label.js"></script>Once. Both paths lead to the same module, and the browser runs each module once per page. The console prints tea-label.js ran one time.
Which file does line 12 load? Write its path from the page.
<script type="importmap"> { "imports": { "tea-kit/": "./tea-kit/" } }</script> <p><tea-price amount="4"></tea-price></p> <script type="module"> import "tea-kit/tea-price.js";</script>tea-kit/tea-price.js, in the tea-kit folder next to the page. The key tea-kit/ matches, and the browser adds the rest of the name to ./tea-kit/. The result shows Price: 4.
The price never shows. Which address does the browser ask for on line 11?
<script type="importmap"> { "imports": { "tea-kit": "./tea-kit/index.js" } }</script> <p><tea-price amount="4"></tea-price></p> <script type="module" src="tea-kit"></script><script type="importmap"> { "imports": { "tea-kit": "./tea-kit/index.js" } }</script> <p><tea-price amount="4"></tea-price></p> <script type="module"> import "tea-kit";</script>The browser asks for /tea-kit. No file has that address, so nothing loads and the price never shows. An import map never changes a src. Import the name inside a module script instead.
Make the header show Welcome to Leaf and Kettle, using its name attribute. Write the class in the tea-kit/tea-shop-header.js tab, and import it in tea-kit/index.js. What does your page show?
<tea-shop-header name="Leaf and Kettle">Leaf and Kettle</tea-shop-header> <script type="module" src="tea-kit/index.js"></script><tea-shop-header name="Leaf and Kettle">Leaf and Kettle</tea-shop-header> <script type="module" src="tea-kit/index.js"></script>See the tea-kit/tea-shop-header.js tab. The class reads name when the tag joins the page and shows Welcome to Leaf and Kettle. The new import in the tea-kit/index.js tab makes the kit load it, like every other component.
On your demo page, select <tea-price> in the Elements panel. In the console, type $0.shadowRoot. What does it give back?
null. TeaPrice writes its text straight into the tag and never calls attachShadow(). In the Elements panel, <tea-price> has no #shadow-root line.
MDN keeps small web components examples on GitHub. Serve the life-cycle-callbacks example with these commands. Click Add custom-square to DOM, and inspect the square. Which two elements are inside its shadow root?
cd ..
git clone https://github.com/mdn/web-components-examples.git
cd leaf-and-kettle
npx wds --root-dir ../web-components-examples/life-cycle-callbacks --watch --openA style element and a div. The class makes both in its constructor. The style gives the div its size and color from the tag's attributes.
Check your understanding
For each question, think about the three parts: the page and its module script, the dev server, and the import map.
Question 1 of 7Ravi double-clicks index.html. The header shows, but the kit's module never runs. Why?
Choose an answer to see the explanation.
Question 2 of 7What does the dev server's
--watchflag do?Choose an answer to see the explanation.
Question 3 of 7What happens when this page with two classic script tags runs?
Read the code, then predict<tea-label name="Green tea" minutes="3"></tea-label> <script src="tea-kit/tea-label.js"></script> <script src="tea-kit/tea-label.js"></script>Choose an answer to see the explanation.
Question 4 of 7A page has no import map, and its server doesn't rewrite anything. Which import works?
Choose an answer to see the explanation.
Question 5 of 7Where must the import map go in the page?
Choose an answer to see the explanation.
Question 6 of 7In the Elements panel,
<tea-shop-header>has acustom-elementbadge. What does that tell you?Choose an answer to see the explanation.
Question 7 of 7Which lesson builds the first real version of the tea shop UI kit?
Choose an answer to see the explanation.
Key takeaways
- A web components setup needs no build step: a folder, a whole HTML page, and a dev server.
type="module"lets one file import the others. A module waits for the page and runs once.- Module scripts don't load on
file://pages. A dev server serves the folder, and--watchreloads the page when you save. - A bare name like
"lit"needs an import map, or a dev server with--node-resolve. - DevTools shows
#shadow-root (open), thecustom-elementandslotbadges, and$0for the selected tag. - The course project is a tea shop UI kit:
tea-kit/, one file per tag, andindex.jsto load them all.
Remember the one-liner.
A folder, a page with a module script, and a dev server: that's the whole setup. Add an import map for packages, and open DevTools to look inside.
Sources
The facts on this page come from these docs and specifications:
- Using modules on the web, MDN.
- Web Dev Server, Modern Web.
- Dev Server: CLI and configuration, Modern Web.
- Development: local dev servers, Lit.
- <script type="importmap">, MDN.
- HTML Standard: Import maps, WHATWG.
- esm.sh: a no-build JavaScript CDN, esm.sh.
- Features: npm dependency resolving, Vite.
- Open Chrome DevTools, Chrome for Developers.
- Badges reference, Chrome for Developers.
- Preferences: Show user agent shadow DOM, Chrome for Developers.
- Console utilities API reference, Chrome for Developers.
- web-components-examples, MDN.
Coming next: Using a component library. Add ready-made components to a page from a CDN or npm, and read their docs.