Warning
This project is more of a proof-of-concept. Its performance does not rival with require()
and most likely never will. If you would like to tackle the efficiency and performance be my guest.
What is import?
Import is a TypeScript-esque approach to Roblox’s default require()
system. It allows you to use string resolving for path directories, and usage of other shorthand top-level headers.
Example
local import = require(game.ReplicatedStorage.Packages.Import)
local Maid = import "@wally/maid" -- handles Wally packages
local SomeClass = import "shared/someClass" -- handles ReplicatedStorage
local SomeClass = import "client/someClass" -- handles LocalPlayer.PlayerScripts
local SomeClass = import "server/someClass" -- handles ServerScriptService
-- for resolving it PascalCase and camelCase are both accepted
local SomeClass = import "shared/SomeClass"
One thing to immediately note is how similar this looks to Rojo’s default file hierarchy. This is intentional as Import is a Wally package itself (hence the need to do ReplicatedStorage.Packages)
Installing Import
Import is a Wally module, and has a GitHub where it can manually installed via .zip
file.
Using Wally
[dependencies]
Import = "alexinite/import@0.1.7"
{
"name": "project-name",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$path": "src/shared",
"Packages": {
"$path": "Packages"
},
"DevPackages": {
"$path": "DevPackages"
}
},
"ServerScriptService": {
"$path": "src/server",
"Packages": {
"$path": "ServerPackages"
}
},
...
}
}
Dot Referencing
Import also allows you to use dot referencing too, but this is where it may become a little weird. Indexing with (script)
is only necessary IF you’re going to use dot referencing.
Example
-- indexing [script] here is important: it was either this or getfenv()
local import = require(game.ReplicatedStorage.Packages.Import) (script)
local SomeSubclass = import "./subclass" -- script.Subclass
local ClassInSameDir = import "../otherClass" -- script.Parent.OtherClass
Why not use getfenv()
?
getfenv
comes with a massive problem: It breaks Luau optimizations. That is the entire reasoning behind why not to use getfenv
.