From 34f96b9b5f040e0535a56ed7a967b1d68d965703 Mon Sep 17 00:00:00 2001
From: ryoppippi <1560508+ryoppippi@users.noreply.github.com>
Date: Sat, 8 Jun 2024 14:22:46 +0100
Subject: [PATCH 1/3] feat(unplugin-typia): add Bun.build examples to README
and bun.ts
This commit adds two examples of how to use the unplugin-typia with
Bun.build in the README.md and bun.ts files. The first example shows
how to use it for running scripts, and the second one for building
scripts. It also updates the documentation in bun.ts to include these
examples and a link to the Bun.build documentation.
---
packages/unplugin-typia/README.md | 55 ++++++++++++++++++++++++++++++
packages/unplugin-typia/src/bun.ts | 18 +++++++++-
2 files changed, 72 insertions(+), 1 deletion(-)
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..ec538e0e 100644
--- a/packages/unplugin-typia/src/bun.ts
+++ b/packages/unplugin-typia/src/bun.ts
@@ -17,6 +17,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,12 +28,26 @@ 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,
From 96b1c6874f4681caf2320d7160837325ee124374 Mon Sep 17 00:00:00 2001
From: ryoppippi <1560508+ryoppippi@users.noreply.github.com>
Date: Sat, 8 Jun 2024 14:22:58 +0100
Subject: [PATCH 2/3] feat(bun-build): add build script and update package.json
This commit introduces a new build script in the bun-build example.
The script uses UnpluginTypia as a plugin for the Bun build tool.
Additionally, the package.json file has been updated to include a
new "build" script which runs the newly introduced build script.
---
examples/bun-build/build.ts | 9 +++++++++
examples/bun-build/package.json | 3 ++-
2 files changed, 11 insertions(+), 1 deletion(-)
create mode 100644 examples/bun-build/build.ts
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",
From fa48046154173a591f112163f1cfe0a0177201cb Mon Sep 17 00:00:00 2001
From: ryoppippi <1560508+ryoppippi@users.noreply.github.com>
Date: Sat, 8 Jun 2024 17:51:02 +0100
Subject: [PATCH 3/3] feat: Refactor bunTypiaPlugin to use unplugin
This commit refactors the `bunTypiaPlugin` function to use the `unplugin`
module instead of the previous `transformTypia` function. The `transform`
function from `unplugin.raw` is now used to transform the source code.
The return type of the `transform` function is also handled differently.
If the result is `null` or a string, the original source is returned.
Otherwise, the transformed code from the result is returned.
This change improves the flexibility and maintainability of the code by
leveraging the `unplugin` module.
---
packages/unplugin-typia/src/bun.ts | 34 +++++++++++++++++++++---------
1 file changed, 24 insertions(+), 10 deletions(-)
diff --git a/packages/unplugin-typia/src/bun.ts b/packages/unplugin-typia/src/bun.ts
index ec538e0e..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) {
@@ -52,11 +53,23 @@ if (globalThis.Bun == null) {
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'
@@ -70,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;