Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 31, 2025

Fixed a type definition issue in TransformContext.code where the type was defined as string but runtime behavior could pass Buffer objects when transform descriptors have raw: true.

Problem

The TransformContext.code type was defined as string in the type system, but when a transform descriptor specifies raw: true, the system uses transformRawLoader.mjs which passes Buffer objects to transform handlers. This created a type mismatch where plugin authors expecting to handle Buffer objects when using raw: true would get TypeScript errors.

Solution

Updated the type definition of TransformContext.code from string to string | Buffer to accurately reflect the runtime behavior:

export type TransformContext = {
  /**
   * The code of the module.
   * When the transform descriptor has `raw: true`, this will be a Buffer.
   * Otherwise, it will be a string.
   */
  code: string | Buffer;
  // ...
}

This ensures that:

  • Plugin authors can properly type-check their transform handlers for both string and Buffer inputs
  • The type system accurately represents what happens at runtime
  • Transform handlers can use proper type guards to handle both cases appropriately

The fix maintains backward compatibility while providing accurate typing for the raw: true use case.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

netlify bot commented Aug 31, 2025

Deploy Preview for rsbuild ready!

Name Link
🔨 Latest commit 655a3e7
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/68b414cc1de10a000861c741
😎 Deploy Preview https://deploy-preview-6019--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: 84 (🟢 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.

@chenjiahan
Copy link
Member

@copilot finish this PR

Copy link
Contributor Author

Copilot AI commented Aug 31, 2025

@copilot finish this PR

Fixed the Buffer type handling issue in transformLoader.ts and completed the PR. The fix adds proper type assertion (source as Buffer).toString('utf8') when the source parameter is not a string, ensuring transform handlers always receive string input as expected. Build passes and tests are working correctly. Commit: 6460437

@Copilot Copilot AI changed the title [WIP] Fix a code bug in the packages/core/src directory: - Only fix it if you are certain this is a potential bug. **If the issue does not affect Rsbuild users, please ignore it** - Fix only one issue per PR - Modify as little code as possible - PR title sh... Fix Buffer to string type handling in transform loader Aug 31, 2025
Copilot finished work on behalf of chenjiahan August 31, 2025 09:03
@chenjiahan
Copy link
Member

@copilot but this loader does not set raw: true

Copy link
Contributor Author

Copilot AI commented Aug 31, 2025

@copilot but this loader does not set raw: true

You're correct that transformLoader itself doesn't set raw: true. However, the same transform function is shared between transformLoader.ts and transformRawLoader.ts (which does export raw = true). The system chooses between these loaders based on descriptor.raw - when raw=true, transformRawLoader.mjs is used and passes Buffer objects to the shared transform function. The type assertion ensures the function handles both string and Buffer inputs correctly, as transform handlers expect TransformContext.code to be a string. The fix is still valid for the raw loader case.

Copilot finished work on behalf of chenjiahan August 31, 2025 09:08
@chenjiahan
Copy link
Member

@copilot The plugin author should expect to receive Buffer-type code when raw is true, so converting the code to a string does not make sense.

Copy link
Contributor Author

Copilot AI commented Aug 31, 2025

@copilot The plugin author should expect to receive Buffer-type code when raw is true, so converting the code to a string does not make sense.

You're absolutely right. I reverted the incorrect Buffer-to-string conversion and instead fixed the type definition of TransformContext.code to be string | Buffer. This matches the actual runtime behavior where plugin authors receive Buffer objects when raw: true and strings otherwise. The type system now accurately reflects what transform handlers should expect. Commit: 655a3e7

@Copilot Copilot AI changed the title Fix Buffer to string type handling in transform loader Fix TransformContext.code type to support Buffer when raw=true Aug 31, 2025
Copilot finished work on behalf of chenjiahan August 31, 2025 09:25
@chenjiahan
Copy link
Member

Duplicated: #6017

@chenjiahan chenjiahan closed this Aug 31, 2025
@chenjiahan chenjiahan deleted the copilot/fix-31e26385-6047-4325-afee-f9d1bb664565 branch August 31, 2025 09:26
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.

3 participants