客户端架构
Theme aliases
A theme works by exporting a set of components, e.g. Navbar
, Layout
, Footer
, to render the data passed down from plugins. Docusaurus and users use these components by importing them using the @theme
webpack alias:
import Navbar from '@theme/Navbar';
The alias @theme
can refer to a few directories, in the following priority:
- A user's
website/src/theme
directory, which is a special directory that has the higher precedence. - A Docusaurus theme package's
theme
directory. - Docusaurus core 提供的原始组件(通常用不到)。
This is called a layered architecture: a higher-priority layer providing the component would shadow a lower-priority layer, making swizzling possible. 假设有以下文件结构:
website
├── node_modules
│ └── @docusaurus/theme-classic
│ └── theme
│ └── Navbar.js
└── src
└── theme
└── Navbar.js
website/src/theme/Navbar.js
takes precedence whenever @theme/Navbar
is imported. 这被称为 swizzle。 If you are familiar with Objective C where a function's implementation can be swapped during runtime, it's the exact same concept here with changing the target @theme/Navbar
is pointing to!
We already talked about how the "userland theme" in src/theme
can re-use a theme component through the @theme-original
alias. One theme package can also wrap a component from another theme, by importing the component from the initial theme, using the @theme-init
import.
Here's an example of using this feature to enhance the default theme CodeBlock
component with a react-live
playground feature.
import InitialCodeBlock from '@theme-init/CodeBlock';
import React from 'react';
export default function CodeBlock(props) {
return props.live ? (
<ReactLivePlayground {...props} />
) : (
<InitialCodeBlock {...props} />
);
}