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
fix: reusing variable names in html module scripts (fix #6851) #6818
Conversation
661a358
to
7d52fe8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This idea makes sense to me, we should definitely have this work for HTML too. I made some comments below, but have another request here (can't highlight in the diff):
We can remove this variable too:
vite/packages/vite/src/node/optimizer/scan.ts
Line 236 in 7d52fe8
let loader: Loader = 'js' |
and for the below usage:
vite/packages/vite/src/node/optimizer/scan.ts
Line 310 in 7d52fe8
loader, |
return js
directly.
@bluwy Thanks for your review, I have made some changes based on your suggestion. However, I have one more confusion about the following comment:
It seems that we can not directly remove the vite/packages/vite/src/node/optimizer/scan.ts Line 282 in 983feb2
vite/packages/vite/src/node/optimizer/scan.ts Line 312 in 983feb2
but we can directly return |
In the PR's code, the |
c226e86
to
d586af7
Compare
d586af7
to
29021bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. This looks good to me. I want to test this locally on a svelte project too to confirm that it still works as usual, but putting my approval first as it seems like a safe change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah looks like there's a case I've overlooked. If you use a named import on a svelte/vue file, it will fail with this PR. e.g.
<!-- Component.svelte -->
<script context="module">
export const named = 'foo'
</script>
import { named } from "./Component.svelte"
The Component.svelte
now doesn't contain any named exports since it's virtualized. I think we may want to handle these special cases for vue and svelte files so that it works again. Mainly Svelte's <script context="module">
and Vue's <script>
should not be virtualized.
@bluwy I think we can use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a pretty smart strategy. I tested it locally and it's working fine now. But I think there's a caveat though if we have two same export name from the star exports, especially for Svelte files only since exports in <script context="module">
means module exports, exports in <script>
means component props. Vue is fine.
So we might still want to handle that for Svelte, otherwise the rest seems fine to me.
84e7783
to
c9fb79a
Compare
I'm not familiar with Svelte, so thank you very much for this case! 👍 I made some changes, you can confirm if I understand what you mean @bluwy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this LGTM. Thanks for making the changes!
I found that this PR can solve part of the problem of another issue #6692 🤔 In the main branch, The error mentioned in the #6692 will also occur in the pre-bundle, because when the code in the vite/packages/vite/src/node/optimizer/scan.ts Line 304 in 33f9671
vite/packages/vite/src/node/optimizer/scan.ts Line 305 in 33f9671
This processing misses the case of With this PR, we can avoid this error when running pre-bundling, because we virtualized all scripts in Vue. But we still get the same error in plugin-vue, because in this case, the code generated by vue/compiler-sfc will contain two default exports. |
Nice find, yeah it looks like this PR partially fixes #6692 too (the scan part). The build part may be an issue in |
Thanks for the PR @OliverTsang, added it to be discussed in our next team meeting. We should merge this one in the 2.9 beta if we proceed so we can get some extra testing from Vue and Svelte folks |
We talked about this PR and decided to move forward with it. We are going to merge it once the Vite 2.9 beta starts to be able to properly test it. |
Description
fixes #6851
I found that Pre-bundling will fail when variable names are reused between html module scripts.
This error is caused by esbuildScanPlugin in vite. This plugin handles two cases internally:
<script>
and<script setup>
(Vue)<script>
and<script context="module">
(Svelte)But it misses handling this case:
<script>
(Html)I think a suitable fix is treating all scripts as separated contexts during import scan phase.
Additional context
Probably need more perspective to see if this makes sense
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).