Skip to content

Commit

Permalink
Add config parameter to disable loading luaurc files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeparlefrancais committed Jan 11, 2025
1 parent 060fae7 commit 0ba33d3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions site/content/docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Any missing field will be replaced with its default value.
// map `pkg` to a path to the `Packages` folder created by Wally:
pkg: "./Packages",
},

// Enable or disable finding `.luaurc` files to load source aliases
use_luau_configuration: true, // default value
},
},

Expand Down
15 changes: 13 additions & 2 deletions site/content/docs/path-require-mode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ The path require mode can be defined as the string 'path' to use all the default
sources: {
pkg: "./Packages",
},

// optional (defaults to true)
use_luau_configuration: true,
}
```

Expand Down Expand Up @@ -110,7 +113,7 @@ Given this configuration file for bundling:
require_mode: {
name: "path",
sources: {
pkg: "./Packages",
@pkg: "./Packages",
// you can also map directly to a file (Lua or
// any supported data file)
images: "./assets/image-links.json",
Expand All @@ -123,6 +126,14 @@ Given this configuration file for bundling:
It is possible to make these require call in any file:

```lua
local Promise = require("pkg/Promise")
local Promise = require("@pkg/Promise")
local images = require("images")
```

## Luau Configuration Files

Luau configuration files are named `.luaurc` and they can contain an `aliases` parameter which acts like the [sources](#sources) parameter in darklua.

The value of `use_luau_configuration` will change how darklua finds new sources. Before looking at the [sources](#sources) value, darklua will attempt to find the nearest `.luaurc` configuration file to each file it processes. If it finds one, it will load the aliases.

This behavior is enabled by default. It can be disabled by setting `use_luau_configuration` to `false`.
13 changes: 13 additions & 0 deletions src/rules/require/path_require_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ pub struct PathRequireMode {
module_folder_name: String,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
sources: HashMap<String, PathBuf>,
#[serde(default = "default_use_luau_configuration")]
use_luau_configuration: bool,
#[serde(skip)]
luau_rc_aliases: Option<HashMap<String, PathBuf>>,
}

fn default_use_luau_configuration() -> bool {
true
}

impl Default for PathRequireMode {
fn default() -> Self {
Self {
module_folder_name: get_default_module_folder_name(),
sources: Default::default(),
use_luau_configuration: default_use_luau_configuration(),
luau_rc_aliases: Default::default(),
}
}
Expand All @@ -53,11 +60,17 @@ impl PathRequireMode {
Self {
module_folder_name: module_folder_name.into(),
sources: Default::default(),
use_luau_configuration: default_use_luau_configuration(),
luau_rc_aliases: Default::default(),
}
}

pub(crate) fn initialize(&mut self, context: &Context) -> Result<(), DarkluaError> {
if !self.use_luau_configuration {
self.luau_rc_aliases.take();
return Ok(());
}

if let Some(config) = find_luau_configuration(context.current_path(), context.resources())?
{
self.luau_rc_aliases.replace(
Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mod timer;

pub(crate) use expressions_as_statement::{expressions_as_expression, expressions_as_statement};
pub(crate) use luau_config::{clear_luau_configuration_cache, find_luau_configuration};
pub(crate) use scoped_hash_map::ScopedHashMap;
pub(crate) use serde_string_or_struct::string_or_struct;
use std::{
ffi::OsStr,
iter::FromIterator,
Expand Down

0 comments on commit 0ba33d3

Please sign in to comment.