Should module loaders be used for better organization?

Guys, I have a question about module loaders.

Do you use a module loader in your projects?

In many projects I’ve worked on, there’s a module loader, but it ends up losing all IntelliSense.

If module loaders are necessary for better organization, should I create types for every single module?

Yes, and I love it.

In my opinion, it is the easiest way to organize (and better yet, optimize) your code.


For IntelliSense handling, I manually define all modules in my main shared framework module:


Then, inside Coastified:
image


Inside any script where Coastified is required, IntelliSense is remained:


There might be better ways to do this, but I found that this works pretty well.

Hey! Thank you for your reply, can you show how do you define your modules??

If you meant how the modules are set up, here’s the Network module as described above:

thanks, can i see how you type a module?

Yes, as long as they’re just loaders, and not what I like to call module providers. Here’s what I mean:

  • Providers: Require modules, which you get in your scripts using something like:
    local ServiceModule = Framework:Get("ServiceModule")
    or local ServiceModule = Framework.Services.ServiceModule
  • Loaders only: Require modules only to run startup steps (like an Init/Setup/Start sequence), and that’s it. Modules get other modules by regular requires:
    local ServiceModule = require(ReplicatedStorage.Services.ServiceModule)

Because of the way most providers load modules (GetChildren, require, put in table), intellisense is completely lost. If you want intellisense while keeping a sort of “provider” format, the only ways around it that i’m aware of are:

  • make types for every single module and make sure to provide said type whenever that module is required somewhere else. This imo is very tedious, having to keep a separate module up to date, especially in a large codebase…
  • require the modules directly in a table, then reference said table directly as well:
-- Framework module (your module loader)
local Framework = {}
local services = {
   ServiceModule = require(folder.ServiceModule),
}

-- do some stuff like run Init/Setup/Start, setup signals, etc

Framework.Services = services
return Framework

-- in some other script:
local Framework = require(path.to.Framework)
local ServiceModule = Framework.Services.ServiceModule

This does make you require only the framework to get the services you need, and keep intellisense, however you’re gonna need to make sure your list of required modules are always up to date.


When you step back and think about it, really the advantage of module loaders (Single Script Architecture) is being able to require the modules in advance so that other places that need it won’t have to go through yielding/init/setup etc steps. Providing the modules afterwards is just a convenience, and if you have plugins for autocomplete like Require Autocomplete, it’s really not necessary – not to mention, losing intellisense, which you have to work around and deal with.

No
Its reinventing regular scripts

no. That is not how module loaders work…

optimize and module loaders in same topic is criminal

i dont think its worth using module loaders, you can organize your scripts without them

Just use what you want if you don’t care enough about optimization. It’s up to personal preference after all :man_shrugging: