diff --git a/examples/bun-build/build.ts b/examples/bun-build/build.ts new file mode 100644 index 00000000..5c6c0529 --- /dev/null +++ b/examples/bun-build/build.ts @@ -0,0 +1,9 @@ +import UnpluginTypia from 'unplugin-typia/bun' + +await Bun.build({ + entrypoints: ["./index.ts"], + outdir: "./out", + plugins: [ + UnpluginTypia() + ] +}); diff --git a/examples/bun-build/package.json b/examples/bun-build/package.json index 1376196d..8ce2e149 100644 --- a/examples/bun-build/package.json +++ b/examples/bun-build/package.json @@ -4,7 +4,8 @@ "type": "module", "scripts": { "prepare": "ts-patch install && typia patch", - "test": "bun run ./index.ts" + "test": "bun run ./index.ts", + "build": "bun run ./build.ts" }, "devDependencies": { "@types/bun": "latest", diff --git a/packages/unplugin-typia/README.md b/packages/unplugin-typia/README.md index a84a8c04..7c0dc760 100644 --- a/packages/unplugin-typia/README.md +++ b/packages/unplugin-typia/README.md @@ -109,6 +109,61 @@ Examples:
+
+Bun.build
+ +### Example 1: Using for running script + +```ts +// preload.ts +import { plugin } from 'bun'; +import UnpluginTypia from 'unplugin-typia/bun'; + +plugin(UnpluginTypia({ /* your options */})); +``` + +```toml +# bun.toml +preload = "preload.ts" + +[test] +preload = "preload.ts" +``` + +For running the script: + +```sh +bun run ./index.ts +``` + +Check the [Plugins – Runtime | Bun Docs](https://bun.sh/docs/runtime/plugins) for more details. + +### Example 2: Using for building script + +```ts +// build.ts +import UnpluginTypia from 'unplugin-typia/bun'; + +await Bun.build({ + entrypoints: ['./index.ts'], + outdir: './out', + plugins: [ + UnpluginTypia({ /* your options */}) + ] +}); +``` + +For building the script: + +```sh +bun run ./build.ts +node ./out/index.js +``` + +Check the [Plugins – Bundler | Bun Docs](https://bun.sh/docs/bundler/plugins) for more details. + +
+
Rollup
diff --git a/packages/unplugin-typia/src/bun.ts b/packages/unplugin-typia/src/bun.ts index 51b6f434..4b8fd2d9 100644 --- a/packages/unplugin-typia/src/bun.ts +++ b/packages/unplugin-typia/src/bun.ts @@ -5,7 +5,8 @@ */ import type { BunPlugin } from 'bun'; -import { type Options, resolveOptions, transformTypia } from './api.js'; +import type { UnpluginContextMeta } from 'unplugin'; +import { type Options, resolveOptions, unplugin } from './api.js'; import { defaultOptions } from './core/options.js'; if (globalThis.Bun == null) { @@ -17,6 +18,8 @@ if (globalThis.Bun == null) { * * some typia functions does not works because of bun/typia internal implementation. see the [issse](https://github.com/ryoppippi/unplugin-typia/issues/44) * @experimental + * also check out hte [Bun.build docc](https://bun.sh/docs/bundler) + * * @example * ```ts * // preload.ts @@ -26,21 +29,47 @@ if (globalThis.Bun == null) { * plugin(UnpluginTypia({ /* your options *\/})) * ``` * ```toml - * // bunfig.toml + * # bunfig.toml * preload = ["./preload.ts"] * * [test] * preload = ["./preload.ts"] * ``` + * + * @example + * ```ts + * // build.ts + * + * import UnpluginTypia from 'unplugin-typia/bun' + * + * Bun.build({ + * entrypoints: ['./index.ts'], + * outdir: './out', + * plugins: [ + * UnpluginTypia({ /* your options *\/}) + * ] + * }) */ function bunTypiaPlugin( options?: Options, ): BunPlugin { + const unpluginRaw = unplugin.raw( + options, + {} as UnpluginContextMeta, + ); + + const { transform } = unpluginRaw; + + if (transform == null) { + throw new Error('transform is not defined'); + } + const bunPlugin = ({ name: 'unplugin-typia', setup(build) { const resolvedOptions = resolveOptions(options ?? {}); const { include } = resolvedOptions; + const filter = include instanceof RegExp ? include : typeof include === 'string' @@ -54,16 +83,17 @@ function bunTypiaPlugin( const source = await Bun.file(path).text(); - const code = await transformTypia( - path, - source, - { warn: console.warn } as Parameters[2], - resolvedOptions, - ); + // @ts-expect-error type of this function is not correct + const result = await transform(source, path); - return { - contents: code ?? source, - }; + switch (true) { + case result == null: + return { contents: source }; + case typeof result === 'string': + return { contents: source }; + default: + return { contents: result.code }; + } }); }, }) as const satisfies BunPlugin;