Skip to content

Lesson 9 of 9 · 28m

Shipping Portable Components: Packaging, SSR, Accessibility, and Versioning

The operational checklist for publishing components that survive multiple bundlers, server rendering, assistive technology, and years of version drift.

A component library that only works in the author's Vite app is not portable; it is merely frameworkless. Production portability means predictable module side effects, collision-aware registration, credible SSR behavior, real accessibility, and versioning consumers can trust.

Registration is a global side effect

export class UserBadge extends HTMLElement {}
if (!customElements.get("user-badge")) {
  customElements.define("user-badge", UserBadge);
}

The guard helps hot reload and duplicate imports, but it does not solve version conflicts. Two package versions can register different classes under one tag; the first wins. Internal application libraries can accept that risk. Public widgets need specific namespaces, controlled define() functions, or scoped registries where support allows.

Publish JavaScript the platform can run

{
  "type": "module",
  "exports": {
    ".": "./dist/index.js",
    "./user-badge": "./dist/user-badge.js"
  },
  "sideEffects": ["./dist/index.js"]
}

sideEffects matters because registration is itself a side effect. Marking everything side-effect-free can let a bundler tree-shake away the definition. Test webpack, Vite/Rollup, and plain browser module imports before release.

SSR is about content first

Declarative Shadow DOM can serialize internal structure:

<user-badge>
  <template shadowrootmode="open">
    <span part="label">Pro</span>
  </template>
</user-badge>

It is useful where supported, but not a universal baseline. A resilient default is server-rendered light DOM content plus progressive enhancement. Do not require shadow DOM for meaningful content unless your support matrix explicitly allows it.

Accessibility cannot be encapsulated away

Verify keyboard focus order through the composed tree, visible focus, state attributes, labels, and reduced-motion behavior. Shadow DOM can hide broken semantics from automated scanners, so run axe or Playwright checks and then operate the control manually. Automation rarely catches a focus trap that technically has roles and labels.

Version the contract and test it

Semantic versioning should reflect behavior: patch for internal fixes, minor for additive slots/parts/properties/events, major for renamed tags, changed event detail shapes, removed hooks, or visual breaking changes.

test("emits composed change event", async () => {
  const el = document.createElement("quantity-stepper");
  document.body.append(el);
  const seen = new Promise((resolve) =>
    el.addEventListener("stepper-change", resolve, { once: true }));
  el.shadowRoot.querySelector("button")?.click();
  expect((await seen).composed).toBe(true);
});

Also test disconnected cleanup, property-before-upgrade behavior, form reset, and duplicate registration. Those are the failures consumers experience as 'randomly broken after navigation'.

Field notes

  • Include a plain HTML demo importing dist without a bundler; it is the fastest packaging smoke test.
  • Libraries should document required polyfills rather than silently depending on globals.
  • Choose specific tag names. <ui-button> invites collision; <acme-checkout-button> survives the ecosystem.
  • Write changelogs in contract terms, not 'improved internals'.

Was this lesson helpful?

Sign in to rate