npm package: @kennethwkz/unplugin-typia
Description: unplugin for typia
Author: kennethwkz
Latest version: 2.6.7

# unplugin-typia

**unplugin for [Typia](https://typia.io/)**

[![npm version](https://img.shields.io/npm/v/@kennethwkz/unplugin-typia?color=yellow)](https://npmjs.com/package/@kennethwkz/unplugin-typia)
[![npm downloads](https://img.shields.io/npm/dm/@kennethwkz/unplugin-typia?color=yellow)](https://npmjs.com/package/@kennethwkz/unplugin-typia)
[![DeepWiki](https://img.shields.io/badge/DeepWiki-kennethwkz%2Funplugin--typia-blue.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAyCAYAAAAnWDnqAAAAAXNSR0IArs4c6QAAA05JREFUaEPtmUtyEzEQhtWTQyQLHNak2AB7ZnyXZMEjXMGeK/AIi+QuHrMnbChYY7MIh8g01fJoopFb0uhhEqqcbWTp06/uv1saEDv4O3n3dV60RfP947Mm9/SQc0ICFQgzfc4CYZoTPAswgSJCCUJUnAAoRHOAUOcATwbmVLWdGoH//PB8mnKqScAhsD0kYP3j/Yt5LPQe2KvcXmGvRHcDnpxfL2zOYJ1mFwrryWTz0advv1Ut4CJgf5uhDuDj5eUcAUoahrdY/56ebRWeraTjMt/00Sh3UDtjgHtQNHwcRGOC98BJEAEymycmYcWwOprTgcB6VZ5JK5TAJ+fXGLBm3FDAmn6oPPjR4rKCAoJCal2eAiQp2x0vxTPB3ALO2CRkwmDy5WohzBDwSEFKRwPbknEggCPB/imwrycgxX2NzoMCHhPkDwqYMr9tRcP5qNrMZHkVnOjRMWwLCcr8ohBVb1OMjxLwGCvjTikrsBOiA6fNyCrm8V1rP93iVPpwaE+gO0SsWmPiXB+jikdf6SizrT5qKasx5j8ABbHpFTx+vFXp9EnYQmLx02h1QTTrl6eDqxLnGjporxl3NL3agEvXdT0WmEost648sQOYAeJS9Q7bfUVoMGnjo4AZdUMQku50McDcMWcBPvr0SzbTAFDfvJqwLzgxwATnCgnp4wDl6Aa+Ax283gghmj+vj7feE2KBBRMW3FzOpLOADl0Isb5587h/U4gGvkt5v60Z1VLG8BhYjbzRwyQZemwAd6cCR5/XFWLYZRIMpX39AR0tjaGGiGzLVyhse5C9RKC6ai42ppWPKiBagOvaYk8lO7DajerabOZP46Lby5wKjw1HCRx7p9sVMOWGzb/vA1hwiWc6jm3MvQDTogQkiqIhJV0nBQBTU+3okKCFDy9WwferkHjtxib7t3xIUQtHxnIwtx4mpg26/HfwVNVDb4oI9RHmx5WGelRVlrtiw43zboCLaxv46AZeB3IlTkwouebTr1y2NjSpHz68WNFjHvupy3q8TFn3Hos2IAk4Ju5dCo8B3wP7VPr/FGaKiG+T+v+TQqIrOqMTL1VdWV1DdmcbO8KXBz6esmYWYKPwDL5b5FA1a0hwapHiom0r/cKaoqr+27/XcrS5UwSMbQAAAABJRU5ErkJggg==)](https://deepwiki.com/kennethwkz/unplugin-typia)

<!-- DeepWiki badge generated by https://deepwiki.kennethwkz.com/ -->

## Why

Typia is fantastic, but it is hard to setup, even for frontend development.
If you use some bundlers for frontend like `Vite`, it is even harder to setup.
This unplugin aims to make it easier to use Typia in your projects.

## Install

First, install `unplugin-typia`:

```bash
# npm
npm install -D @kennethwkz/unplugin-typia
```

Then, install `typia`:

```bash
# install typia! (nypm detects your PM ✨)
npx nypm add typia

# setup typia!
npx typia setup
# pnpm dlx typia setup
# yarn dlx typia setup

# after installing typia, run prepare script
npm run prepare
```

More details about setting up Typia can be found in the [Typia Docs](https://typia.io/docs/setup/#unplugin-typia).

Then, add the unplugin to your favorite bundler:

<details>
<summary>Vite</summary><br>

```ts
// vite.config.ts
import UnpluginTypia from '@kennethwkz/unplugin-typia/vite';

export default defineConfig({
	plugins: [
		UnpluginTypia({ tsconfig: './tsconfig.app.json' /* options */ }), // should be placed before other plugins like `react`, `svetle`, etc.
	],
});
```

> When using typia with types imported from non-relative paths like tsconfig `compilerOptions.paths` or relative to
> tsconfig `compilerOptions.baseUrl`, they must be defined in vite.config.ts under [resolve.alias](https://vitejs.dev/config/shared-options#resolve-alias)
> in order to be resolved, according to vite's resolution mechanism.

Examples:

- [`examples/vite-react`](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples/vite-react)
- [`examples/vite-hono`](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples/vite-hono)
- [`examples/sveltekit`](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples/sveltekit)

<br></details>

<details>
<summary>esbuild</summary><br>

```ts
// esbuild.config.js
import UnpluginTypia from '@kennethwkz/unplugin-typia/esbuild';

export default {
	plugins: [
		UnpluginTypia({ /* options */ }),
	],
};
```

Examples:

- [`tests/rollup.spec.ts`](https://github.com/KennethWKZ/unplugin-typia/tree/main/packages/unplugin-typia/tests/esbuild.spec.ts)

<br></details>

<details>
<summary>Next.js</summary><br>

```js
// next.config.mjs
import unTypiaNext from 'unplugin-typia/next';

/** @type {import('next').NextConfig} */
const nextConfig = { /* your next.js config */};

/** @type {import("unplugin-typia").Options} */
const unpluginTypiaOptions = { /* your unplugin-typia options */ };

export default unTypiaNext(nextConfig, unpluginTypiaOptions);

// you can omit the unplugin-typia options when you don't need to customize it
// export default unTypiaNext(nextConfig);
```

Examples:

- [`examples/nextjs`](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples/nextjs)

<br></details>

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

### Example 1: Using for building script

```ts
// build.ts
import UnpluginTypia from '@kennethwkz/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.

### Example 2: Using for running script

```ts
// preload.ts
import { plugin } from 'bun';
import UnpluginTypia from '@kennethwkz/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.

<br></details>

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

```ts
// rollup.config.js
import UnpluginTypia from '@kennethwkz/unplugin-typia/rollup';

export default {
	plugins: [
		UnpluginTypia({ /* options */ }),
	],
};
```

Examples:

- [`tests/rollup.spec.ts`](https://github.com/KennethWKZ/unplugin-typia/tree/main/packages/unplugin-typia/tests/rollup.spec.ts)

<br></details>

<details>
<summary>Webpack</summary><br>

```js
// webpack.config.js

const { default: UnpluginTypia } = require('@kennethwkz/unplugin-typia/webpack');

module.exports = {
	plugins: [
		UnpluginTypia({ /* options */ }),
	],
};
```

<br></details>

You can find examples in the [`examples/`](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples).

## Supported File Extensions

- `.ts`
- `.tsx`
- `.mts`
- `.mtsx`
- `.svelte` (only script tag with `lang="ts"`)

## Development

This repository is a monorepo managed by [Bun](https://bun.sh).

- [unplugin-typia](https://github.com/KennethWKZ/unplugin-typia/tree/main/packages/unplugin-typia)
- [examples](https://github.com/KennethWKZ/unplugin-typia/tree/main/examples)

```sh
bun i --frozen-lockfile
```

## Acknowledgements

This project started as an unofficial integration of Typia for bundlers.
Now, this plugin is one of the official integrations of Typia and is sponsored by [@samchon](https://github.com/samchon) (the creator of Typia).

## LICENSE

[MIT](./LICENSE)

![analytics](https://repobeats.axiom.co/api/embed/30d90d6f9ab91e8b06159ba792765a1377ea7d3e.svg 'Repobeats analytics image')