Skip to content

Commit

Permalink
Revert "feat(bun): remove mjs path resolution"
Browse files Browse the repository at this point in the history
This reverts commit 073d86b.
  • Loading branch information
ryoppippi committed Jan 27, 2025
1 parent 61c5481 commit bd5e5dc
Showing 1 changed file with 19 additions and 2 deletions.
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() };
});
}
},
}) as const satisfies BunPlugin;

Expand Down

0 comments on commit bd5e5dc

Please sign in to comment.