Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 31, 2025

The TransformContext type has been enhanced with conditional typing to provide better type safety based on the raw option in transform descriptors.

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

api.transform({ test: /\.node$/, raw: true }, ({ code }) => {
  // code was typed as string | Buffer, requiring runtime checks
  if (Buffer.isBuffer(code)) {
    emitFile(name, code); // Type assertion needed
  }
});

Solution:
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 - no runtime checks needed!
  emitFile(name, code); // TypeScript knows this is safe
});

// When raw is omitted or false, code is definitively string
api.transform({ test: /\.js$/ }, ({ code }) => {
  // code is typed as string - type-safe string operations
  return code.toUpperCase(); // No type assertions needed
});

Type System:

  • TransformContext<true>code: Buffer (raw transforms)
  • TransformContext<false>code: string (non-raw transforms)
  • TransformContext (default) → code: string (backward compatible)

This enhancement eliminates the need for runtime type checking while maintaining full backward compatibility with existing transform handlers.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@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 79fbde4
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/68b4135379d5e60008b28dc0
😎 Deploy Preview https://deploy-preview-6017--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: 78 (🟢 up 3 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

The TransformContext.code type now supports both string and Buffer to handle raw transforms properly. This fixes typing issues when using raw loaders like in the nodeAddons plugin.

Co-authored-by: chenjiahan <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 31, 2025

@copilot finish this PR

✅ PR completed! Fixed the TransformContext.code typing issue to support both string | Buffer for raw loader compatibility. The fix ensures proper type safety when using raw transforms like in the nodeAddons plugin. All tests pass and existing functionality remains intact.

Changes made in commit 8ad5d9a:

  • Updated TransformContext type to accept string | Buffer for the code field
  • Added comprehensive tests validating both raw and non-raw transform scenarios
  • Verified the fix works with existing plugins like nodeAddons

@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 TransformContext typing to support Buffer for raw loaders Aug 31, 2025
Copilot finished work on behalf of chenjiahan August 31, 2025 09:05
*/
code: string;
code: string | Buffer;
Copy link
Member

Choose a reason for hiding this comment

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

@copilot can we ensure that code is string when raw option is not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ Implemented conditional typing for TransformContext! Now code is properly typed as string when raw is not used and Buffer when raw: true.

The typing now provides:

  • TransformContext<true>code: Buffer (raw transforms)
  • TransformContext<false>code: string (non-raw transforms)
  • TransformContext (default) → code: string (backward compatible)

This ensures type safety and eliminates the need for runtime type checking in transform handlers. Fixed in commit 79fbde4.

@Copilot Copilot AI changed the title Fix TransformContext typing to support Buffer for raw loaders Enhance TransformContext typing with conditional types for better type safety Aug 31, 2025
@Copilot Copilot AI requested a review from chenjiahan August 31, 2025 09:19
Copilot finished work on behalf of chenjiahan August 31, 2025 09:19
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