Part of [Architecture](https://docs.mapcairn.com/architecture.md), which is built from [ARCHITECTURE.md](https://github.com/MapCairn/mapcairn/blob/main/ARCHITECTURE.md) in the repository, where it is edited.

How the six SDK packages are built, and in what order.

`core`, `react`, `theme`, `map` and `adapter-maplibre` build to `dist`, and so does the umbrella `sdk`, last (all six are in the release list `SDK_PACKAGES`, in that order, each after the packages it depends on; the umbrella's stylesheet is a hand-written three-line `src/styles.css` that `buildPackage.mjs` copies, and its declarations keep their star re-exports of the packages). `UNRELEASED_SDK_PACKAGES` in `scripts/sdkPackages.mjs` holds a private package that builds and is checked like the others but is not in the release set yet (none at present), and `BUILT_SDK_PACKAGES` (the two lists, in that order) is what `SDK_BUILD_ORDER`, `depcruise` and the API reports follow. Each builds JavaScript with its `.js` extensions written in, one rolled-up `.d.ts` per entry point and, for the three that compile styles, a stylesheet (`theme` ships `tokens.css`, `map` and `adapter-maplibre` ship `styles.css`; `DESIGN.md`, "How the CSS ships"). `adapter-maplibre` has no hand-written stylesheet: `MapLibreMapAdapter.ts`, the module the factory is in, imports `./styles.css`, the `styles.css.ts` file, so the JavaScript build meets it and compiles it into `dist/styles.css`. The import is not in the entry module (`index.ts`), because `index.ts` is not listed in `sideEffects`, so a host's bundler drops its own imports (the factory module keeps its import because the host uses the factory); in the reference host's production build that left the engine without its styles, which `basemap.spec.ts` and `globe.spec.ts` caught. Its `sideEffects` list keeps `**/*.css` and `**/*.css.ts` for a bundler. A built module that came from a `*.css.ts` file is named `*.styles.js`, because vanilla-extract's plugin compiles every `*.css.js` file it meets. `npm run build:sdk` (`scripts/buildSdk.mjs`) builds them in that order (`map` after `theme`, because it builds against the built `theme`) and skips a package whose `dist/.built` stamp is newer than its `src`, its manifest, `tsconfig.base.json`, `package-lock.json` and the stamp of every SDK package it depends on, so rebuilding `theme` rebuilds `map`, and whose stamp holds the same hash as the build scripts have now (`BUILD_SCRIPTS` in `scripts/buildInputs.mjs`: `buildSdk.mjs`, `buildPackage.mjs`, `entryPoint.mjs`, `sdkPackages.mjs`, `shareDeclarations.mjs` and the file itself), so editing one of them rebuilds every package, and a checkout that only touches them rebuilds nothing; `--force` rebuilds. `scripts/buildPackage.mjs` builds one package: a Vite library build (ES modules, one module per source module, every bare import left external), `tsc --emitDeclarationOnly`, then API Extractor rolling the declarations up, the main entry (`index`) first. An entry point rolled up alone would inline a copy of every type it names, so before each other entry is rolled up `scripts/shareDeclarations.mjs` rewrites its imports of anything the main entry exports to import it from the package (`@mapcairn/core` in the `contracts` declarations), leaving one declaration of each shared type. The API report reads a copy of the declaration outside the package, so that import stays external.

Each entry point in `exports` has three conditions, in this order: `@mapcairn/source` (the `src` file, which a published package does not hold: `files` lists `dist` only, and `npm run pack:check` fails on anything else), `types` and `default` (the `dist` files). The condition is custom and never `development`, which bundlers switch on for consumers. Inside Vite the host and the unit tests resolve source for hot reload: `tsconfig.base.json` sets `customConditions`, and the Vite and Vitest configs set `resolve.conditions` and `environments.ssr.resolve.conditions` with the default conditions spread back in, because a set list replaces Vite's defaults. The environment setting, and not the older `ssr.resolve.conditions`, is what reaches the vanilla-extract compiler, which replaces the whole `ssr` option of the config it reads. Node-side code that does not go through Vite (the `tsImport` in `packages/web/vite.config.ts`, the Playwright specs, `scripts/catalogueCheck.mjs`) resolves `dist`, which is why the host's scripts run `build:sdk` first. An environment variable such as `NODE_OPTIONS=--conditions` is not used, because it does not work in the Windows shell.

`map` is built against the built `theme`: its `tsconfig.json` sets `customConditions: []`, so its declarations come from the other packages' `dist`, and its Vite build turns the source condition off, so its `.css.ts` files import `theme`'s compiled `tokens.styles.js`, whose token references are the names `tokens.css` defines. Built against `theme`'s source, a package's compiled CSS would repeat the token blocks and use different generated custom property names. `examples/embed` also sets `customConditions: []`, has no vanilla-extract plugin, and imports `@mapcairn/theme/tokens.css` and `@mapcairn/map/styles.css`, so it builds and runs from `dist` exactly as an outside app would. `@vanilla-extract/css` is a dev dependency of `theme`, and a dependency of `map` for the reason below, because neither's built JavaScript nor declarations name it; `@vanilla-extract/recipes` stays a dependency of `map`. It declares `@vanilla-extract/css` as a peer but does not load it when a recipe runs, which was checked with npm (default and `--legacy-peer-deps`) and with pnpm in strict mode. Plain npm installs the peer anyway, but pnpm with strict peer dependencies and no automatic peer install refuses the install, so `map` declares `@vanilla-extract/css` as a regular dependency (decided by the maintainer; see `sites/docs/docs/guides/public-api.md`) and the throwaway-consumer check (`npm run consumer:check`) runs that strict install, and also an install with Yarn, a Bun install when Bun is present (untested so far), and a server render of the packed build on workerd where Miniflare is installed (`sites/docs/docs/guides/public-api.md`).

## The public API

Each SDK entry point has a report of its exports at `packages/<name>/api/<entry>.api.md`, generated by API Extractor from the built declarations (`scripts/apiReports.mjs`, with the options in code and no `api-extractor.json`). `npm run api:check` fails when a report differs from the committed file, and `npm run api:update` rewrites it, so a change to an exported name shows in the pull request diff. CI runs the check after the build and prints the TSDoc warnings (`ae-undocumented`, unresolved links and TSDoc syntax) without failing on them. The public API is more than the exports: the shared-link format, the layer-definition validator, message keys, the formats a host stores, and new members of exported unions count too, and the custom property names and `data-*` hooks are not public until the maintainer decides. `sites/docs/docs/guides/public-api.md` has the full list, the release tags, how each is checked and how a change is classed for a release.
