-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Could we declare multiple <script> and <style> blocks in a svelte file ? #13616
Comments
Please show what you are doing in your script/style/markup, I suspect that there are good ways of organizing the code without requiring multiple scripts/styles. Maybe commonalities can just be extracted to a separate JS file, CSS file or component. (A change to the way these blocks work seems unlikely to me.) |
Unfortunately it's in french, but here's a short example attached to it(, it's an 8th example in my 51 svelte files that i haven't thought about) : Ouverture.zip It's an interesting one that i've made these last two days, but even if it's small it needs to work with other files, it's not autonomous, if you want to i can create a more independent component, however you may have simply asked for a random example. I'm calling the component Ouverture.svelte like that : <OUVERTURE type='nouvel onglet' nouvelOnglet={créerOnglet('autres', laCommunauté.community.title||laCommunauté.community.name, laCommunauté.community.icon)}
<COMMUNAUTÉ instance={instance} bind:cetteCommunauté={laCommunauté} afficherPlus={true} bind:afficherLaCommunauté={afficherLaCommunauté} />
</OUVERTURE> Here, <COMMUNAUTÉ/> will be opened in a new tab because
Once again, sorry for the file written in french. I could also find an old version of a very large file of hundreds of lines with more than a dozen type if the problem doesn't seem obvious with such a short file.
Yep, me too, such big change would be suprising(, well, i.d.k.) |
This design seems odd to me. The code for the various types seems very independent, why are you not using separate components instead? I.e. Even if you keep an |
The first reason, for all files, would be that it makes more sense to put them all in the same file rather than having a folder of multiples files, it's cleaner. For some files, such as the one with the icons, it's because they may have a different html depending on the type but they keep the same style. For other files, such as It'd just be more concise/practical to put everything in a file instead of a folder with multiple files. And it's not only related to files that can be compartimented with a prop such as If requests are only for bugs then sorry for my mistake. That javascript naturally supports multiple script blocks may not mean that it'd necessarily be easy, but i'm so ignorant that i'm thinking that even if a single script block is necessary then maybe the only change the compiler would have to do is to cut-paste the different content of the script blocks into a single script block, and then continue the process as usual(, as well as adapting the svelte parser, etc.), which is why i.d.k. if such change/feature/improvement would be technically impossible at first sight. |
It may look like that, but it makes it harder to reason about the code and navigate it. Usually many small 10 line functions are preferable to a single 200 line function. Especially when multiple people work on something, it saves lot of time if those 190 lines can be completely ignored if only 10 are actually relevant to the issue at hand.
That is not the case, don't worry, but feature requests also need to be challenged.
Apart from the technical feasibility, a very important question is whether it is a good idea. Even if a feature itself has neutral value, just adding another feature increases the overall complexity of the codebase and increases the risk of bugs and regressions in other areas. So by default, everything is already a net negative. Given that most people probably would not recommend the approach you are taking to component and file organization, I doubt that there is much demand for this and that it would justify the associated costs. On top of the added complexity, having additional syntax means also that more things need to be documented and potentially taught/learned — more stuff also means more potential for confusion. So as mentioned, I would go with separate components if the contents are mostly independent, extract shared things into their own CSS/JS files and keep files relatively small. (Also highly recommend navigating components directly via their file names rather than going through the directory tree, having all those small components actually helps navigation when doing this.) |
(Oh, ok, that's entirely natural then, i just had the first impression that the only argument against this feature was that it was useless because another way existed, now i understand that your point was instead that you don't consider it as desirable/practical as i do)
I agree
Some features more than others, yes
No. Wouldn't you want to have the freedom of writing multiple script and style blocks ? It's an evident advantage, i don't understand how i could lose the argument that such a basic/fundamental amelioration would be useful. Even those who agree with this approach to component and file organization may appreciate to have this liberty/possibility(, e.g. temporarily to avoid frequently scrolling back-and-forth between the html and the script and/or style while adding something new in some cases). I guess ideally the desire of the "Svelte society" should be seeked by a survey, because it'd be hard to debate subjective preferences. I expected a technical reason, not "We could (easily? )do it, but( the real problem is that) i prefer to have a lot of files and folders rather than a few of them, so you should to", and i guess to be fair i should add that you don't believe it'd be used&useful enough, which i can't agree with or even understand, perhaps could you develop on that if you want to challenge what seems an obvious/undeniable advantage in my eyes ? P.S. : To orientate myself inside a large function/file, i'm using different type of comments with different regular expressions :
|
Depends. One advantage that frameworks can provide is consistency. If every Svelte codebase is completely different from all others, this hinders collaboration, compatibility and is a cause for confusion. It also makes learning and reading the code harder. Multiple competing syntaxes are generally avoided due to similar problems.
There might be technical reasons not to do this, but I have not considered that aspect since I am also not too familiar with Svelte's internals and I am not a maintainer. I just suspect that the incurred costs of this are too high to justify it. There of course could be various problems with scoping, hoisting, statement ordering and whatnot. Since the request for multiple components in single file was rejected (though Svelte 5 will have snippets), I doubt that the maintainer team will like the idea of multiple scripts/styles. I am not telling you want to like, but you might have make do with the status quo. While I acknowledge that some things are just a personal preference, many things have a measurable impact. E.g. there might be differences in how fast certain code styles can be read. Of course this can come down to familiarity but if most of the world codes a certain way, that probably needs to be taken into account. Your custom comment syntax shows to me that your code style is pretty far removed from the familiar. Ultimately, frameworks unfortunately can't really please everyone, otherwise they become a mess. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I'm reopening this subject because i've read that you can't put script and style blocks in a snippet(, shouldn't have lazingly trusted ChatGPT on that one). Since, apparently, you can only put HTML in there, it isn't equivalent to the initial request :/ While many people here are in favor of reducing the number of files by having multiple components in a single svelte file(, e.g., or here), there are also many people who are really opposed to the complexification of syntax that will be brought by snippets. "My" solution is ultra basic : create a component with the prop "type" and use multiple if..else <script for type=='type 1'>
{#if type=='type 1'}
{/if}
<style for type=='type 1'>
<script for type=='type 2'>
{#if type=='type 2'}
{/if}
<style for type=='type 2'>
<script for type=='type 3'>
{#if type=='type 3'}
{/if}
<style for type=='type 3'> If i'm not mistaken somewhere, and the performances aren't too bad, then we'd have the best of both worlds : no new syntax ; and the possibility of dumping little components in a single svelte file. It's almost certain that i'm mistaken but i'm asking just in case. |
I checked out the Ouverture.svelte and saw four components stuffed up into one file. If multiple cases require barely related logic, markup, and styles, then they should be implemented as separate components. Otherwise, you get messy code, and the fact that you have to use colourful comments only proves this. Your approach is like organized chaos - it's good for you and only you. |
That's what @brunnerh said : increase your number of files and folders. However, many people would prefer to put multiple small related components inside a single svelte file, in order to avoid having too many folders and files, that comment is another one. I don't agree with the apparent "norm" of spreading your code on multiple lines, functions, and files. I'm not the only one to prefer to visually encompass as much as i can without having to do a back and forth and trust my memory. And if there's a shared logic and/or style i'd have to add even more files inside the folder(, or rely on a single mega .ts and/or .css file for every shared logic/style, which would reduce the number of files but not the number of necessary opened tabs). I don't like switching between tabs and i'm not the only one(, since there's a lot of upvotes in the other request). |
Here's a list of the disadvantages i could think of :
And here's a list of the advantages i could think of :
I've certainly forgotten a lot of advantages and disadvantages. Notes :[1] : A practice could be to have a |
A.1 Many projects were started as pet projects, and then other people came to fix something or add a feature. The more code follows common and good practices, the easier it is for other people to join; otherwise, they may even give up on joining. A.2 The file size (number of LoC) isn't the problem, but rather such files often contain smelly code: functions with deeply nested code, functions that do too many things at the same time (violation of single responsibility principle), use variables in multiple places separated by miles of code. D.3 Of course, it's doable; you can even do it yourself with your own preprocessor. D.4 I doubt it will have a noticeable impact, but it may make the compiler significantly more complex.
I have an example I've written: sort of a different type of post - they share the frame but are very different inside. A clean, straightforward, and understandable code. Cramping it into a single file, will turn this into a total mess. |
A very good addition, do you believe it was satisfyingly answered in « This wouldn't arise in a working group(, with defined rules) », with "working group" defined as the opensource project ? Or do you feel that this point isn't answered in the lists above ? To develop on this :
I've updated the D.2 and A.2 as a consequence, please tell me what you think of the answer if you feel inclined to
I don't understand this point, why would you need to differentiate them ?
I've edited the D.3 by adding a D.3.5
A simple ctrl-f will lead you to the if..else statement delimiting this compartment
If it's in the case of a file compartmentalized into multiple svelte components, then their lines of code will be independent from each other(, similarly to But if you were saying that a bug in one svelte component could affect the rest : the bug will only appear if the value of And if you were saying that having multiple files related to each other will reduce the number of bugs(, e.g., by having
It's subjective, but i don't agree, except perhaps with short examples like class="flex". Tailwind is known for cluttering the markups/html, at least i personally don't think it has its place in the html. Do you want me to add this argument in one of the list above ? I'm not sure how i would formulate it.
That's very annoying, i've created a keyboard shortcut last year to do that, but barely used it. It's a nice advice, but you're constantly forgetting what has been folded or not, and you spend your time folding stuff, with the need to remember where everything is. Good advice though, some people probably use that instead of scrolling.
And lose the possibility of having long lines.
And you'll have to switch between mutiple tabs :/
It ultimately depends of the complexity of your components, but even if it was complex(, well, reasonably of course), it wouldn't be a problem if you have colored comments that visually delimits your if..else compartments, scrolling up and down would be the exact same as switching tabs. Once again, please correct me if you feel inclined to, but in any case, thank you very much for having engaged with this request thus far. |
I mean pet projects with initially a single developer.
It sounds like "I don't write a bad code". But looking at the Ouverture, I cannot name it a good code at all:
You can quickly go to any file with ctrl+p
The slideshow must definitely be a component in this case. It looks like you don't understand what the single responsibility principle means or why it is used.
you can split vertically as well
I'd say I rarely switch between "sibling" components - I more often go to child or parent components.
My personal opinion: 500+ LoC - you really should think about splitting the component; 1000+ LoC - there is definitely something wrong. 62KB svelte file - no wonder you have trouble with intense scrolling; here, even minimap won't help. |
I don't think i have much to add to your comment, so i'll keep it brief( for once) :
Certainly me neither(, it was only a 2-days old file after all), but not always for the same reasons(, b.t.w., if it wasn't for openNewPage(), i'm usually putting short functions only used once, like closeNewPage(), directly in the on:click of the html, but i agree with the naming, and also with the on:event(, i've used on:pointerdown&Co in other files, so i should have stopped this habit of putting them in the onMount, thx)). <script for type=='type 1'> or whatever it will be is a new syntax. Why would you ? (edit : i understood a bit too late what he meant, and edited the first comment to be clearer)
We both believe that we follow this principle, even if i agree that your interpretation is stricter than mine(, probably for functions as well). There's always a granularity that is subjective, you can theoretically make a different function for every single operation for instance.
Any solution with multiple panels will make me lose the possibility of visually encompassing as much as i can, but i understand why programmers have multiple(&large) screens in order to avoid/limit such loss. |
Since Svelte 5 already implemented nested <style> elements, and that snippets cancelled a large part of my argument in favor of a single large file with if..else for excessively small svelte components, and primarily because it seems fair to assume in retrospect that multiple |
Describe the problem
I have multiple svelte component that export the same kind of prop ==> type:'type 1'|'type 2'|'type 3'
I'm currently writing the svelte file as such :
It'd be ideal if i could write the svelte file like that instead :
As @johndeighan reminded us here, JavaScript allows multiple script blocks. His request was closed because his case was too specific, but mine is very general.
Many other cases could also benefit from this ; e.g., when a function is only used once in the html, and the programmer believes that it would be clearer to include this function next to the html, inside <script> tags
In my app i have 51 svelte files, and 7 files i organized in this way(, for short tools, icons, type of moderation msgs, type of statistics, type of menus, parameters, and themes), and i'm not counting the files with a tab bar(, the modlog, the search results, ...), i don't think i would be the only one who would benefit from such feature, if it can be done.
Describe the proposed solution
If it'd lead to too many complications then nevermind, there may be a good reason for getting rid of this useful feature/freedom, but if it can be added i'd like to request it.
Thx !
Importance
nice to have
If you want to skip the discussion, i've made a list of advantages and disadvantages that will be edited if i'm aware of any new comment in this thread
The text was updated successfully, but these errors were encountered: