Sub topic 15
Beyond JSON: Other Module Types
This chapter assumes you already know the with { type: "json" } syntax, why the type attribute exists, and how a JSON module exposes its data as a default export. Here you will see that the same mechanism is built to support more than JSON, and that support for each extra type depends on the host.
The Type Attribute Is a General Mechanism
The type attribute tells the host which kind of resource to expect. The host is the environment that runs the code, such as a browser or Node.js. The language itself only defines how attributes are written and passed along. Each host decides which type values it accepts and how it parses each one.
CSS Module Scripts
Browsers based on Chromium support CSS module scripts. Importing a stylesheet with type: "css" gives you a constructable CSSStyleSheet object as the default export. You can attach it to a document or to a shadow root.
/* card.css */
.card {
border: 1px solid #ccc;
padding: 1rem;
border-radius: 8px;
}// card.js
import sheet from "./card.css" with { type: "css" };
class InfoCard extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({ mode: "open" });
root.adoptedStyleSheets = [sheet];
root.innerHTML = `<div class="card"><slot></slot></div>`;
}
}
customElements.define("info-card", InfoCard);Several rules you know from JSON modules apply here too:
- The server must send the file with the
text/cssMIME type. A mismatch fails the import. - The module is evaluated once, so every importer gets the same
CSSStyleSheetobject. If you edit it withsheet.replaceSync()orinsertRule(), every shadow root that adopted it updates. - The only export is the default export. Named imports fail.
@importrules are not allowed inside a CSS module script. Constructed stylesheets ignore them.
Comparing Module Types
| Type value | Default export | Where it works | Status |
|---|---|---|---|
| `"json"` | Parsed JSON value | Modern browsers, Node.js, Deno, Bun | Standardized |
| `"css"` | `CSSStyleSheet` | Chromium-based browsers | Shipped in some engines |
| `"text"`, `"bytes"` | String or byte data | Some runtimes and bundlers | Proposals, not yet standard |
| Other values | None | Nowhere by default | Rejected with a `TypeError` |
WebAssembly integration for ES modules is also being worked on. It uses a different syntax (import source) rather than a type value.
Detecting Support at Runtime
A static import of an unsupported type breaks the whole module graph. When support is uncertain, use dynamic import() and fall back if it fails:
async function loadSheet(url) {
try {
const mod = await import(url, { with: { type: "css" } });
return mod.default;
} catch {
const sheet = new CSSStyleSheet();
const text = await (await fetch(url)).text();
sheet.replaceSync(text);
return sheet;
}
}
document.adoptedStyleSheets = [await loadSheet("./card.css")];The fallback builds the same kind of object by hand, so the code that calls loadSheet works the same either way.
Exercise
Two web components import "./theme.css" with { type: "css" } and both adopt the sheet. One component calls sheet.replaceSync(".card { color: red; }"). What happens to the other component, and why?
Show answer
The other component's styles change too. The browser evaluates the CSS module once and gives every importer the same CSSStyleSheet object, so a change through one reference is visible to everyone who adopted it. This is the same shared, cached behavior that JSON modules have.
Exercise
Your code runs in Node.js and contains import styles from "./app.css" with { type: "css" }. What happens?
Show answer
The import fails with an error at load time because Node.js does not support the "css" type. Because it is a static import, the whole module graph fails and none of the importing module's code runs.