richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Top-level await import leads to ReferenceError: Cannot access 'x' before initialization #84

Closed cdauth closed 9 months ago

cdauth commented 9 months ago

I have a file where I'm using a top-level (await import('./x.md')).html statement. This gets transpiled into (await __vitePreload(() => Promise.resolve().then(() => x), true ? void 0 : void 0)).html. However, x is defined further down in the code, leading to an error Uncaught ReferenceError: Cannot access 'x' before initialization.

As a workaround, I managed to solve the issue by using regular imports.

richardtallent commented 9 months ago

Closed as this has no reproduction, it's not really clear if this is an issue with this plugin specifically or with vite in general, and it appears you have a workaround.

I'm also not sure why you would bother doing an async import here -- wouldn't the markdown file be part of the bundle, and thus there would be no IO to await?