Build a library with peer dependencies
Treat the package boundary as a public contract
A component library can ship independently of the application that consumes it. Package exports define which entry points consumers may import. A peer dependency expresses the host React versions the library intends to support, rather than bundling its own React copy. A second React instance can cause Hook failures if it differs from the renderer instance. A peer declaration alone does not ensure a single copy; verify the installed graph and bundler output. The proposed React 18/19 range below is a compatibility target, not a tested range. This local ESM-only package needs no bundler: the component uses createElement directly. A JSX/TSX library normally needs compilation and declaration generation.
{ "name": "@bytemind-lab/status-badge", "version": "1.0.0", "private": true, "type": "module", "files": [ "index.mjs", "index.d.ts", "status.mjs", "status.d.mts", "badge.css", "README.md" ], "exports": { ".": { "types": "./index.d.ts", "import": "./index.mjs" }, "./status": { "types": "./status.d.mts", "import": "./status.mjs" }, "./badge.css": "./badge.css" }, "types": "./index.d.ts", "peerDependencies": { "react": "^18.0.0 || ^19.0.0" }, "sideEffects": [ "**/*.css" ]}The dot entry exposes the component and the status subpath exposes a dependency-free helper. The types conditions appear before import conditions. CSS has its own explicit export so a bundler consumer can opt in to styles. The files allowlist limits distribution and private prevents ordinary npm publish; npm pack can still create a local archive. The sideEffects list is bundler metadata, not a JavaScript guarantee: only CSS is marked here because the reviewed modules have no intended import-time side effects. An ESM-only package does not promise a CommonJS entry. If CJS is a real requirement, generate and test separate import/require entries and compatible type declarations; do not relabel ESM as CJS.
Implement a small runtime entry
import {createElement} from 'react';import {statusLabel} from './status.mjs';export function Badge({status}) { return createElement('span', {className: 'bm-badge', 'data-status': status}, statusLabel(status));}The source imports React from the host and calls the helper in status.mjs (included in the fixture). A consumer imports Badge from @bytemind-lab/status-badge and imports @bytemind-lab/status-badge/badge.css in a CSS-aware build. Ready or Waiting appears as text; invalid runtime status throws instead of quietly displaying the wrong status. If using Vite library mode for JSX instead, externalize react, react-dom when used, and their imported subpaths such as react/jsx-runtime; inspect generated modules, do not assume configuration alone works. Tree shaking means a compatible bundler may remove unused exports; measure an actual consumer build.
Ship types and CSS as deliberate artifacts
import type {ReactElement} from 'react';export type Status = 'ready' | 'waiting';export type BadgeProps = {status: Status};export declare function Badge(props: BadgeProps): ReactElement;The declaration documents the same accepted status values and a React element return. It is hand authored for this small teaching package; keep it synchronized with the runtime and test it with actual React declarations. A larger library should emit declarations from the TypeScript source and inspect the resulting file graph. The included CSS uses a border, readable text and a status-specific background; consumers must import it explicitly. Verify that CSS and type files survive packing and that a real consumer can resolve each public path. Practice: propose a third status, update the helper, component, declaration, CSS and stories, then plan tests against both peer versions.