Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lisonge/rollup-plugin-tla
A rollup plugin to add top level await support for iife/umd
https://github.com/lisonge/rollup-plugin-tla
await rollup rollup-plugin vite vite-plugin
Last synced: about 1 month ago
JSON representation
A rollup plugin to add top level await support for iife/umd
- Host: GitHub
- URL: https://github.com/lisonge/rollup-plugin-tla
- Owner: lisonge
- License: mit
- Created: 2023-08-22T12:36:24.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-23T12:02:36.000Z (about 1 year ago)
- Last Synced: 2024-10-04T21:56:04.645Z (about 1 month ago)
- Topics: await, rollup, rollup-plugin, vite, vite-plugin
- Language: TypeScript
- Homepage:
- Size: 82 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rollup-plugin-tla
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](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){}`
the original code of this plugin project comes from [vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey/blob/35f56bd76cb426aeab115eda1d8e7c5df1457c5b/packages/vite-plugin-monkey/src/node/topLevelAwait.ts)
## Installation
```shell
pnpm add rollup-plugin-tla
# yarn add rollup-plugin-tla
# npm install rollup-plugin-tla
```## Usage
```ts
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;
};
``````ts
// 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](/playground/test-tla/rollup.config.ts)
example dist code -> [playground/test-tla/dist/index.js](playground/test-tla/dist/index.js)