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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions packages/unplugin-typia/src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import { type ID, type Source, wrap } from './core/types.js';
/**
* Options for bun plugin
*/
export type BunOptions = Options;
export type BunOptions = {
/**
* Convert path of typia to mjs
* even though typia provides mjs, bun cannot handle it (because typia's package.json has "type": "commonjs").
* @default true
*/
forceImportTypiaMjs?: boolean;
} & Options;

if (!isBun()) {
throw new Error('You must use this plugin with bun');
Expand Down Expand Up @@ -103,7 +110,7 @@ function bunTypiaPlugin(
const bunPlugin = ({
name: 'unplugin-typia',
async setup(build) {
const { ...options } = bunOptions ?? {};
const { forceImportTypiaMjs = true, ...options } = bunOptions ?? {};
const resolvedOptions = resolveOptions(options ?? {});
const { include } = resolvedOptions;

Expand Down Expand Up @@ -142,6 +149,16 @@ function bunTypiaPlugin(
return { contents: code ?? source };
});
}

/** 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() };
});
}
Comment on lines +152 to +161
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;
}
});
}

},
}) as const satisfies BunPlugin;

Expand Down
Loading