Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import.meta.glob breaks in Svelte template #10949

Closed
7 tasks done
janosh opened this issue Nov 16, 2022 · 5 comments · Fixed by #11056
Closed
7 tasks done

import.meta.glob breaks in Svelte template #10949

janosh opened this issue Nov 16, 2022 · 5 comments · Fixed by #11056
Labels
contribution welcome p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@janosh
Copy link

janosh commented Nov 16, 2022

Describe the bug

First reported in sveltejs/kit#7671.

According to @gtm-nayan

... this is a Vite bug because instead of transforming and replacing just the import.meta.glob('./*') part with an object, it's looking at the whole expression generated by Svelte to coerce the object to a string.

Reproduction

https://stackblitz.com/edit/vitejs-vite-hzfakv?file=package.json,src%2FApp.svelte

Steps to reproduce

No response

System Info

StackBlitz

Used Package Manager

npm

Logs

No response

Validations

@Jevon617
Copy link
Contributor

When using import.meta.glob("./*") in the Svelte template, it will be transformed to import. meta.glob("./*") + "", it is a BinaryExpression rather than a CallExpression, so the vite:import-glob plugin throws an error, should we support BinaryExpression for this situation?

@bluwy
Copy link
Member

bluwy commented Nov 23, 2022

Yeah looks like we should support that. It can be fixed here to get the ast.left if it's a BinaryExpression.

if (ast.type === 'SequenceExpression')
ast = ast.expressions[0] as CallExpression
// immediate property access, call expression is nested
// import.meta.glob(...)['prop']
if (ast.type === 'MemberExpression') ast = ast.object as CallExpression
if (ast.type !== 'CallExpression')
throw err(`Expect CallExpression, got ${ast.type}`)

@bluwy bluwy added contribution welcome p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Nov 23, 2022
@github-actions
Copy link

Hello @janosh. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!

@gtm-nayan
Copy link
Contributor

There is another bug with parsing where an expression like import.meta.glob('./*.js')['./foo.js']() would fail because it looks at the whole string as a call expression and the second set of parens doesn't have a glob in it.

Would adding acorn-walk as a dep be acceptable? Then, it might be better to use findNodeAt as a more general solution instead of special-casing BinaryExpression.

@bluwy
Copy link
Member

bluwy commented Nov 24, 2022

Would adding acorn-walk as a dep be acceptable? Then, it might be better to use findNodeAt as a more general solution instead of special-casing BinaryExpression.

Looks like it's only a small dependency. I'd be fine by that too 👍

patak-dev pushed a commit that referenced this issue Nov 28, 2022
patak-dev pushed a commit to vitejs/vite-plugin-react that referenced this issue Dec 2, 2022
patak-dev pushed a commit to vitejs/vite-plugin-vue that referenced this issue Dec 2, 2022
bluwy pushed a commit that referenced this issue Dec 5, 2022
patak-dev pushed a commit that referenced this issue Dec 5, 2022
* fix: glob import parsing (#10949) (#11056)

closes #10949
closes #11051

* fix: import.meta.env and process.env undefined variable replacement (fix #8663) (#10958)

Co-authored-by: bluwy <[email protected]>
fix #8663

* fix(esbuild): handle inline sourcemap option (#11120)

* fix(importGlob): preserve line count for sourcemap (#11122)

* fix: Dev SSR dep optimization + respect optimizeDeps.include (#11123)

* fix: reset global regex before match (#11132)

* chore: fix test

Co-authored-by: gtmnayan <[email protected]>
Co-authored-by: julienv3 <[email protected]>
Co-authored-by: 翠 / green <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Dec 13, 2022
futurGH pushed a commit to futurGH/vite that referenced this issue Feb 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution welcome p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
4 participants