seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
93 stars 13 forks source link

A rule for injecting libraries #213

Open jiwonz opened 2 months ago

jiwonz commented 2 months ago

inject_modules rule would be great for injecting libraries and global functions too (exampe: injecting libraries of luau written in pure lua to emulate luau outside of luau)

Example

.darklua.json5

rules: {
  inject_libraries: {
    libraries: [
      {
        name: "buffer",
        path: "buffer.lua"
      },
      {
        name: "typeof",
        path: "typeof.lua"
      }
    ]
  }
}

src/main.lua

print(typeof(buffer.create))

After processing:

local buffer = require("{top_path}/_DARKLUA_lib{buffer_blake3_hash}")
local typeof = require("{top_path}/_DARKLUA_lib{typeof_blake3_hash}")
print(typeof(buffer.create))
nightcycle commented 2 months ago

Completely agree! I had to do this just yesterday with a bash script in order to get the task library added to code that I deploy in both lune and roblox.