命令行界面
Docusaurus 提供了一组脚本来帮助您生成、服务和部署您的网站。
一旦你的网站被引导启动,网站源代码将包含你可以用你的包管理器调用的Docusaurus脚本:
{
// ...
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
"serve": "docusaurus serve",
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids"
}
}
Docusaurus CLI 命令
以下是Docusaurus CLI命令及其用法的列表:
docusaurus start [siteDir]
使用Webpack Dev Server在本地构建并提供您网站的预览。
选项
| Name | Default | Description |
|---|---|---|
--port | 3000 | Specifies the port of the dev server. |
--host | localhost | Specify a host to use. For example, if you want your server to be accessible externally, you can use --host 0.0.0.0. |
--locale | Specify site locale to be used. | |
--hot-only | false | Enables Hot Module Replacement without page refresh as a fallback in case of build failures. More information here. |
--no-open | false | Do not open the page automatically in the browser. |
--config | undefined | Path to Docusaurus config file, default to [siteDir]/docusaurus.config.js |
--poll [optionalIntervalMs] | false | Use polling of files rather than watching for live reload as a fallback in environments where watching doesn't work. More information here. |
--no-minify | false | Build website without minimizing JS/CSS bundles. |
请注意,某些功能(例如,锚点链接)在开发环境中将无法使用。这些功能在生产环境中将按预期工作。
当从远程服务器或虚拟机(例如 GitHub Codespaces)转发端口 3000 时,您可以在 0.0.0.0 上运行开发服务器,使其监听本地 IP。
- npm
- Yarn
- pnpm
npm run start -- --host 0.0.0.0
yarn run start --host 0.0.0.0
pnpm run start --host 0.0.0.0
启用HTTPS
有多种方式可以获取证书。我们将以mkcert为例。
-
运行
mkcert localhost以生成localhost.pem+localhost-key.pem -
运行
mkcert -install以将证书安装到您的信任存储中,并重新启动浏览器 -
使用 Docusaurus HTTPS 环境变量启动应用程序:
HTTPS=true SSL_CRT_FILE=localhost.pem SSL_KEY_FILE=localhost-key.pem yarn start
- 打开
https://localhost:3000/
docusaurus build [siteDir]
为生产环境编译您的站点。
选项
| Name | Default | Description |
|---|---|---|
--dev | Builds the website in dev mode, including full React error messages. | |
--bundle-analyzer | false | Analyze your bundle with the webpack bundle analyzer. |
--out-dir | build | The full path for the new output directory, relative to the current workspace. |
--config | undefined | Path to Docusaurus config file, default to [siteDir]/docusaurus.config.js |
--locale | Build the site in the specified locale. If not specified, all known locales are built. | |
--no-minify | false | Build website without minimizing JS/CSS bundles. |
为了对CSS包进行高级压缩,我们使用了高级cssnano预设(以及一些额外的PostCSS插件)和clean-css的2级优化。如果由于这种高级CSS压缩导致CSS损坏,请使用环境变量USE_SIMPLE_CSS_MINIFIER=true构建您的网站,以使用默认的cssnano预设进行CSS压缩。如果您遇到CSS压缩错误,请提交问题。
您可以使用环境变量SKIP_HTML_MINIFICATION=true跳过HTML压缩。
docusaurus swizzle [themeName] [componentName] [siteDir]
Swizzle 一个主题组件以自定义它。
- npm
- Yarn
- pnpm
npm run swizzle [themeName] [componentName] [siteDir]
# Example (leaving out the siteDir to indicate this directory)
npm run swizzle @docusaurus/theme-classic Footer -- --eject
yarn swizzle [themeName] [componentName] [siteDir]
# Example (leaving out the siteDir to indicate this directory)
yarn swizzle @docusaurus/theme-classic Footer --eject
pnpm run swizzle [themeName] [componentName] [siteDir]
# Example (leaving out the siteDir to indicate this directory)
pnpm run swizzle @docusaurus/theme-classic Footer --eject
swizzle CLI 是交互式的,将引导您完成整个 swizzle 过程。
选项
| Name | Description |
|---|---|
themeName | The name of the theme to swizzle from. |
componentName | The name of the theme component to swizzle. |
--list | Display components available for swizzling |
--eject | Eject the theme component |
--wrap | Wrap the theme component |
--danger | Allow immediate swizzling of unsafe components |
--typescript | Swizzle the TypeScript variant component |
--config | Path to docusaurus config file, default to [siteDir]/docusaurus.config.js |
不安全的组件由于内部重构而具有更高的破坏性更改风险。
docusaurus deploy [siteDir]
使用GitHub Pages部署您的站点。查看部署文档以获取更多详细信息。
选项
| Name | Default | Description |
|---|---|---|
--locale | Deploy the site in the specified locale. If not specified, all known locales are deployed. | |
--out-dir | build | The full path for the new output directory, relative to the current workspace. |
--skip-build | false | Deploy website without building it. This may be useful when using a custom deploy script. |
--target-dir | . | Path to the target directory to deploy to. |
--config | undefined | Path to Docusaurus config file, default to [siteDir]/docusaurus.config.js |
docusaurus serve [siteDir]
在本地提供您构建的网站。
| Name | Default | Description |
|---|---|---|
--port | 3000 | Use specified port |
--dir | build | The full path for the output directory, relative to the current workspace |
--build | false | Build website before serving |
--config | undefined | Path to Docusaurus config file, default to [siteDir]/docusaurus.config.js |
--host | localhost | Specify a host to use. For example, if you want your server to be accessible externally, you can use --host 0.0.0.0. |
--no-open | false locally, true in CI | Do not open a browser window to the server location. |
docusaurus clear [siteDir]
清除Docusaurus站点生成的资源、缓存、构建产物。
我们建议在报告错误之前、升级版本之后或您的Docusaurus网站出现问题时运行此命令。
docusaurus write-translations [siteDir]
编写你需要翻译的JSON翻译文件。
默认情况下,文件被写入 website/i18n/。
| Name | Default | Description |
|---|---|---|
--locale | <defaultLocale> | Define which locale folder you want to write translations the JSON files in |
--override | false | Override existing translation messages |
--config | undefined | Path to Docusaurus config file, default to [siteDir]/docusaurus.config.js |
--messagePrefix | '' | Allows adding a prefix to each translation message, to help you highlight untranslated strings |
docusaurus write-heading-ids [siteDir] [files]
为您的网站的Markdown文档添加显式标题ID。
| Name | Default | Description |
|---|---|---|
files | All MD files used by plugins | The files that you want heading IDs to be written to. |
--maintain-case | false | Keep the headings' casing, otherwise make all lowercase. |
--overwrite | false | Overwrite existing heading IDs. |