Skip to content

feat(core): support environment scoped exposed APIs#7994

Open
9aoy wants to merge 9 commits into
mainfrom
9aoy/environment-scoped-expose
Open

feat(core): support environment scoped exposed APIs#7994
9aoy wants to merge 9 commits into
mainfrom
9aoy/environment-scoped-expose

Conversation

@9aoy

@9aoy 9aoy commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds environment-scoped expose / useExposed support so integrations such as Rstest can expose per-environment APIs without leaking configuration changes across projects. useExposed now prefers an API registered for the current plugin environment and falls back to the global exposed API, while expose remains backward-compatible and only scopes APIs when an environment option is provided.

Usage Example

const rsbuild = await createRsbuild({
  callerName: 'rstest',
  config: {
    environments: {
      project1: {
        plugins: [project1Plugin],
      },
      project2: {
        plugins: [project2Plugin],
      },
    },
  },
});

rsbuild.expose('rstest', project1RstestApi, { environment: 'project1' });
rsbuild.expose('rstest', project2RstestApi, { environment: 'project2' });
const project1Plugin: RsbuildPlugin = {
  name: 'project1-plugin',
  setup(api) {
    const rstestApi = api.useExposed<RstestPluginAPI>('rstest');

    rstestApi?.modifyRstestConfig((config) => {
      config.include.push('**/*.project1.test.ts');
    });
  },
};

In this example, project1Plugin resolves the rstest API exposed for project1. It does not receive the API exposed for project2, and will fall back to a global exposed API only when no matching environment API exists.

Related Links

https://github.com/web-infra-dev/rsbuild/blob/main/packages/core/src/pluginManager.ts

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0314628b-777d-47d7-8d4e-af1a0dc238c9

📥 Commits

Reviewing files that changed from the base of the PR and between fd2dd24 and aa015d3.

📒 Files selected for processing (1)
  • packages/core/src/initPlugins.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/initPlugins.ts

📝 Walkthrough

Walkthrough

The PR adds environment-scoped support for rsbuild.expose and api.useExposed. api.expose now accepts optional environment metadata, core storage keeps exposed values per id and environment, and lookup resolves exact environment matches before falling back to global values. Documentation was updated in English and Chinese, and new e2e tests cover matching, fallback, visibility, and overwrite cases.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: environment-scoped exposed APIs in core.
Description check ✅ Passed The description accurately matches the changes and explains the new environment-scoped expose/useExposed behavior.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@9aoy

9aoy commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: b733f8ccf0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@9aoy 9aoy marked this pull request as ready for review June 25, 2026 05:00
Copilot AI review requested due to automatic review settings June 25, 2026 05:00

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@website/docs/en/plugins/dev/core.mdx`:
- Around line 758-761: Update the plugin API example to use api.expose instead
of rsbuild.expose so it matches the documented method and does not suggest the
environment-scoped option is only available on the instance API. Locate the
snippet in the core.mdx docs section around the api.expose documentation and
replace both rsbuild.expose calls with the corresponding api.expose calls while
keeping the same arguments.

In `@website/docs/zh/plugins/dev/core.mdx`:
- Around line 759-760: The plugin API example currently uses rsbuild.expose,
which conflicts with the surrounding api.expose documentation. Update the
example in the core plugin API section to use api.expose for both calls, and
keep the same arguments and environment values so the example matches the API
being described.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0c6deb72-5e81-41fa-b2cc-a773c05f79e3

📥 Commits

Reviewing files that changed from the base of the PR and between f45b5aa and b733f8c.

📒 Files selected for processing (7)
  • e2e/cases/plugin-api/plugin-expose/index.test.ts
  • packages/core/src/initPlugins.ts
  • packages/core/src/types/plugin.ts
  • website/docs/en/api/javascript-api/instance.mdx
  • website/docs/en/plugins/dev/core.mdx
  • website/docs/zh/api/javascript-api/instance.mdx
  • website/docs/zh/plugins/dev/core.mdx

Comment thread website/docs/en/plugins/dev/core.mdx
Comment thread website/docs/zh/plugins/dev/core.mdx Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends Rsbuild’s exposed plugin APIs to support environment-scoped registration and resolution, enabling integrations (e.g. Rstest) to expose different APIs per config.environments entry without leaking across environments. useExposed now prefers an API exposed for the current plugin environment and falls back to a global exposed API, while expose remains backward-compatible unless an environment option is provided.

Changes:

  • Extend expose to accept an optional { environment?: string } and store multiple exposed APIs per id (global + per-environment).
  • Update useExposed resolution to prefer same-environment API and fall back to global.
  • Add e2e coverage and update both English/Chinese docs for the new behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
website/docs/zh/plugins/dev/core.mdx Documents api.expose options and environment-scoped resolution behavior.
website/docs/zh/api/javascript-api/instance.mdx Adds environment-scoped rsbuild.expose usage example and behavior description.
website/docs/en/plugins/dev/core.mdx Documents api.expose options and environment-scoped resolution behavior.
website/docs/en/api/javascript-api/instance.mdx Adds environment-scoped rsbuild.expose usage example and behavior description.
packages/core/src/types/plugin.ts Adds ExposeOptions and updates RsbuildPluginAPI.expose signature + useExposed behavior note.
packages/core/src/initPlugins.ts Implements environment-scoped exposed API storage and environment-aware useExposed.
e2e/cases/plugin-api/plugin-expose/index.test.ts Adds e2e tests for environment scoping, fallback to global, and non-leakage to global plugins.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/core/src/types/plugin.ts
Comment thread website/docs/en/plugins/dev/core.mdx Outdated
Comment thread website/docs/zh/plugins/dev/core.mdx Outdated
Comment thread website/docs/en/api/javascript-api/instance.mdx Outdated
Comment thread website/docs/zh/api/javascript-api/instance.mdx Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 25, 2026 06:01
9aoy and others added 3 commits June 25, 2026 14:01
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Comment thread website/docs/en/plugins/dev/core.mdx Outdated
Comment thread website/docs/en/plugins/dev/core.mdx Outdated
Comment thread website/docs/en/api/javascript-api/instance.mdx Outdated
Comment thread packages/core/src/types/plugin.ts Outdated
Copilot AI review requested due to automatic review settings June 25, 2026 06:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread packages/core/src/initPlugins.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 25, 2026 06:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

@9aoy 9aoy requested a review from chenjiahan June 25, 2026 06:20
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 25, 2026

Copy link
Copy Markdown

Deploying rsbuild with  Cloudflare Pages  Cloudflare Pages

Latest commit: fd2dd24
Status:🚫  Build failed.

View logs

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.

2 participants