Menci / vite-plugin-top-level-await

Transform code to support top-level await in normal browsers for Vite.
MIT License
273 stars 15 forks source link

[vite-plugin-top-level-await] Cannot read properties of undefined (reading 'transformNeeded') #18

Closed svenbarnett closed 1 year ago

svenbarnett commented 1 year ago

[vite-plugin-top-level-await] Cannot read properties of undefined (reading 'transformNeeded') error during build: TypeError: Cannot read properties of undefined (reading 'transformNeeded') at ....\node_modules\vite-plugin-top-level-await\dist\transform.js:158:75

how to resolve this problem?

thanks

svenbarnett commented 1 year ago

rendering chunks (90)...[vite-plugin-top-level-await] Cannot read properties of undefined (reading 'transformNeeded') error during build: TypeError: Cannot read properties of undefined (reading 'transformNeeded') at xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:158:75 at transformByType (xxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:18:16) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25) at transformByType (xxxxxx\node_modules\vite-plugin-top-level-await\dist\transform.js:15:25)

Menci commented 1 year ago

reproduce project pls

svenbarnett commented 1 year ago

delete node_modules and npm install again, but not take effect

steveworkman commented 1 year ago

This happens in the scenario where CSS is being extracted into its own file. It doesn't have an AST so https://github.com/Menci/vite-plugin-top-level-await/blob/main/src/transform.ts#L243 fails.

I've made a PR with a test https://github.com/Menci/vite-plugin-top-level-await/pull/23

Menci commented 1 year ago

@steveworkman Are there any example? import "./xxxx.css" generates __vitePreload() for me. It doesn't generate import "./xxxx.css" statements.

steveworkman commented 1 year ago

We are dynamically importing CSS from our main.ts file. Do that with vite defaults gives this error

On Sun, 5 Feb 2023, 12:54 Menci, @.***> wrote:

@steveworkman https://github.com/steveworkman Are there any example? import "./xxxx.css" generates __vitePreload() for me. It doesn't generate import "./xxxx.css" statements.

— Reply to this email directly, view it on GitHub https://github.com/Menci/vite-plugin-top-level-await/issues/18#issuecomment-1417758765, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHS4WPY2SZAQUEIMRONCL3WV6PHRANCNFSM6AAAAAASZER4XY . You are receiving this because you were mentioned.Message ID: @.***>

Menci commented 1 year ago

@steveworkman I can't reproduce. Vite generates __vitePreload(() => Promise.resolve({}),true?["assets/semantic.be4f5b7e.css"]:void 0); for me.

Could you send me a example project?

steveworkman commented 1 year ago

Run npm run build on this https://github.com/steveworkman/vite-tla-bug and you get:

vite v4.1.1 building for production...
✓ 8 modules transformed.
rendering chunks (2)...[vite-plugin-top-level-await] Cannot read properties of undefined (reading 'transformNeeded')
error during build:
TypeError: Cannot read properties of undefined (reading 'transformNeeded')
    at /Users/steve/code/vite-project/node_modules/vite-plugin-top-level-await/dist/transform.js:204:75
    at transformByType (/Users/steve/code/vite-project/node_modules/vite-plugin-top-level-await/dist/transform.js:19:16)
Menci commented 1 year ago

Got it. There're other transforms after this plugin that removed the import() expressions so I never noticed it.

Menci commented 1 year ago

Fixed in v1.2.4