# Layers

A layer is a directory or a package with its own `hamur.config.ts`, `app/`, `server/` and `public/`. An app lists its layers in `extends`, and Hamur merges them with the app. Only `extends` makes a layer: a folder named `layers/` is not scanned.

````ts [layers/extends.ts]
import { defineConfig } from "hamur"

export default defineConfig({
  framework: "react",
  extends: ["../base", "@acme/hamur-layer"],
})
````

## Every `extends` form

| Entry                         | Is                                                                                                                                                                    |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `"../base"`, an absolute path | a directory, resolved from the directory of the config that lists it                                                                                                  |
| `"@acme/hamur-layer"`         | a package: its `package.json` is resolved from that directory and its folder is the layer, also one installed from git or a tarball ([Remote layers](#remote-layers)) |

A missing layer fails the load with `Cannot find the layer "<entry>"`. A layer's own `extends` come right after it, so the list order is the priority order, and a directory is loaded once. A layer's config can set `name`, which is what `#server/<name>` calls it (default: the basename of its folder).

````ts [layers/layer-config.ts]
import { defineConfig } from "hamur"

export default defineConfig({
  name: "base",
  framework: "react",
  srcDir: "src",
})
````

## What is merged

Configs merge with the app over its layers, and earlier layers over later ones. The keys `name`, `extends`, `modules`, `rootDir`, `srcDir`, `serverDir`, `publicDir`, `buildDir`, `outputDir` and `cacheDir` of a layer's config are not merged into the app's; a layer's `srcDir`, `serverDir` and `publicDir` set its own folders. Layer modules run before the app's. A layer makes a stylesheet global by importing it in a plugin of its `app/plugins/` ([Global CSS](/styling#global-css)).

A layer's `server/index.ts` is its own door: the layer's pages read it as `#server`, its `plugins` and `middleware` run before the app's, and the app takes one of its handlers with `export { name } from "#server/<layer>"`.

### Layered directories

The app comes first, then each layer, an earlier one over a later one.

| Directory                                                                                                                                                                     | Rule                                                                                                                                                                                                                        |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `app/pages` (every `dir.pages`), `app/layouts`, `app/middleware`, `app/plugins`, `app/params`, `app/documents`, `app/tools`, `app/components/content`, `app/pages/_not-found` | the highest-priority entry of a route path or name                                                                                                                                                                          |
| `server/index.ts`                                                                                                                                                             | one door per root; its `defineServer` lists run low priority first, then the app's                                                                                                                                          |
| `server/tasks`                                                                                                                                                                | merged; one task name in two roots fails                                                                                                                                                                                    |
| `public/`                                                                                                                                                                     | merged by path: the app's file, else the highest-priority layer's; served by `hamur dev`, copied to `.output/public/`. A path that a layer's `public/` and `addPublicAssets` or `emitPublicAsset` both give fails the build |
| `app/error.<ext>`, `app/app.<ext>`                                                                                                                                            | the file of the highest-priority directory that has one                                                                                                                                                                     |
| `app/composables`, `app/utils`, `app/components`, `server/utils`, `shared/utils`                                                                                              | not merged: plain folders whose files are imported by path                                                                                                                                                                  |

Nothing else of a layer is scanned for names to import. A layer's components, composables and utils are reached by a path or from the layer's package; see [Importing a layer's files](#importing-a-layers-files).

## Importing a layer's files

A layer is a package with its own `package.json`, and its `imports` are its own: `#app/utils/y.ts` in a layer's page reads the layer's `app/utils/y.ts` while the app's `#app/utils/y.ts` reads the app's, because Bun, Node and TypeScript take the `imports` of the `package.json` nearest to the importing file. The app imports a layer installed as a dependency by its package name (`import { x } from "@acme/base/app/utils/x.ts"`) and a local layer by a relative path or an entry of its own `imports` (`"#base/*": "./layers/base/app/*"`). An in-tree layer's `tsconfig.json` extends `.hamur/tsconfig.layers-<name>.json`, which carries the layer's `#server`. A layer's door is reached with `#server/<name>`, not through a path into its `server/`: client code that imports a file under a layer's `server/` fails the build with the same `server/` refusal as the app's.

## Remote layers

A layer from GitHub, GitLab or a tarball is a package dependency. Install it with the package manager, which pins it in `bun.lock` and installs the layer's own dependencies with the app's, then list its package name in `extends`:

````json [layers/dependency-package.json]
{
  "devDependencies": {
    "@acme/base": "github:acme/base#v2",
    "acme-layer": "https://example.com/layer.tar.gz"
  }
}
````

````ts [layers/dependency.ts]
import { defineConfig } from "hamur"

export default defineConfig({
  framework: "react",
  extends: ["@acme/base", "acme-layer"],
})
````

`bun add github:acme/base#v2` writes the first line; `bun add https://example.com/layer.tar.gz` the second. The layer is found the same way on every machine, and no command makes a request for the config. A private repository authenticates through `.npmrc` or `bunfig.toml`, as any private package does. A layer installed as `@acme/base` is called `base`, its folder name, unless its config sets `name`.

Hamur does not download layers itself. An `extends` entry that starts with `github:`, `gh:`, `gitlab:` or `https:` fails the load before any request, naming the command to run:

- `extends[0] "github:acme/base#v2" is a remote layer, which was removed; add it as a dependency (bun add github:acme/base#v2) and extend its package name`;
- `extends[1] takes a path or a package name; [entry, options] was removed` for an `[entry, { auth, install }]` pair.

`hamur dev` watches the root of every layer, also one outside the app or under `node_modules`, and treats an edit there as an edit of the app.

## Find the directories from a module

`getLayerDirectories()` returns the app's directories and each layer's, highest priority first, every path absolute and ending in `/`. It works wherever a module runs. `hamur info` prints the same list under `Layers:`. See [`getLayerDirectories`](/api-reference/kit#getlayerdirectories-layerdirectories) for the fields.

````ts [layers/directories.ts]
import { defineModule, getLayerDirectories } from "hamur/kit"

export default defineModule({
  meta: { name: "layer-report" },
  setup() {
    const [, ...layers] = getLayerDirectories()
    for (const layer of layers) console.log(`${layer.name}: pages ${layer.appPages.join(", ")}, public ${layer.public}`)
  },
})
````
