• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transform code to support top-level await in normal browsers for Vite.

vite-plugin-top-level-await

Test Status npm Commitizen friendly code style: prettier License

Transform code to support top-level await in normal browsers for Vite. Support all modern browsers of Vite's default target without need to set build.target to esnext.

Installation

yarn add -D vite-plugin-top-level-await

Usage

Put this plugin in your plugin list. At most case you don't need to care the order, but if there're any plugin transforming bundle before it, there's a little chance that this plugin fails to parse code since it does only parse Rollup's output export { ... } export statement.

import topLevelAwait from "vite-plugin-top-level-await";

export default defineConfig({
  plugins: [
    topLevelAwait({
      // The export name of top-level await promise for each chunk module
      promiseExportName: "__tla",
      // The function to generate import names of top-level await promise in each chunk module
      promiseImportName: i => `__tla_${i}`
    })
  ]
});

Workers

You can use this plugin for workers (by putting it in config.worker.plugins).

  • If the worker format is ES, the plugin works normally.
  • If the worker format is IIFE, the plugin first let Vite build your worker as an ES bundle since IIFE doesn't support top-level awaits, and then build the transformed ES bundle to IIFE. Please use IIFE when targeting Firefox.

Note

This plugin transforms code from:

import { a } from "./a.js"; // This module uses top-level await
import { b } from "./b.js"; // This module uses top-level await too
import { c } from "./c.js"; // This module does NOT use top-level await

const x = 1;
await b.func();
const { y } = await somePromise;

export { x, y };

To:

import { a, __tla as __tla_0 } from "./a.js"; // This module uses top-level await
import { b, __tla as __tla_1 } from "./b.js"; // This module uses top-level await too
import { c } from "./c.js"; // This module does NOT use top-level await

// Original exported variables
let x, y;

// Await imported TLA promises and execute original top-level statements
let __tla = Promise.all([
  (() => { try { return __tla_0; } catch {} })(),
  (() => { try { return __tla_1; } catch {} })()
]).then(async () => {
  // Transform exported variables to assignments
  x = 1;

  await b.func();

  // Destructing patterns (and function / class declarations as well) are handled correctly
  ({ y } = await somePromise);
});

// Export top-level await promise
export { x, y, __tla };

It could handle correct usage of circular dependencies with the default behavior of ES standard. But when an TLA dependency is being awaited, an accessing to one of its exports will NOT raise an exception. At most time you don't need to care about this. These could be supported by doing more transformations of the whole AST but it will make building a lot slower. Open an issue and tell me your scenario if you really need the exception.

More Repositories

1

vite-plugin-wasm

