Skip to content

feat: enhance api.transform typing with conditional types#6499

Merged
chenjiahan merged 1 commit intomainfrom
enhance_type_1104
Nov 4, 2025
Merged

feat: enhance api.transform typing with conditional types#6499
chenjiahan merged 1 commit intomainfrom
enhance_type_1104

Conversation

@chenjiahan
Copy link
Member

@chenjiahan chenjiahan commented Nov 4, 2025

Summary

Previously, the code property of api.transform was always typed as string | Buffer, requiring type checking even when the raw mode was known at compile time:

api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
  if (Buffer.isBuffer(code)) {
    // 
  }
});

This PR implemented conditional typing that provides precise types based on the raw option:

// When raw: true, code is definitively Buffer
api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
  // code is typed as Buffer
  emitFile(name, code);
});

Related Links

close #6017

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings November 4, 2025 14:11
@netlify
Copy link

netlify bot commented Nov 4, 2025

Deploy Preview for rsbuild ready!

Name Link
🔨 Latest commit d65bb95
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/690a09acec7aa400084908aa
😎 Deploy Preview https://deploy-preview-6499--rsbuild.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 69 (🟢 up 1 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: 60 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Contributor

Copilot AI left a comment

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 adds type-safe support for the raw option in transform hooks, ensuring that when raw: true is specified in a transform descriptor, the handler receives a Buffer instead of a string for the code property.

Key changes:

  • Made TransformContext and TransformHandler generic with a Raw type parameter
  • Updated TransformHook to infer the raw property from the descriptor and provide correct typing to the handler
  • Updated the transformer storage to accommodate handlers with different raw configurations

Reviewed Changes

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

File Description
packages/core/src/types/plugin.ts Added generic type parameters to TransformContext, TransformHandler, and TransformHook to support type-safe raw Buffer handling
packages/core/src/initPlugins.ts Updated transformer record type to accept handlers with either raw or string code types

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

@chenjiahan chenjiahan merged commit 59b0794 into main Nov 4, 2025
19 checks passed
@chenjiahan chenjiahan deleted the enhance_type_1104 branch November 4, 2025 14:52
@chenjiahan chenjiahan mentioned this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant