Illustration of issues around use of top-level await in Vite apps: https://github.com/vitejs/vite/issues/5013
Try node run.js
. The expectation is that it will load src/index.js
(which depends on src/data.js
, which includes a top-level await) and run its exported list_fruits
function.
Instead, we get an error:
3:45:57 PM [vite] Error when evaluating SSR module src/index.js:
SyntaxError: Cannot use keyword 'await' outside an async function (2:0)
at Object.pp$5.raise (/path/to/vite-top-level-await-repro/node_modules/vite/dist/node/chunks/dep-36bf480c.js:47485:13)
This means that apps that use Vite for server-side rendering cannot make use of top-level await.
This one is probably just PEBKAC, but: I would expect npx vite build
to build the app successfully, despite the top-level await failing ESBuild's default safari13.1
target check, because the Vite config specifies esbuild: false
. For whatever reason, it's not having any effect.
I encountered this while trying to figure out if there's any magic that Vite performs (or could perform) to transpile top-level awaits for environments where it's not natively supported (of which Safari 14 and below is the only one I particularly care about).
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。