close
  • 简体中文
  • CLI 使用教程

    我们提供了 @rsdoctor/cli 让你可以在本地通过 CLI 程序来使用 Rsdoctor 提供的功能

    Tip

    @rsdoctor/webpack-plugin@rsdoctor/rspack-plugin@rsdoctor/cli 要保持同一个 major 和 minor 版本。

    安装 @rsdoctor/cli

    Tip
    • @rsdoctor/cli & @rsdoctor/webpack-plugin & @rsdoctor/rspack-plugin >= 0.1.2。
    • 可以使用非安装方式,使用 npx @rsdoctor/cli <command> [options]
    npm
    yarn
    pnpm
    bun
    deno
    npm add @rsdoctor/cli -D

    命令使用

    
    rsdoctor <command> [options]
    

    @rsdoctor/cli 目前提供了以下几个命令对应不同的功能:

    analyze 命令

    analyze 命令主要是用于在本地加载 manifest.json 并且无需再次构建直接启动 Rsdoctor 的分析报告页面。

    rsdoctor analyze --profile <manifestFile>

    参数定义

    • manifestFile 即为 manifest.json 的路径(支持本地路径)

    使用示例

    rsdoctor analyze --profile "./dist/.rsdoctor/manifest.json"

    bundle-diff 命令

    bundle-diff 命令用于在本地加载两份 manifest.json,并对比分析两次构建产物。默认情况下,该命令会在浏览器中打开 Rsdoctor 的 Bundle Diff 页面。使用 --json [path] 可以直接生成 JSON 报告,而不会启动报告服务器或打开浏览器。

    rsdoctor bundle-diff --baseline <baselineManifestJsonPath> --current <currentManifestJsonPath>

    参数定义

    • --baseline <path>:作为基准manifest.json 路径或在线 URL。
    • --current <path>:作为当前构建manifest.json 路径或在线 URL。
    • --json [path]:生成 JSON 差异报告,默认输出路径为 rsdoctor-diff.json

    在浏览器中打开报告

    rsdoctor bundle-diff --baseline="baseline/.rsdoctor/manifest.json" --current="current/.rsdoctor/manifest.json"

    生成 JSON 报告

    rsdoctor bundle-diff \
      --baseline="baseline/.rsdoctor/manifest.json" \
      --current="current/.rsdoctor/manifest.json" \
      --json="result.json"

    JSON 报告包含 assets、modules 和 packages 的差异数据,可用于 CI 流程或生成 Pull Request 评论。

    Node API

    我们在 @rsdoctor/cli 中提供了 Node.js API,可以让你在 Node.js 运行时进行调用。

    引入模块

    esm
    cjs
    import { execute } from '@rsdoctor/cli';

    execute()

    execute 异步函数是 Rsdoctor CLI 的执行函数,通过调用 execute 函数就会自动解析 process.argv,然后调用不同的命令。

    execute('analyze', {...})

    如果你需要通过 Node.js API 直接执行 analyze 命令,则可以通过如下方式调用:

    execute('analyze', {
      profile: 'input the manifest.json path or url',
    }).then((sdk) => {
      console.log('execute "analyze" command success');
      // you can stop the Rsdoctor's dev-server by calling the sdk'api below:
      // sdk.dispose();
    });