Skip to content

feat(theme): deep-merge theme config defaults under the site config - #5592

Open
lorlike wants to merge 1 commit into
vuejs:mainfrom
lorlike:feat/theme-config
Open

lorlike wants to merge 1 commit into
vuejs:mainfrom
lorlike:feat/theme-config

Conversation

@lorlike

@lorlike lorlike commented Nov 30, 2026

Copy link
Copy Markdown

Motivation

Themes commonly need their own themeConfig defaults — outline, docFooter, search provider, theme-specific options. Today a theme can only deliver those by shipping a config export (my-theme/config) that every site has to import and wire up manually, or by duplicating defaults into each site's config. Hexo solved this years ago with theme_config: the theme ships defaults, the site config overrides them, and the framework merges them.

This PR brings that model to VitePress. It covers the config-merging half of the theme-decoupling goal discussed in #5344 (component overrides are out of scope here).

What this PR does

A theme may ship a config file colocated with its theme entry:

.vitepress/
└─ theme/
   ├─ index.ts    # theme entry
   └─ config.ts   # theme-provided default themeConfig
// .vitepress/theme/config.ts
import { defineThemeConfig } from 'vitepress'

export default defineThemeConfig({
  themeConfig: {
    outline: { level: [2, 3], label: 'On this page' },
    docFooter: { prev: 'Previous', next: 'Next' }
  }
})

VitePress loads it with the existing config loader and deep-merges it as the bottom-most themeConfig layer:

theme config defaults  <  extends chain  <  site config

Sites only declare the fields they want to override — everything else comes from the theme:

// .vitepress/config.mts
export default defineConfig({
  themeConfig: {
    outline: { level: [2, 4] } // everything else comes from the theme
  }
})

Merge semantics

Implemented by the new mergeThemeConfig() (distinct from mergeConfig() on purpose):

  • plain objects merge recursively
  • arrays and non-plain-object values are replaced wholesale by the higher layer — a theme's default outline.level / nav should be fully overridable per key, which mergeConfig's array concatenation would break
  • the site config, including its own extends chain, always wins
  • locale layering is unaffected: defaults live in the root themeConfig, below the existing locale/additional-config layers

API

  • defineThemeConfig<ThemeConfig>(config) — typed helper for the theme config file (themeConfig?: DeepPartial<ThemeConfig>)
  • mergeThemeConfig(themeDefaults, siteConfig) — exported for tests and advanced composition
  • DeepPartial is now exported from the shared types

Behavior details

  • The file must stay Node-safe (no Vue component imports) — it is loaded via the same loadConfigFromFile used for site configs and additional configs
  • Both theme/config.[ext] and theme/config/index.[ext] are resolved
  • The file and its imports are added to configDeps — the dev server restarts on change, exactly like site config changes
  • Only loaded when a custom theme directory exists (.vitepress/theme/); the default theme is unaffected
  • No implicit merging across the theme's extends chain (that would require loading theme modules in Node). Themes building on other themes should import/spread their base defaults explicitly

Docs

New "Theme Config Defaults" section in the custom theme guide, including the npm-distribution pattern (ship defaults as a package sub-path, e.g. my-theme/config, and document a one-line re-export in the site's .vitepress/theme/config.ts).

Tests

  • 4 new unit tests for mergeThemeConfig: nested merge under site values, array/non-plain-value replacement, undefined-key passthrough, single-layer fallback
  • Full unit suite green (529 passed)
  • Verified end-to-end against a real-world blog site: theme defaults applied in dev and build, site overrides win, config-file edits restart the dev server

Notes / possible follow-ups

Allow a theme to ship default themeConfig values in a config file
colocated with its theme entry (.vitepress/theme/config.[ext]),
resolved with the existing config loader. VitePress deep-merges these
defaults under the extends chain and the site config at config
resolution time, so sites only declare the fields they want to
override — similar to Hexo's theme_config.

- add defineThemeConfig() helper and mergeThemeConfig() (objects merge
  recursively; arrays and non-plain-object values are replaced
  wholesale, unlike mergeConfig which concatenates arrays)
- theme defaults form the bottom-most config layer: extends chain and
  the site config override them
- theme config file and its imports are tracked in configDeps, so dev
  restarts on change like site config changes
- export the DeepPartial type used by the helper
- document the convention in the custom theme guide
@netlify

netlify Bot commented Nov 30, 2026

Copy link
Copy Markdown

👷 Deploy request for vitepress-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit cc71c1f

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant