A Vite plugin that transforms ES modules using top-level await (TLA) into compatible code for browsers that do not natively support TLA. Version 1.6.0 is current, with a moderate release cadence. It works by rewriting imports and exports to use Promise-based orchestration, handling circular dependencies correctly. Unlike setting build.target to esnext, this plugin retains compatibility with Vite's default browser targets. Supports both ES and IIFE worker formats. Notable alternatives include vite-plugin-top-level-await by Menci (this one) and manualPromise-based polyfilling; this plugin is the most popular and actively maintained for Vite ≥2.8.
npm install vite-plugin-top-level-awaitVerified import paths — ran on the pinned version, not inferred.
Basic Vite config using plugin with default options, plus example of top-level await usage in source code.
Place vite-plugin-top-level-await early in the plugins array, after transformations that modify bundle structure.
Ensure no circular dependencies with top-level await, or design code to handle missing exports.
For Firefox support, use IIFE format and follow the documented pattern for worker instantiation.
Change to import statement: import topLevelAwait from 'vite-plugin-top-level-await'
Use default import: import topLevelAwait from 'vite-plugin-top-level-await' (no destructuring)
Install vite: npm install -D vite