diff --git a/site/content/docs/config/index.md b/site/content/docs/config/index.md index 7f20e6b7..75c72b75 100644 --- a/site/content/docs/config/index.md +++ b/site/content/docs/config/index.md @@ -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 }, }, diff --git a/site/content/docs/path-require-mode/index.md b/site/content/docs/path-require-mode/index.md index e15c3be8..d46a42eb 100644 --- a/site/content/docs/path-require-mode/index.md +++ b/site/content/docs/path-require-mode/index.md @@ -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, } ``` @@ -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", @@ -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`. diff --git a/src/rules/require/path_require_mode.rs b/src/rules/require/path_require_mode.rs index 1a85c241..b9543566 100644 --- a/src/rules/require/path_require_mode.rs +++ b/src/rules/require/path_require_mode.rs @@ -23,15 +23,22 @@ pub struct PathRequireMode { module_folder_name: String, #[serde(default, skip_serializing_if = "HashMap::is_empty")] sources: HashMap, + #[serde(default = "default_use_luau_configuration")] + use_luau_configuration: bool, #[serde(skip)] luau_rc_aliases: Option>, } +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(), } } @@ -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( diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ce0730fc..76204d9f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -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,