Custom Tag Discovery

Custom tags are reusable .marko components discovered automatically from the filesystem or installed packages without explicit imports.

Priority

Marko resolves a tag name in the following order:

  1. Local variables (PascalCase identifiers in scope)
  2. Core tags (built-in tags cannot be shadowed by custom tags)
  3. Relative custom tags in tags/ directories up to the nearest package.json
  4. Installed custom tags from dependencies in package.json
  5. Native HTML, SVG, and MathML tags

Local Variables

When a tag name starts with an uppercase letter, Marko checks local scope first. This handles explicitly imported templates and <define> snippets:

import MyTag from "./my-tag.marko";

<MyTag/>
import MyTag from "./my-tag.marko";

MyTag
<define/MyTag|input|><span>Hello ${input.name}</span></define>
<MyTag name="HTML"/>
<MyTag name="Marko"/>
<define/MyTag|input: { name: string }|><span>Hello ${input.name}</span></define>

<MyTag name="HTML"/>
<MyTag name="Marko"/>
define/MyTag|input|
  span -- Hello ${input.name}
MyTag name="HTML"
MyTag name="Marko"
define/MyTag|input: { name: string }|
  span -- Hello ${input.name}

MyTag name="HTML"
MyTag name="Marko"
Note

Reference a non-PascalCase local variable using dynamic tag syntax:

import { camelCaseTag } from "somewhere";

<${camelCaseTag}/>
import { camelCaseTag } from "somewhere";

${camelCaseTag}

Relative Custom Tags

For tags not matched in local scope, Marko walks upward from the current template's directory to the package root (package.json), checking each tags/ directory in precedence order:

  1. tags/TAG_NAME.marko
  2. tags/TAG_NAME/index.marko
  3. tags/TAG_NAME/TAG_NAME.marko
  4. One level deep inside a grouping folder
tags/
  app-header.marko
  app-footer.marko
pages/
  about/
    tags/
      team-members.marko
    page.marko
  home/
    tags/
      home-banner.marko
    page.marko

pages/home/page.marko resolves:

  • <app-header>
  • <app-footer>
  • <home-banner>

pages/about/page.marko resolves:

  • <app-header>
  • <app-footer>
  • <team-members>

The home page cannot resolve <team-members> and the about page cannot resolve <home-banner>, scoping page-specific tags to their respective subtrees.

Note

Marko 5 discovered relative tags in components/ directories, which are now used as a heuristic for Marko 5 interop.

Grouping Folders

Related tags can be organized one level deep inside a folder that is not itself a tag. The folder name organizes files on disk without changing tag names:

tags/
  menu-button.marko
  icons/
    icon-chevron.marko
    icon-plus.marko

This discovers <menu-button>, <icon-chevron>, and <icon-plus>.

Grouping rules:

  • Grouping applies one level deep (tags/icons/outline/icon-star.marko is not discovered).
  • A folder containing index.marko, a matching FOLDER_NAME.marko, or marko-tag.json is treated as a single tag rather than a grouping folder, so helper files alongside it are not crawled.
  • tags, components, and node_modules folders are never treated as grouping folders.
  • Dot-prefixed folders (such as .hidden) are skipped; non-leading dots (such as legacy.v1) still group.

Installed Custom Tags

Marko inspects packages listed in package.json (dependencies, peerDependencies, and devDependencies) for a root marko.json with an "exports" directory:

marko.json
{
  "exports": "./dist/tags"
}

If the package defines a package.json "exports" map, it must also expose the directory named in marko.json because compiled consumer templates import discovered tags by their package path (such as @acme/ui/dist/tags/acme-icon/index.marko):

package.json
{
  "exports": {
    "./dist/*": "./dist/*"
  }
}

TypeScript tag libraries publish .d.marko.ts declarations emitted by @marko/type-check alongside their templates (see TypeScript).

Tip

To keep internal helper tags private to a library, place them in a nested tags/ directory inside the exported directory (for example dist/tags/tags/).

Caution

When two packages export the same tag name, Marko selects the first one discovered. Prefix library tag names (for example ebay-) to avoid collisions, or import by path to disambiguate.

Native Tag Registration

The compiler recognizes standard HTML, SVG, and MathML tags. To emit non-standard elements or custom elements verbatim as native HTML tags, register them in a marko.json beside the template or in an ancestor directory:

{
  "<x-datepicker>": { "html": true }
}

Supporting Files

Marko automatically discovers adjacent stylesheets for any .marko template using a single extension (<name>.style.<ext>, or style.<ext> next to index.marko). A tag directory may also include a marko-tag.json:

tags/
  card.marko
  card.style.css
  modal/
    index.marko
    style.scss
  widget/
    index.marko
    marko-tag.json

Any single preprocessor extension (.css, .scss, .less, etc.) is supported.


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.