From bd5e5dc6fb353b9030471cf1659b9105704dc2df Mon Sep 17 00:00:00 2001 From: ryoppippi <1560508+ryoppippi@users.noreply.github.com> Date: Wed, 4 Dec 2024 07:19:57 +0900 Subject: [PATCH] Revert "feat(bun): remove mjs path resolution" This reverts commit 073d86b6646ed4d705f901bea2100abdfc039a48. --- packages/unplugin-typia/src/bun.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/unplugin-typia/src/bun.ts b/packages/unplugin-typia/src/bun.ts index f7d561c0..7e9b8455 100644 --- a/packages/unplugin-typia/src/bun.ts +++ b/packages/unplugin-typia/src/bun.ts @@ -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'); @@ -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; @@ -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;