Skip to main content

Layers and dependency rules

Part of Architecture, which is built from ARCHITECTURE.md in the repository, where it is edited.

Which package may import which, and what each layer must not do.

LayerLives inMay importMust not
corepackages/core/srcitself and npm maths librariestouch DOM, React, a map engine, a browser global or the clock
portspackages/core/src/portstypes from corecontain an implementation (apart from small typed error classes)
fakespackages/core/src/fakesports, coreread browser globals
uipackages/map/srccore, ports, theme, reactimport an adapter, an engine or a browser service
adapterspackages/web/src/adaptersports, corebe imported by anything except the composition root
engine adapterpackages/adapter-maplibre/srccore, theme, the engine (a peer)import the ui, the hooks, the umbrella or the host
composition rootpackages/web/src/compositioneverythingbe imported as a singleton; it exposes createServices() only
umbrellapackages/sdk/srcthe packages' entry pointshold code of its own, or be imported by any package or the host
documentation sitesites/docs/{src,plugins,scripts}the packages' entry points, by nameimport a file inside a package or the host, or be imported by a package or an example

The documentation site is the mapcairn-docs Worker (sites/docs/cloudflare.config.ts), which owns docs.mapcairn.com as a Custom Domain and serves the build from its root. The host Worker (packages/web/workers/app.ts) answers /docs and /docs/* on the reference hostname and on the old hostnames with a permanent redirect to the same page on docs.mapcairn.com, without the /docs prefix (docs/plans/0017-documentation-site.md).

The umbrella (@mapcairn/sdk, released with the other packages from 0.1.0-rc.3) is for apps outside the repository: every file in it is export * from one package's entry point, plus two explicit lines in the root that settle the names two of the four declare (ScaleBar from map, ReadingDirection from core). The adapter is reachable at the subpath @mapcairn/sdk/adapter-maplibre only: src/adapterMaplibre.ts is the one script that imports @mapcairn/adapter-maplibre, and dependency-cruiser refuses the root and the other subpaths that import it directly (umbrella-root-has-no-adapter; it does not parse src/styles.css, whose @import of the adapter's stylesheet is checked by umbrella.test.ts and sdkDist.test.ts, and it does not follow a re-export through another file, which the drift tests catch), so an app that does not import the subpath has no map engine in its page or its server bundle. maplibre-gl is a required peer of the umbrella at the adapter's range; npm and pnpm install it, and strict pnpm and Yarn ask the app to list it. packages/core/src/lint/umbrella.test.ts and packages/sdk/src/umbrella.test.ts fail when it drifts from them.

The engine adapter is its own package, @mapcairn/adapter-maplibre, released with the others from 0.1.0-rc.3. It is the only SDK package that imports a map engine, maplibre-gl, which it takes as a peer dependency, so core, map, react and theme stay free of the engine. It imports only core and theme among MapCairn packages. Outside tests, nothing in packages/ imports it except the host's composition root: dependency-cruiser refuses an import of it from core, react, theme and map, and from any host file outside packages/web/src/composition (adapter-package-imported-by-composition-only). The only other file that may import the engine is the host's workerUrl.ts, which builds the worker's URL.

The ui layer (packages/map and packages/react) may read the clock (performance.now(), for the sheet's flick speed), make blob URLs (URL.createObjectURL and URL.revokeObjectURL, for the image download) and call the WebMCP API (document.modelContext) directly. None of these is a port. A port is added only when a second implementation is needed, for example a host that must replace the value or a test that cannot control it. Core still reads none of them.

npm run lint enforces these rules with dependency-cruiser (layers, engine imports, that only the composition root imports the adapter package, and that nothing imports the umbrella), Biome (restricted imports and globals, accessibility rules) and the harmony scanner in scripts/harmony (style, markup and wiring rules), whose rules are proven by Vitest tests in packages/core/src/lint. A violation fails the build.