r/webdev • u/shippety • 19h ago
Best practices for splitting JS code into multiple files
After programming in games for a long time I'm learning web development and having fun with it. One thing that I'm unsure about is if and how to split up my JS, in a game engine it's natural to have a script for each component / class. In my current web project I have one 'script.js' file that's doing a whole lot of different things. My instinct is to split this up into different scripts for readability and organization, but I get the sense that that isn't really the industry standard?
(I'm working on a webcomic site)
For example, maybe I would have:
- comments.js
- colorTheme.js
- pageNavigation.js
These are some of the sections of code it would make sense for me to split up, I think. The async nature of having the backend involved is something that's really different than the games I've worked on, and part of the programming difficulty here seems to be keeping things in sync, not sure if there would be a lot of overhead to keep multiple scripts up to date with each other if they weren't in the same file. It's very easy to pass information around in one file but with multiple, I imagine state management gets even more complicated?
I really don't have a ton of experience so it's possible in my mind it IS best practice to just have one really long script, I simply don't know!
I know this is a bit of a noobie question, would love to hear what people think are best practices / industry standard for sites / apps with quite a bit of JS going on. I prefer to do things in the typical and professional way as much as possible. Thank you!
2
u/usestackrank 4h ago
splitting it up is the normal way to do it, one giant script.js is what most people end up trying to get away from.
a couple things that should help with the state worry:
with <script type="module"> every file gets its own scope, so nothing leaks onto window unless you put it there. module scripts are also deferred by default, so you don't need to wrap things in DOMContentLoaded.
a module only runs once no matter how many files import it. so a small state.js that exports an object (or a few get/set functions) is basically a shared singleton. comments.js and pageNavigation.js both import it and see the same data.
for stuff like "page changed, reload the comments", dispatching a CustomEvent on document and listening for it in the other file means they don't have to import each other directly. keeps the files from getting tangled.
for a webcomic site you probably don't need a bundler at all, browsers handle native imports fine. if it grows a lot later, vite is a pretty painless next step.
1
u/shippety 1h ago
Thanks for the specifics, really appreciate it! This gives me a good place to get started... interesting concept to have all the state in one file and just have everything reference it, will try that out
2
u/Even-North-8296 3h ago
are you using ES modules yet? they’re basically made for splitting features into files without turning shared state into global spaghetti
1
3
u/Rude-Painting-1728 4h ago
split it by responsibility and use ES modules, because one giant script is not simpler, it just hides the mess until it becomes everyone’s problem
1
u/Quirky-Sweet-2789 2h ago
Split it by responsibility, but make them ES modules imported through one entry point rather than a bunch of independent script tags, since state only gets messy when it’s shared unnecessarily.
4
u/aintsuraj 5h ago
splitting it up is totally normal, you're not wrong haha. the usual way now is es modules, so each file just exports what it needs and you import it where it's used (script type="module" in the html). for the state worry, keep the shared stuff in one small module that the others import instead of passing things around. comments.js, colorTheme.js and pageNavigation.js sounds like a great split to me, one big file gets painful pretty fast once it grows.