Skip to main content
Version: 3.5.2

命令行界面

Docusaurus 提供了一组脚本来帮助您生成、服务和部署您的网站。

一旦你的网站被引导启动,网站源代码将包含你可以用你的包管理器调用的Docusaurus脚本:

package.json
{
// ...
"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在本地构建并提供您网站的预览。

选项

NameDefaultDescription
--port3000Specifies the port of the dev server.
--hostlocalhostSpecify a host to use. For example, if you want your server to be accessible externally, you can use --host 0.0.0.0.
--localeSpecify site locale to be used.
--hot-onlyfalseEnables Hot Module Replacement without page refresh as a fallback in case of build failures. More information here.
--no-openfalseDo not open the page automatically in the browser.
--configundefinedPath to Docusaurus config file, default to [siteDir]/docusaurus.config.js
--poll [optionalIntervalMs]falseUse polling of files rather than watching for live reload as a fallback in environments where watching doesn't work. More information here.
--no-minifyfalseBuild website without minimizing JS/CSS bundles.
info

请注意,某些功能(例如,锚点链接)在开发环境中将无法使用。这些功能在生产环境中将按预期工作。

Development over network

当从远程服务器或虚拟机(例如 GitHub Codespaces)转发端口 3000 时,您可以在 0.0.0.0 上运行开发服务器,使其监听本地 IP。

npm run start -- --host 0.0.0.0

启用HTTPS

有多种方式可以获取证书。我们将以mkcert为例。

  1. 运行 mkcert localhost 以生成 localhost.pem + localhost-key.pem

  2. 运行 mkcert -install 以将证书安装到您的信任存储中,并重新启动浏览器

  3. 使用 Docusaurus HTTPS 环境变量启动应用程序:

HTTPS=true SSL_CRT_FILE=localhost.pem SSL_KEY_FILE=localhost-key.pem yarn start
  1. 打开 https://localhost:3000/

docusaurus build [siteDir]

为生产环境编译您的站点。

选项

NameDefaultDescription
--devBuilds the website in dev mode, including full React error messages.
--bundle-analyzerfalseAnalyze your bundle with the webpack bundle analyzer.
--out-dirbuildThe full path for the new output directory, relative to the current workspace.
--configundefinedPath to Docusaurus config file, default to [siteDir]/docusaurus.config.js
--localeBuild the site in the specified locale. If not specified, all known locales are built.
--no-minifyfalseBuild website without minimizing JS/CSS bundles.
info

为了对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 run swizzle [themeName] [componentName] [siteDir]

# Example (leaving out the siteDir to indicate this directory)
npm run swizzle @docusaurus/theme-classic Footer -- --eject

swizzle CLI 是交互式的,将引导您完成整个 swizzle 过程

选项

NameDescription
themeNameThe name of the theme to swizzle from.
componentNameThe name of the theme component to swizzle.
--listDisplay components available for swizzling
--ejectEject the theme component
--wrapWrap the theme component
--dangerAllow immediate swizzling of unsafe components
--typescriptSwizzle the TypeScript variant component
--configPath to docusaurus config file, default to [siteDir]/docusaurus.config.js
warning

不安全的组件由于内部重构而具有更高的破坏性更改风险。

docusaurus deploy [siteDir]

使用GitHub Pages部署您的站点。查看部署文档以获取更多详细信息。

选项

NameDefaultDescription
--localeDeploy the site in the specified locale. If not specified, all known locales are deployed.
--out-dirbuildThe full path for the new output directory, relative to the current workspace.
--skip-buildfalseDeploy website without building it. This may be useful when using a custom deploy script.
--target-dir.Path to the target directory to deploy to.
--configundefinedPath to Docusaurus config file, default to [siteDir]/docusaurus.config.js

docusaurus serve [siteDir]

在本地提供您构建的网站。

NameDefaultDescription
--port3000Use specified port
--dirbuildThe full path for the output directory, relative to the current workspace
--buildfalseBuild website before serving
--configundefinedPath to Docusaurus config file, default to [siteDir]/docusaurus.config.js
--hostlocalhostSpecify a host to use. For example, if you want your server to be accessible externally, you can use --host 0.0.0.0.
--no-openfalse locally, true in CIDo not open a browser window to the server location.

docusaurus clear [siteDir]

清除Docusaurus站点生成的资源、缓存、构建产物。

我们建议在报告错误之前、升级版本之后或您的Docusaurus网站出现问题时运行此命令。

docusaurus write-translations [siteDir]

编写你需要翻译的JSON翻译文件。

默认情况下,文件被写入 website/i18n//...

NameDefaultDescription
--locale<defaultLocale>Define which locale folder you want to write translations the JSON files in
--overridefalseOverride existing translation messages
--configundefinedPath 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

NameDefaultDescription
filesAll MD files used by pluginsThe files that you want heading IDs to be written to.
--maintain-casefalseKeep the headings' casing, otherwise make all lowercase.
--overwritefalseOverwrite existing heading IDs.