Update require-by-string-aliases.md

This commit is contained in:
menarulalam 2023-10-04 15:10:00 -07:00 committed by GitHub
parent 5ef1ca7390
commit 116bc71acc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
## Summary
We need to add intuitive alias functionality to facilitate the grouping together of related Luau files into libraries and allow for future package managers to be developed and integrated easily.
We need to add intuitive alias and paths functionality to facilitate the grouping together of related Luau files into libraries and allow for future package managers to be developed and integrated easily.
## Motivation
@ -82,6 +82,75 @@ Of course, users can still use the alias map to explicitly define this behavior
}
```
#### Paths
Similar to [paths in TypeScript](https://www.typescriptlang.org/tsconfig#paths), we will introduce a `paths` array that can be configured in `.luaurc` files. Whenever a path is passed to `require` and does not begin with `./` or `../`, the path will first be resolved relative to the requiring file. If this fails, we will attempt to resolve paths relative to each path in the `paths` array.
The `paths` array can contain absolute paths, and relative paths are resolved relative to `.luaurc` file in which they appear.
##### Example Definition
With the given `paths` definition (`.luaurc` file located in `/Users/johndoe/Projects/MyProject/src`):
```json
"paths": [
"../dependencies",
"/Users/johndoe/MyLuauLibraries",
"/Users/johndoe/MyOtherLuauLibraries",
]
```
If `/Users/johndoe/Projects/MyProject/src/init.luau` contained the following code:
```lua
local graphing = require("graphing")
```
We would search the following directories, in order:
- `/Users/johndoe/Projects/MyProject/src`
- `/Users/johndoe/Projects/MyProject/dependencies`
- `/Users/johndoe/MyLuauLibraries`
- `/Users/johndoe/MyOtherLuauLibraries`
### Paths array
The `paths` configuration variable provides convenience and allows Luau developers to build complex, well-organized libraries. Imagine the following project structure:
```
luau-paths-project
├── .luaurc
├── dependencies
│ └── dependency.luau
└── src
└── module.luau
```
If `.luaurc` contained the following `paths` array:
```json
{
"paths": ["./dependencies"]
}
```
Then, `module.luau` could simply require `dependency.luau` like this:
```lua
local dependency = require("dependency")
-- Instead of: require("../dependencies/dependency")
```
Using the `paths` array allows Luau developers to organize their projects however they like without compromising code readability.
### Large-scale projects in Luau
For large-scale Luau projects, we might imagine that every dependency of the project is a Luau project itself. We might use an organizational structure like this to create a clean hierarchy:
```
large-luau-project
├── .luaurc
├── subproject-1
├── subproject-2
└── subproject-3
```
##### Current limitations of aliases
- Aliases cannot reference other aliases. (However, this is compatible with this proposal and will likely be implemented in the future.)
@ -227,4 +296,4 @@ Some potential issues with this approach:
#### Defining configuration files in a non-JSON format
Alias maps could alternatively be defined in specially named `.luau` files themselves, or at least adhering to Luau syntax. This approach is somewhat unappealing, however, as it would require package management software to parse Luau syntax. JSON syntax is well-understood and well-supported, which would likely facilitate the development of package management software.
Alias maps could alternatively be defined in specially named `.luau` files themselves, or at least adhering to Luau syntax. This approach is somewhat unappealing, however, as it would require package management software to parse Luau syntax. JSON syntax is well-understood and well-supported, which would likely facilitate the development of package management software.