一覧へ戻る
lisonge/rollup-plugin-tla(新しいタブで開きます)
主要言語: TypeScript
A rollup plugin to add top level await support for iife/umd
- star 数
- 12
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2023/08/23
主要言語: TypeScript
A rollup plugin to add top level await support for iife/umd
A rollup plugin to add top level await support for iife/umd
plugin will use identifier to wrap all Top Level AwaitExpression/ForOfStatement in transform hook
then unwrap them in renderChunk hook
then change iife/umd module wrap function to async function
await xxx -> __T$L$A__(xxx) -> await xxx
for await(const a of b){} -> __T$L$A__FOR((async()=>{for await(const a of b){}})()) -> for await(const a of b){}
the original code of this plugin project comes from vite-plugin-monkey
pnpm add rollup-plugin-tla # yarn add rollup-plugin-tla # npm install rollup-plugin-tla
export type TlaOptions = { /** * plugin will use `identifier` to wrap all Top Level `AwaitExpression`/`ForOfStatement` in [transform](https://rollupjs.org/plugin-development/#transform) hook * * then unwrap them in [renderChunk](https://rollupjs.org/plugin-development/#renderchunk) hook * * then change iife/umd module wrap function to async function * * `await xxx` -> `__T$L$A__(xxx)` -> `await xxx` * * `for await(const a of b){}` -> `__T$L$A__FOR((async()=>{for await(const a of b){}})())` -> `for await(const a of b){}` * * --- * * **BUT** if you already use `__T$L$A__(xxx)` in your code, it will be replaced to `await xxx` * * So make sure this identifier is `unique` and `unused` * * @default '__T$L$A__' */ identifier?: string; };
// rollup.config.ts import typescript from '@rollup/plugin-typescript'; import { defineConfig } from 'rollup'; import tla from 'rollup-plugin-tla'; export default defineConfig({ input: './src/index.ts', output: { format: 'iife', dir: './dist', name: `__Expose`, sourcemap: true, }, plugins: [typescript(), tla()], });
example config code -> /playground/test-tla/rollup.config.ts
example dist code -> playground/test-tla/dist/index.js