Skip to content

feat: add transformImport feature for module path conversion#32

Merged
xierenyuan merged 2 commits intomainfrom
feat/add-transform-import-feature
Jul 30, 2025
Merged

feat: add transformImport feature for module path conversion#32
xierenyuan merged 2 commits intomainfrom
feat/add-transform-import-feature

Conversation

@xierenyuan
Copy link
Contributor

@xierenyuan xierenyuan commented Jul 30, 2025

close #31

Summary by CodeRabbit

  • New Features

    • Added support for configurable import transformation, enabling modular imports and optional style inclusion for libraries such as antd.
    • Introduced a new plugin to manage import transformations based on user configuration.
  • Chores

    • Updated package dependencies and scripts to support prebundling and modular import handling.
    • Added configuration files to customize the prebundle process and external module resolution.
    • Updated ignore patterns to exclude specific directories from version control.
  • Bug Fixes

    • None.
  • Documentation

    • None.

Copilot AI review requested due to automatic review settings July 30, 2025 02:32
@coderabbitai
Copy link

coderabbitai bot commented Jul 30, 2025

Walkthrough

A modular import transformation feature was integrated into the build system. This includes adding a Babel plugin for import transformation, updating configuration files to support the new transformImport option, and extending package scripts and dependencies. Type declarations and prebundle configurations were also introduced or updated.

Changes

Cohort / File(s) Change Summary
Ignore Pattern Update
.gitignore
Added .takumi/todos to ignore list.
Ant-Pro Example Config
examples/ant-pro/config/config.ts
Added transformImport array property to exported config, specifying import transformation for 'antd'.
Babel Plugin Import Integration
packages/preset-bundler/compiled/babel-plugin-import/index.js,
.../index.d.ts,
.../package.json
Added Babel plugin implementation for import transformation, its type declaration, and package metadata.
Bundler Package Configuration
packages/preset-bundler/package.json
Added prebundle script, new dependency on @kmijs/bundler-compiled, and devDependency on babel-plugin-import.
Prebundle Tool Config
packages/preset-bundler/prebundle.config.mjs
Added prebundle configuration to handle babel-plugin-import and external module remapping.
Transform Import Feature
packages/preset-bundler/src/features/transformImport/transformImport.ts
Introduced a feature plugin for configurable import transformation, including schema validation, Babel plugin injection, and SWC loader option modification.
Bundler Plugin Registration
packages/preset-bundler/src/index.ts
Registered the new transformImport feature plugin in the exported plugin list.

Sequence Diagram(s)

sequenceDiagram
    participant UserConfig as User Config
    participant Bundler as Bundler Plugin Loader
    participant TransformImport as transformImport Feature
    participant Babel as Babel Plugin System

    UserConfig->>Bundler: Provide config with transformImport array
    Bundler->>TransformImport: Initialize with config
    TransformImport->>TransformImport: Validate schema
    TransformImport->>Babel: Register babel-plugin-import with options
    Babel->>TransformImport: Transform import statements per config
    TransformImport->>Bundler: Return modified imports for build
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Assessment against linked issues

