"Top-level await" is am ES2022 feature. Check to see which browsers are supported using CanIUse.com - await: top-level.
Option 1 - If top-level await is in the list of your supported browsers, then you can safely ignore this error with the Vite config below. This is my prefered method of disabling the build error as it only ignores a specific ES feature and doesn't hide errors from other potential breaking changes.
Set up your vite.config.js or vite.config.ts like this if the error is coming from your source code:
export default defineConfig({
esbuild: {
supported: {
'top-level-await': true
},
},
});
Set up your vite.config.js or vite.config.ts like this if the error is coming from a specific dependency, for instance pdfjs-dist (removing the dependency name will apply the rule to all dependencies):
export default defineConfig({
optimizeDeps: {
include: ["pdfjs-dist"], // optionally specify dependency name
esbuildOptions: {
supported: {
"top-level-await": true
},
},
},
});
Option 2 - You can use a Vite plugin to remove the top level await. An example would be vite-plugin-top-level-await. The downside with this option is that you are adding yet another dependency to your project and it will also affect your production code. Bugs may be more difficult to trace as you have a more unique configuration.
export default defineConfig({base: './', build: { target: 'esnext'}, });