Skip to content

About

Skeleton for a TypeScript frontend library built with Vite

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@ruphin/frontend-structure

Default structure for frontend projects.

Layout

.
├── 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.

Entry points and tree shaking

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:

"sideEffects": ["./dist/elements/*.js"],
"exports": {
  ".":            { "types": "./dist/types/index.d.ts",      "import": "./dist/index.js" },
  "./elements/*": { "types": "./dist/types/elements/*.d.ts", "import": "./dist/elements/*.js" }
}

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:

  1. Create the pure class in src/components/<name>.ts and export it from src/index.ts.
  2. Create src/elements/<name>.ts that imports the class, defines the element, and augments HTMLElementTagNameMap.
  3. Nothing else: the build globs src/elements/*.ts for 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.

Scripts

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

Conventions

  • ESM only ("type": "module"). Pure barrel at src/index.ts, registering entrypoints in src/elements/.
  • Internal imports use .js extensions so the emitted declarations resolve under Node's ESM rules.
  • Runtime dependencies that consumers should install themselves go in peerDependencies and in rollupOptions.external in vite.config.ts.

Testing environments

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:

  1. npm i -D @vitest/browser playwright && npx playwright install chromium
  2. Add a test.browser block to vite.config.ts (see the Vitest browser mode docs) and put such tests under a separate project or file pattern such as src/**/*.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

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.

About

Skeleton for a TypeScript frontend library built with Vite

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages