darklua
repository·main·Indexed 42 days ago
https://github.com/seaofvoices/darkluaA code transformation tool for Lua 5.1 and Roblox Luau that allows developers to apply configurable rules to transform source code. It supports bundling Lua code by merging require calls into a single file, converting data files (JSON, YAML, TOML) into Lua modules, and providing custom content loaders for asset management.
What's inside darklua
- darklua is a tool used to transform Lua 5.1 and Roblox Luau code using a set of configurable rules. It allows developers to automate code transformations and optimizations.
Understand the Gatsby project structure
mainA standard Gatsby project includes several key configuration files and directories:
/src: Contains front-end source code (e.g., components, page templates).gatsby-config.js: The main configuration file for site metadata and plugin management.gatsby-node.js: Used to implement Gatsby Node APIs to customize the site build process.gatsby-browser.js: Used to implement Gatsby browser APIs for customizations affecting the browser.gatsby-ssr.js: Used to implement Gatsby server-side rendering (SSR) APIs.package.json: The Node.js manifest file defining project metadata and dependencies..prettierrc: Configuration for the Prettier code formatting tool.
How Path Resolution works in Path Require Mode
mainWhen darklua encounters a
requirecall with a string, it resolves the path in two steps:1. Finding the Head (Starting Point)
- Starts with
.or..: The path is relative to the file containing therequirecall. - Starts with
/: The path is treated as an absolute path. - Otherwise: The first component of the path is used to look up a matching entry in the configured
sources.
2. Resolving the Tail (File Matching)
Darklua searches for the first available file in this specific order:
- The exact path provided.
- The path +
.luauextension. - The path +
.luaextension. - The path joined with the
module_folder_name(e.g.,./path/init). - The path joined with
module_folder_name+.luau. - The path joined with
module_folder_name+.lua.
Example: A require for
./examplewill check:./example./example.luau./example.lua./example/init./example/init.luau./example/init.lua
- Starts with
Integrate with Luau configuration files (.luaurc)
mainDarklua can respect Luau's native configuration files named
.luaurc. These files can contain analiasesparameter that functions identically to thealiasesparameter in darklua.To enable this behavior, ensure
use_luau_configurationis set totrue(which is the default) in your darklualuaurequire mode configuration. When enabled, darklua will search for the nearest.luaurcfile relative to each file being processed and load its aliases before applying the aliases defined in the darklua config.Use Luau Require Mode for bundling
mainThe
luaurequire mode allows darklua to resolve content using Luau-specific module paths, following Luau RFCs for require resolution (including Amended Require Resolution and Abstract Module Paths). This mode is specifically designed to match the behavior of the Lune runtime.Warning: Do not confuse the
luaurequire mode with thepathrequire mode. While both use strings that look like file paths, they resolve to actual files using different logic.{ "bundle": { "require_mode": { "name": "luau" } } }Use Luau Configuration Files (.luaurc) for aliases
mainDarklua can respect Luau configuration files named
.luaurc. These files can contain analiasesparameter that functions identically to thesourcesparameter in darklua's own configuration.If
use_luau_configurationis set totrue(which is the default), darklua will attempt to find the nearest.luaurcfile relative to each file it processes and load its aliases before checking the main darkluasourcesconfiguration.To disable this behavior, set
use_luau_configuration: falsein your path require mode configuration.How darklua content loaders work
maindarklua selects a loader for every file based on its extension or a configured pattern.
- Default Behavior: Lua and Luau files are parsed as code; recognized data files (JSON, TOML, YAML) are converted to Lua modules; other files are ignored.
- Custom Loaders: You can use the
loadersfield in your configuration to map glob patterns to specific loader names. - Pattern Matching: Patterns are matched in order. The first pattern that matches a file is the one used.
- Fallback: If no pattern matches, darklua falls back to extension-based defaults. If no default loader exists for an extension, the file is skipped.
{ loaders: { "**/*.model.json": "copy", "**/*.md": "string", "**/*.png": "buffer/base64", }, }Configure Roblox Require Mode without a Rojo Sourcemap
mainIf you do not provide a
rojo_sourcemap,darkluaassumes all paths are relative to the file performing therequireand that the file structure matches the Roblox DataModel.Example conversions:
./MyClassbecomesscript.Parent:FindFirstChild("MyClass")(orscript:FindFirstChild("MyClass")if the target is aninit.luamodule).../MyClassbecomesscript.Parent.Parent:FindFirstChild("MyClass").
How darklua rules work
mainRules are the core mechanism that transform Lua code. When running
darklua process, the engine iterates through a provided list of rules sequentially. Each rule transforms the code and passes the result to the next rule in the list.Important: The order of rules in your configuration matters. You should perform operations like injecting values (e.g., using
inject_global_value) before attempting to perform static expression computation or optimizingifbranches, as subsequent rules depend on the code state produced by earlier ones.Require data files as Lua during bundling
mainWhen using the
pathrequire mode, darklua can automatically convert data files into Lua data structures during the bundling process. It supports the following file extensions:- JSON:
.jsonor.json5 - YAML:
.ymlor.yaml - Toml:
.toml - Text:
.txt(mapped to a string containing the file content)
These files are inlined into the resulting Lua bundle as native Lua tables or strings.
- JSON:
Use special global groups in rename_variables
mainThe
globalsproperty in therename_variablesrule supports special tokens starting with$that allow you to protect entire groups of identifiers at once.$default: Protects all standard Lua language globals (e.g.,print,math,table,pairs,_G).$roblox: Protects all standard Roblox Lua globals (e.g.,game,workspace,Instance,CFrame).
Note: Lua language keywords like
returnanddoare automatically excluded from renaming and cannot be configured.{ rule: "rename_variables", globals: ["$default", "$roblox"] }Install darklua via GitHub Releases
mainPre-built versions of darklua are available for direct download on the GitHub Releases page.