Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(bun): remove mjs path resolution" #347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ryoppippi
Copy link
Owner

@ryoppippi ryoppippi commented Dec 3, 2024

This reverts commit 073d86b.

after samchon/typia#1413 is merged, merge this PR

Summary by CodeRabbit

  • New Features
    • Enhanced the plugin to support an optional property for controlling the import format of typia, improving compatibility with Bun's module handling.
  • Bug Fixes
    • Addressed compatibility issues with typia's CommonJS package by allowing for dynamic path adjustments from .js to .mjs.

Copy link

vercel bot commented Dec 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unplugin-typia-nextjs ⬜️ Ignored (Inspect) Dec 3, 2024 10:42pm

Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The changes involve modifications to the BunOptions type and the bunTypiaPlugin function in the packages/unplugin-typia/src/bun.ts file. The BunOptions type now includes an optional boolean property forceImportTypiaMjs, which defaults to true. This addition allows for better handling of typia's module format, particularly regarding Bun's compatibility with CommonJS packages. The bunTypiaPlugin function has been updated to incorporate this new property, enabling conditional loading of .mjs files based on its value.

Changes

File Change Summary
packages/unplugin-typia/src/bun.ts Updated BunOptions type to include optional forceImportTypiaMjs boolean property. Modified bunTypiaPlugin function to handle forceImportTypiaMjs and conditionally set up an onLoad handler for .mjs files.

Poem

In the realm of code where rabbits hop,
A new option blooms, making plugins pop.
With forceImportTypiaMjs in sight,
Our paths now gleam, oh what a delight!
From .js to .mjs, we leap with glee,
In Bun's warm embrace, we're wild and free! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 04fac5c and f09d9a6.

📒 Files selected for processing (1)
  • packages/unplugin-typia/src/bun.ts (3 hunks)
🔇 Additional comments (2)
packages/unplugin-typia/src/bun.ts (2)

152-161: Verify dependency on typia#1413.

Since this is a revert PR dependent on typia#1413, we should ensure that PR is merged and the revert is still necessary.

Let's check the status of the dependent PR:


17-24: LGTM! Well-documented type extension.

The type definition clearly explains the purpose of forceImportTypiaMjs and the CommonJS compatibility issue it addresses.

Let's verify if typia's package.json indeed has "type": "commonjs":

✅ Verification successful

The forceImportTypiaMjs option is correctly implemented and documented

The implementation aligns with the documented behaviour:

  • The option defaults to true as stated in the type definition
  • It's used in the Bun plugin to transform typia's .js imports to .mjs
  • The code handles the module resolution issue by intercepting loads from typia/lib and replacing the extensions

Even though we couldn't directly verify typia's package.json, the implementation and usage context provide sufficient evidence that this is addressing a real compatibility issue between Bun and typia's module system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify typia's package.json type field
rg '"type":\s*"commonjs"' node_modules/typia/package.json

Length of output: 131


Script:

#!/bin/bash
# Let's check if typia is a direct dependency and find its package.json
fd package.json --type f --exec grep -l "typia" {}

# Also search for any direct references to typia's package.json type field in the codebase
rg -i "typia.*package\.json.*type.*commonjs"

Length of output: 508


Script:

#!/bin/bash
# Let's check the actual implementation to understand how this option is used
ast-grep --pattern 'forceImportTypiaMjs'

# Also check if there are any related test cases or examples
rg 'forceImportTypiaMjs' -A 3

Length of output: 996

Comment on lines +152 to +161

/** if input is ./node_modules/typia/lib/*, convert js to mjs */
if (forceImportTypiaMjs) {
build.onLoad({ filter: /.+\/node_modules\/typia\/lib\/.*\.js$/ }, async (args) => {
const { path } = args;
const mjsPath = path.replace(/\.js$/, '.mjs');

return { contents: await Bun.file(mjsPath).text() };
});
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding error handling for missing .mjs files.

The current implementation assumes the .mjs file will always exist. Consider adding error handling to gracefully handle cases where the .mjs file is missing.

Here's a suggested improvement:

 if (forceImportTypiaMjs) {
   build.onLoad({ filter: /.+\/node_modules\/typia\/lib\/.*\.js$/ }, async (args) => {
     const { path } = args;
     const mjsPath = path.replace(/\.js$/, '.mjs');
-
-    return { contents: await Bun.file(mjsPath).text() };
+    try {
+      const file = Bun.file(mjsPath);
+      if (await file.exists()) {
+        return { contents: await file.text() };
+      }
+      console.warn(`MJS file not found: ${mjsPath}, falling back to JS`);
+      return { contents: await Bun.file(path).text() };
+    } catch (error) {
+      console.error(`Error loading file: ${mjsPath}`, error);
+      throw error;
+    }
   });
 }
📝 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
/** if input is ./node_modules/typia/lib/*, convert js to mjs */
if (forceImportTypiaMjs) {
build.onLoad({ filter: /.+\/node_modules\/typia\/lib\/.*\.js$/ }, async (args) => {
const { path } = args;
const mjsPath = path.replace(/\.js$/, '.mjs');
return { contents: await Bun.file(mjsPath).text() };
});
}
/** if input is ./node_modules/typia/lib/*, convert js to mjs */
if (forceImportTypiaMjs) {
build.onLoad({ filter: /.+\/node_modules\/typia\/lib\/.*\.js$/ }, async (args) => {
const { path } = args;
const mjsPath = path.replace(/\.js$/, '.mjs');
try {
const file = Bun.file(mjsPath);
if (await file.exists()) {
return { contents: await file.text() };
}
console.warn(`MJS file not found: ${mjsPath}, falling back to JS`);
return { contents: await Bun.file(path).text() };
} catch (error) {
console.error(`Error loading file: ${mjsPath}`, error);
throw error;
}
});
}

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.

1 participant