Objective Addressed Explanation
Support transformImport configuration at the top level for modular import transformation (#31)
Integrate import transformation for libraries as described in the example (#31)
Ensure configuration schema validation and plugin registration for transformImport (#31)

Poem

In the garden of code, new imports now bloom,
Transforming with Babel, dispelling old gloom.
With scripts and configs, the bundler hops on,
Modular and tidy, the clutter is gone!
🐇✨
Now rabbits can rest, their imports in tune,
For the codebase is ready—review coming soon!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/add-transform-import-feature

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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 a new transformImport feature to the preset-bundler package that enables module path conversion for selective imports, similar to the functionality provided by babel-plugin-import. The feature allows users to transform import statements to optimize bundle size by importing only specific components and their styles from libraries.

Key Changes:

  • Adds a new transformImport configuration option with support for library-specific import transformations
  • Integrates babel-plugin-import for Babel-based builds and rspack experiments for rspack builds
  • Includes comprehensive configuration schema with options for custom naming, style imports, and path transformations

Reviewed Changes

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

Show a summary per file
File Description
packages/preset-bundler/src/index.ts Registers the new transformImport feature plugin
packages/preset-bundler/src/features/transformImport/transformImport.ts Core implementation with configuration schema and Babel/rspack integration
packages/preset-bundler/prebundle.config.mjs Prebundle configuration for babel-plugin-import dependency
packages/preset-bundler/package.json Adds required dependencies and prebundle script
packages/preset-bundler/compiled/babel-plugin-import/* Compiled babel-plugin-import files
examples/ant-pro/config/config.ts Example configuration demonstrating antd usage
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

…mport.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bugbot free trial expires on August 4, 2025
Learn more in the Cursor dashboard.

style,
transformToDefaultImport,
}

Copy link

Choose a reason for hiding this comment

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

Bug: Import Transformation Configuration Issues

The transformImport feature has the following issues:

  • The styleLibraryDirectory option is defined in types and schema but is not destructured or passed to the babel-plugin-import options, causing it to be ignored.
  • The api.config.rspack check incorrectly determines the active bundler, potentially skipping babel plugins when webpack is in use but an rspack config object exists.
  • api.config.transformImport is used with .map() without a null/undefined check, which could lead to a TypeError if the config is not an array.
Locations (1)
Fix in Cursor Fix in Web

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
packages/preset-bundler/src/features/transformImport/transformImport.ts (1)

66-67: Fix typo in description

🧹 Nitpick comments (3)
packages/preset-bundler/compiled/babel-plugin-import/package.json (1)

1-1: Reformat for better readability.

The package.json content is correct, but the single-line format makes it difficult to read and maintain.

Consider reformatting for better readability:

-{"name":"babel-plugin-import","author":"chencheng <sorrycc@gmail.com>","version":"1.13.8","license":"MIT","types":"index.d.ts","type":"commonjs"}
+{
+  "name": "babel-plugin-import",
+  "author": "chencheng <sorrycc@gmail.com>",
+  "version": "1.13.8",
+  "license": "MIT",
+  "types": "index.d.ts",
+  "type": "commonjs"
+}
packages/preset-bundler/compiled/babel-plugin-import/index.d.ts (1)

1-1: Consider improving type definitions if possible.

The generic any type export provides no type safety benefits. While this may be appropriate for a compiled external dependency, consider adding more specific type definitions if the babel-plugin-import API is well-documented.

If the plugin API is known, consider providing more specific types:

-export = any;
+interface TransformImportOptions {
+  libraryName: string;
+  libraryDirectory?: string;
+  styleLibraryDirectory?: string;
+  style?: boolean | string;
+  camelToDashComponentName?: boolean;
+}
+
+declare const plugin: (options: TransformImportOptions) => any;
+export = plugin;
packages/preset-bundler/compiled/babel-plugin-import/index.js (1)

1-481: Consider excluding compiled files from review

This is a compiled/bundled file generated by webpack. Any issues should be addressed in the source code rather than the compiled output. Consider adding compiled directories to your PR review exclusions.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3e2582e and b72e855.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • .gitignore (1 hunks)
  • examples/ant-pro/config/config.ts (1 hunks)
  • packages/preset-bundler/compiled/babel-plugin-import/index.d.ts (1 hunks)
  • packages/preset-bundler/compiled/babel-plugin-import/index.js (1 hunks)
  • packages/preset-bundler/compiled/babel-plugin-import/package.json (1 hunks)
  • packages/preset-bundler/package.json (2 hunks)
  • packages/preset-bundler/prebundle.config.mjs (1 hunks)
  • packages/preset-bundler/src/features/transformImport/transformImport.ts (1 hunks)
  • packages/preset-bundler/src/index.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/preset-bundler/src/features/transformImport/transformImport.ts (2)
packages/preset-bundler/src/index.ts (1)
  • api (3-28)
packages/types/src/api.ts (1)
  • IApi (20-41)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: lint
  • GitHub Check: build-and-test (22.x, windows-latest)
  • GitHub Check: build-and-test (22.x, ubuntu-latest)
🔇 Additional comments (6)
.gitignore (1)

71-71: LGTM!

The addition of .takumi/todos to the gitignore is appropriate and follows the existing pattern. This will help keep development-related todo files out of version control.

packages/preset-bundler/src/index.ts (1)

20-20: LGTM!

The integration of the transformImport feature plugin follows the established pattern and is placed appropriately in the plugins array. The path resolution is consistent with other feature plugins.

examples/ant-pro/config/config.ts (1)

161-167: LGTM!

The transformImport configuration demonstrates proper usage of the new feature. The configuration structure with libraryName, libraryDirectory, and style properties aligns with the babel-plugin-import API and addresses the requirements from issue #31.

packages/preset-bundler/package.json (1)

32-32: LGTM!

The package.json changes properly integrate the new dependencies and prebundle script needed for the transformImport feature.

Also applies to: 37-37, 49-49

packages/preset-bundler/prebundle.config.mjs (1)

1-12: LGTM!

The prebundle configuration correctly sets up babel-plugin-import with appropriate external mappings.

packages/preset-bundler/src/features/transformImport/transformImport.ts (1)

122-130: LGTM!

The SWC loader options modification correctly integrates transformImport configuration for rspack builds.

Comment on lines +79 to +94
const {
libraryName,
libraryDirectory,
camelToDashComponentName,
style,
transformToDefaultImport,
customName,
customStyleName,
} = item
const importOpts: Record<string, any> = {
libraryName,
libraryDirectory,
camel2DashComponentName: camelToDashComponentName,
style,
transformToDefaultImport,
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Missing styleLibraryDirectory in babel plugin options

The styleLibraryDirectory is extracted from the configuration but not passed to the babel plugin options.

Apply this diff to include the missing property:

       const {
         libraryName,
         libraryDirectory,
         camelToDashComponentName,
         style,
+        styleLibraryDirectory,
         transformToDefaultImport,
         customName,
         customStyleName,
       } = item
       const importOpts: Record<string, any> = {
         libraryName,
         libraryDirectory,
         camel2DashComponentName: camelToDashComponentName ?? true,
         style,
+        styleLibraryDirectory,
         transformToDefaultImport,
       }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In packages/preset-bundler/src/features/transformImport/transformImport.ts
between lines 79 and 94, the styleLibraryDirectory property is extracted from
the configuration but not included in the importOpts object passed to the babel
plugin. To fix this, add styleLibraryDirectory as a key in the importOpts object
with its corresponding value from the extracted configuration to ensure it is
properly passed to the babel plugin options.

Comment on lines +88 to +94
const importOpts: Record<string, any> = {
libraryName,
libraryDirectory,
camel2DashComponentName: camelToDashComponentName,
style,
transformToDefaultImport,
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix inconsistent property naming

The babel-plugin-import expects camel2DashComponentName but the config schema uses camelToDashComponentName. This inconsistency needs to be addressed.

Apply this diff to fix the property name mapping:

       const importOpts: Record<string, any> = {
         libraryName,
         libraryDirectory,
-        camel2DashComponentName: camelToDashComponentName,
+        camel2DashComponentName: camelToDashComponentName ?? true,
         style,
         transformToDefaultImport,
       }

Note: I've also added the default value of true to match the schema description at line 47.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const importOpts: Record<string, any> = {
libraryName,
libraryDirectory,
camel2DashComponentName: camelToDashComponentName,
style,
transformToDefaultImport,
}
const importOpts: Record<string, any> = {
libraryName,
libraryDirectory,
camel2DashComponentName: camelToDashComponentName ?? true,
style,
transformToDefaultImport,
}
🤖 Prompt for AI Agents
In packages/preset-bundler/src/features/transformImport/transformImport.ts
around lines 88 to 94, the property name camel2DashComponentName is incorrectly
assigned from camelToDashComponentName, causing inconsistency with the
babel-plugin-import expectation. Rename the property assignment key from
camel2DashComponentName to camelToDashComponentName to match the config schema,
and set its default value to true as described near line 47 to ensure consistent
behavior.

@xierenyuan xierenyuan merged commit 9bfda63 into main Jul 30, 2025
5 checks passed
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.

Unrecognized key(s) "transformImport" in object

2 participants