Skip to content

Commit

Permalink
Merge pull request #56 from ryoppippi/feature/impvore-bun
Browse files Browse the repository at this point in the history
feat(unplugin-typia): add Bun.build examples to README and bun.ts/ enable cache for bun.ts
  • Loading branch information
ryoppippi authored Jun 8, 2024
2 parents 21bf51f + fa48046 commit 004c16a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
9 changes: 9 additions & 0 deletions examples/bun-build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import UnpluginTypia from 'unplugin-typia/bun'

await Bun.build({
entrypoints: ["./index.ts"],
outdir: "./out",
plugins: [
UnpluginTypia()
]
});
3 changes: 2 additions & 1 deletion examples/bun-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
55 changes: 55 additions & 0 deletions packages/unplugin-typia/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,61 @@ Examples:

<br></details>

<details>
<summary>Bun.build</summary><br>

### 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.

<br></details>

<details>
<summary>Rollup</summary><br>

Expand Down
52 changes: 41 additions & 11 deletions packages/unplugin-typia/src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -54,16 +83,17 @@ function bunTypiaPlugin(

const source = await Bun.file(path).text();

const code = await transformTypia(
path,
source,
{ warn: console.warn } as Parameters<typeof transformTypia>[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;
Expand Down

0 comments on commit 004c16a

Please sign in to comment.