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