Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wjchumble/vite-plugin-async-catch
A vite plugin that can auto inject try catch code for async function.
https://github.com/wjchumble/vite-plugin-async-catch
async-function try-catch vite-plugin
Last synced: 4 months ago
JSON representation
A vite plugin that can auto inject try catch code for async function.
- Host: GitHub
- URL: https://github.com/wjchumble/vite-plugin-async-catch
- Owner: WJCHumble
- License: mit
- Created: 2021-05-15T05:02:58.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-19T14:08:54.000Z (over 3 years ago)
- Last Synced: 2024-10-09T08:08:16.531Z (4 months ago)
- Topics: async-function, try-catch, vite-plugin
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/vite-plugin-async-catch
- Size: 180 KB
- Stars: 12
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README-CN.md
- License: LICENSE
Awesome Lists containing this project
README
## vite-plugin-async-catch
一个可以自动为 `async` 函数注入 `try catch` 代码的 Vite 插件。Inspried by [async-catch-loader](https://github.com/yeyan1996/async-catch-loader).
支持在以下类型的项目中使用:
- vue3
- vue2
- react
- vannila js## 使用
安装:
```bash
npm i vite-plugin-async-catch -D
```添加插件到 `vite.config.ts`(或者 `vite.config.js`)。
在 Vue3 项目中:
```typescript
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AsyncCatch from "vite-plugin-async-catch";export default defineConfig({
plugins: [
vue(),
AsyncCatch({
catchCode: `console.error(e)`,
}),
],
});
```在 Vue2 项目中:
```javascript
import { defineConfig } from "vite";
import { createVuePlugin } from "vite-plugin-vue2";
import AsyncCatch from "vite-plugin-async-catch";export default defineConfig({
plugins: [
createVuePlugin(),
AsyncCatch({
catchCode: `console.error(e)`,
}),
],
});
```在 React 项目中:
```typescript
import reactRefresh from "@vitejs/plugin-react-refresh";
import { defineConfig } from "vite";
import AsyncCatch from "vite-plugin-async-catch";export default defineConfig({
plugins: [
reactRefresh(),
AsyncCatch({
catchCode: `console.error(e)`,
}),
],
});
```## Options
`Options` 的类型定义:
```typescript
interface Options {
catchCode: string;
identifier?: string;
finnallyCode?: any;
}
```其中,每个参数的意义:
| 名称 | 介绍 | 类型 | 默认值 |
| ------------ | ----------------------- | ------ | ---------------- |
| catchCode | 在 catch 代码块中使用 | string | console.error(e) |
| identifier | catch 代码块的错误参数 | string | e |
| finnallyCode | 在 finally 代码块中使用 | string | null |