Add WebAssembly ESM integration (aka. Webpack's `asyncWebAssembly`) to Vite and support `wasm-pack` generated modules.
TypeScript
134
star
2

electron-titlebar

Cool titlebar for electron apps for every system
JavaScript
115
star
3

simple-sandbox

A simple Linux sandbox with Node.js API.
C++
69
star
4

acme

Issue free SSL certs on GitHub Actions with acme.sh.
35
star
5

oi.men.ci

Menci 的 OI 博客(源代码与网站文件)
JavaScript
27
star
6

monaco-tree-sitter

Highlight your Monaco Editor with tree-sitter grammar.
TypeScript
26
star
7

hexo-deployer-upyun

UPYUN deployer for Hexo.
JavaScript
22
star
8

html-parser

The task of Advanced Programming class on 2019/03/28. C++ HTML parser that generates a simple DOM tree.
C++
18
star
9

docker-archlinuxarm

Docker image for Arch Linux (multiarch ARM64 + AMD64, daily updated with GitHub Actions)
Dockerfile
18
star
10

docker-sandbox

Run unsafe program in docker sandbox
C++
18
star
11

homebrew-libvirt-m1

Homebrew formulaes to install `virt-manager`, `virt-viewer` and `libvirtd` on macOS with Apple Silicon
Ruby
16
star
12

magpie

Bidirectional NDP proxy and route maintainer to relay an IPv6 SLAAC network.
C++
16
star
13

wastyle

AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
C++
16
star
14

resume

个人简历
14
star
15

markdown-it-merge-cells

A markdown-it plugin to merge adjacent cells with same content in tables
JavaScript
13
star
16

deploy-certificate-to-aliyun

GitHub Action to Deploy SSL certificate to Aliyun Certificates Service (and use in CDN).
JavaScript
10
star
17

service-worker-redirect-origin

Redirect requests of current origin to another domain with Service Worker.
TypeScript
10
star
18

vite-plugin-public-path

Vite's equivalent of `__webpack_public_path__` in Webpack. Works for `index.html` and modern/legacy build.
TypeScript
9
star
19

search-engine-oop

The task of Advanced Programming class on 2019/04/23. Text search engine using inversed index and TF-IDF in C++.
C++
8
star
20

stego

数据隐写:将数据嵌入到到 BMP 数据中
C
8
star
21

tree-sitter-wasm-prebuilt

Prebuilt WASM binaries for tree-sitter's language parsers.
JavaScript
8
star
22

chroot-sandbox

Run unsafe program in chroot sandbox
C++
8
star
23

aur.men.ci

Menci's prebuilt AUR repo for Archlinux and Manjaro
TypeScript
8
star
24

double

The task of Advanced Programming class on 2018/10/10. Implementation of IEEE 754 floating point number. Like `double` in C.
C
8
star
25

bootloader

操作系统课作业:MBR 引导加载器
Assembly
8
star
26

pipeline

Pipelined MIPS processor in Verilog
SystemVerilog
7
star
27

TuringAdvancedProgramming19A

Code of tasks of Turing Class 2019's Advanced Programming (first term)
C
7
star
28

hexo-renderer-syzoj-renderer

Render Hexo's markdown documents with syzoj-renderer
JavaScript
6
star
29

Menci

My GitHub homepage
6
star
30

SummerProgrammingPractice2020

"Summer Programming Practice 2020" for RUC Turing Class 2019
HTML
6
star
31

Blog

My new blog
TypeScript
6
star
32

moemark-turndown

Convert hexo-renderer-moemark-pygments rendered HTML back to markdown
JavaScript
6
star
33

search-engine

The task of Advanced Programming class on 2019/04/16. Text search engine using inversed index and TF-IDF.
Python
6
star
34

avl-tree

The task of Advanced Programming class on 2018/12/05. Implementation of AVL tree and heap memory leak check in C.
C
5
star
35

FakeTPM.kext

Something you might need to run Windows 11 🤣
5
star
36

build-archlinux-package

GitHub Action to build Archlinux / Manjaro package with Docker, for AMD64 or ARM64
Shell
5
star
37

deploy-certificate-to-upyun

GitHub Action to Deploy SSL certificate to Upyun CDN or OSS.
TypeScript
5
star
38

mepage-2015-archive

很久很久以前(2015 年)设计的个人主页 MePage 的存档
JavaScript
5
star
39

upload-to-oss

GitHub Actions to sync files in a directory to Aliyun OSS with a prefix incrementally, with filename filter.
TypeScript
5
star
40

web-crawler

The task of Advanced Programming class on 2019/03/21. C++ web crawler with wget.
C++
4
star
41

xinu-vm

操作系统课作业 xinu
C
4
star
42

pipeline-tester

Random tests generator & runner for your pipelined MIPS processor (from @t123yh)
C#
4
star
43

avl-tree-zip

The task of Advanced Programming class on 2018/12/26. Compress and decompress an avl-tree with (1 `double` + 1 `char`) for each node, or its insert sequence.
C
3
star
44

men.ci-backup

Menci's personal homepage
3
star
45

syntect-js

Syntect (Syntax highlighter in Rust) for Node.js and WebAssembly
JavaScript
3
star
46

FlappyBird

JavaScript
3
star
47

deploy-certificate-to-azure-web-app

GitHub Action to deploy SSL certificate to Azure Web App (including Function App)
2
star
48

karatsuba-algorithm

The fast big integer multiplication algorithm
C++
2
star
49

luogu-openapi

Luogu OpenAPI SDK (unofficial)
TypeScript
2
star
50

tkpl

The "Ka" Programming Language
2
star
51

ssh-certdeploy

Deploy certificate to servers with SSH.
Shell
2
star
52

big-integer

The task of Advanced Programming class on 2019/04/04. C++ BigInteger class.
C++
2
star
53

arcaea-builder

Build your own Arcaea game client to play self-made or modified official charts!
TypeScript
2
star
54

qsort

The task of Advanced Programming class on 2018/10/10. Implementation of C library function qsort().
C
2
star
55

Tsukasa

A flexible port forwarder among TCP, UNIX socket and (optionally) Tailscale, with PROXY protocol support, written in Golang.
Go
2
star
56

CompilationPrinciples

编译原理作业:基于 flex 和 bison 的 Pascal 到 Intel x86_64 Assembly 编译器
C++
1
star
57

tpm2-luks-helper

Shell
1
star
58

.dotfiles

My ~/.dotfiles on GNU/Linux and macOS
1
star
59

sf-mono-webfont

SF Mono webfont (WOFF & WOFF2) generated with FontSquirrel.
HTML
1
star
60

mdb

Menci DeBugger: Simple Linux x86_64 debugger with ptrace(2)
C++
1
star
61

ics2

RUC ICS2 Labs
C
1
star
62

docker-polyfill-service

Dockerized polyfill-service (https://github.com/Financial-Times/polyfill-service, aka. https://polyfill.io)
Dockerfile
1
star