Add relative requires for better modularity
Relative requires (requires that support paths relative to the script) are extremely helpful in other engines to help support more modular and portable design. This approach also makes it easier to scale a codebase.
As of right now, all require calls are relative to the root by default. For example, let's assume we have a structure like this:
.
└── Project/
├── main.lua
└── Packages/
└── MyLibraryName/
├── init.lua
├── subModule.lua
├── anotherModule.lua
└── deeperFolder/
└── deeperModule.luaFor example, if subModule.lua wanted to require anotherModule.lua, you would have to write this in it:
local anotherModule = require("Packages.MyLibraryName.anotherModule")As you get deeper into modularity, this becomes increasingly lengthy of a require, but more importantly, it's becomes more difficult to copy-paste it into another project.
Relative requires can solve this. We could do something similar to Roblox's require-by-string syntax. For example, anotherModule.lua could access deeperModule.lua with something like this:
local deeperModule = require("@self.deeperFolder.deeperModule")Instead of:
local deeperModule = require("Packages.MyLibraryName.deeperFolder.deeperModule")Additionally, we could include @parent in the require string to get the parent directory. For example, if subModule.lua wanted to require anotherModule.lua, we could do:
local anotherModule = require("@parent.anotherModule")@parent could also stack. For example, if deeperModule.lua wanted to require subModule.lua, you could write:
local subModule = require("@[email protected]")Of course there might be a more elegant way to write this, but the idea of it I believe in. In other engines I do this a lot, so this would help a ton.
Source: love2d/love