Default structure for frontend projects.
.
├── src/
│ ├── index.ts Pure public entrypoint: classes, helpers, types. No side effects.
│ ├── lib/ Non-component code. If the project has no components, put it directly in src.
│ │ └── greet.ts
│ ├── components/ Custom element classes. Pure: no define() here.
│ │ ├── counter-button.ts
│ │ └── counter-button.test.ts Tests live next to the code they test (Vitest).
│ └── elements/ Registering entrypoints, one per element. These have side effects.
│ └── counter-button.ts import class, define("counter-button", ...)
├── test/ Tests that are not tied to one source file (packaging contract).
├── dev/ Local development page that consumes the library; never published.
│ ├── index.html
│ ├── main.ts
│ └── vite.config.ts Aliases the package name (and /elements/*) to ../src for HMR.
├── dist/ Build output (gitignored). Mirrors src/ one file per module.
│ ├── index.js
│ ├── lib/greet.js
│ ├── components/counter-button.js
│ ├── elements/counter-button.js
│ └── types/**/*.d.ts Declarations generated by vite-plugin-dts, same tree.
├── vite.config.ts Library build (lib mode, preserveModules) + Vitest config.
├── tsconfig.json
├── package.json "exports" and "sideEffects", see below.
└── CHANGELOG.md Keep a Changelog file, maintained by hand.
The package exposes two kinds of entry points:
| Import | Side effects | Use |
|---|---|---|
import { CounterButton } from "<pkg>" |
none | Extend the class, or register it under your own tag name |
import "<pkg>/elements/counter-button" |
registers | Defines <counter-button> |
Bundlers tree-shake unused exports from the pure barrel, so a consumer who imports one helper does not ship every component. Registration cannot be tree-shaken, so each element gets its own registering module under src/elements/ and a matching subpath export:
Never set "sideEffects": false on this package. Bundlers would then drop import "<pkg>/elements/counter-button" entirely and the element would never be defined. The build uses preserveModules so dist/ mirrors src/ and the sideEffects glob matches exactly the registering modules. test/package-exports.test.ts guards this contract.
To add an element:
- Create the pure class in
src/components/<name>.tsand export it fromsrc/index.ts. - Create
src/elements/<name>.tsthat imports the class, defines the element, and augmentsHTMLElementTagNameMap. - Nothing else: the build globs
src/elements/*.tsfor entries and the./elements/*export pattern covers it.
Rules for the pure side: no top-level calls, no @customElement style decorators, no static initializers that touch globals.
| Script | What it does |
|---|---|
npm run dev |
Serve dev/ for local development, importing the library from src |
npm run build |
Build ESM bundle + type declarations into dist/ |
npm test |
Run Vitest once (test:watch for watch mode) |
npm run typecheck |
tsc --noEmit over src, dev and config |
npm run format |
Prettier write (format:check to verify only) |
npm run check |
typecheck + format:check + test |
npm publish |
Runs prepublishOnly (check + build) first |
- ESM only (
"type": "module"). Pure barrel atsrc/index.ts, registering entrypoints insrc/elements/. - Internal imports use
.jsextensions so the emitted declarations resolve under Node's ESM rules. - Runtime dependencies that consumers should install themselves go in
peerDependenciesand inrollupOptions.externalinvite.config.ts.
Tests run in happy-dom by default (test.environment in vite.config.ts). It is fast and covers custom elements, shadow roots, slots, adoptedStyleSheets and :host styling.
Known gaps in happy-dom 20 that matter for web components: composed events are not retargeted to the host, ElementInternals / attachInternals are missing, assignedSlot is undefined, declarative shadow DOM is not parsed, and ::part() / ::slotted() are dropped from stylesheets.
When a test depends on one of those, use a real browser instead of switching the whole suite:
npm i -D @vitest/browser playwright && npx playwright install chromium- Add a
test.browserblock tovite.config.ts(see the Vitest browser mode docs) and put such tests under a separate project or file pattern such assrc/**/*.browser.test.ts.
A single file can also opt into jsdom with // @vitest-environment jsdom at the top; jsdom retargets composed events and has ElementInternals, but lacks slotchange and :host styles.
CHANGELOG.md follows Keep a Changelog and the project uses Semantic Versioning. Changes go under ## [Unreleased] as they are made, grouped into ### Added, ### Changed, ### Deprecated, ### Removed, ### Fixed or ### Security, one bullet per change. When publishing, rename the Unreleased heading to the new version and date, bump version in package.json, and start a fresh Unreleased section.