Script Hoisting
Vue version
3.2.40
Link to minimal reproduction
Steps to reproduce
Just open the link to the page
What is expected?
I expected the <script setup>
block to be the first script to be executed and then the script
created after it to have to export default an object and then the options would be merged that way.
What is actually happening?
When the two scripts have merged the code from the script
blocks is hoisted to the top of the overall script that is generated. The code from <script setup>
is written to the bottom of the screen it allows. The code that is written in <script setup>
has access to the code that is written in every other script. This is not good behavior. I was going to ask for a <script components>
block but didn't think it was possible. Even though I like this behavior since I don't have to register components anymore. I can also split my components into many pieces and avoid creating huge templates. The way this works is not good.
<script setup>
should not have any access to code written in other script blocks- If another script block is used the developer should control whether or not it should have access to what is returned inside of setup
- Ideally there should only be an export default block and a script block.
- The setup block and any other script blocks should be isolated from each other.
- When it comes to Component Registration When I create a component inside of an export default block It should only be avaliable to the component that it is being called inside of
System Info
No response
Any additional comments?
I ran into this while creating an Astro project for myself. I wanted to practice creating sites. I was refactoring the app so that I could then look at the code base and read the code better when I go back to reading it. I can understand it. I wanted to try to split my components into pieces and then I found out that my components were working even though I did not register them.
I gave suggestions on how to fix this problem. But to reiterate. Even though I like the fact That I can easily split components into pieces and bring them back together in the form of components. I would like it. If the blocks are separated from each other and one block has no access to the code written in another block. You can wrap Each script Block in An IIFE to prevent leakage.
All of the points you want to see changed where purposely designed this way see RFC and for obvious reasons will not change now.
You are free to start a discussion about a different approach in the rfcs repo, but dont expect it to be implemented unless your proposal introduces a major added benefit.
Ok then. From now on I'll have to tell developers to put the state inside of the setup block but components will stay in the original script block