RFC: Script Loader
See original GitHub issueRFC: scriptLoader
A replacement for the script loading mechanism in bullmq
See WIP branch here and tests here
Why ?
To allow modularity and composability of lua scripts, with recursive dependency handling and path mapping.
Example
isNil.lua
local function isNil(x)
return x == nil or x == cjson.null
end
string.lua
--- @include "isNil"
local function safeLen(x)
return not isNil(x) and #x or 0
end
math.lua
--- @include "isNil"
local function sign(x)
if isNil(x) then return nil end
if (x == 0) then return 0 end
return x < 0 and -1 or 1
end
utils.lua
--- @include "string"
--- @include "math"
app.js
const script = await loadScript('path/to/utils.lua')
result
local function isNil(x)
return x == nil or x == cjson.null
end
local function safeLen(x)
return not isNil(x) and #x or 0
end
local function sign(x)
if isNil(x) then return nil end
if (x == 0) then return 0 end
return x < 0 and -1 or 1
end
All children are loaded transitively and in dependency order to any depth.
Path mapping is supported, so we can do the following:
utils.lua
--- @include "<includes>/math"
--- @include "<includes>/string"
--- @include "<includes>/jobs"
app.ts
addScriptPathMapping('includes', 'relative/path/to/includes');
const script = await loadScript('utils.lua');
The ~ character at the beginning of an include maps to the project root (location of the project’s package.json)
For convenience, we can do glob includes
utils.lua
--- @include "<includes>/util-*.lua"
API
loadScript() function
Signature:
export declare function loadScript(filePath: string, cache?: Map<string, ScriptInfo>): string;
Parameters
| Parameter | Type | Description |
|---|---|---|
| filePath | string | path to the top-level script we want to load |
| cache | Map<string, ScriptInfo> | an optional map to return the metadata or all files gathered to fulfil the request |
Returns:
string
The interpolated script
addScriptPathMapping() function
Add a script path mapping. Allows @includes of the form <includes>/utils.lua where includes is a user
defined path.
Signature:
export declare function addScriptPathMapping(name: string, relativePath: string): void;
Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | the name of the path mapping |
| relativePath | string | the path relative to the caller of this function |
Returns:
void
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
rfcs/0040-script-setup.md at master · vuejs/rfcs - GitHub
Introduce a new script type in Single File Components: <script setup> , which exposes all its top level bindings to the template.
Read more >Issuehunt
RFC : scriptLoader. A replacement for the script loading mechanism in bullmq. See WIP branch here and tests here. Why ? To allow...
Read more >rfc:splclassloader - PHP.net wiki
Make SplClassLoader silently fails if class is not found. This one is useful when using multiple instances of SplClassLoader in a single script....
Read more >Explaining The New script setup Type in Vue 3 - LearnVue
Explaining The New script setup Type in Vue 3 - RFC Takeaways ... The <script setup> type is a proposed change in the...
Read more >RFC Red Small - Mansion Brush
Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/mansionb/public_html/site/wp-includes/script-loader.php on line ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This looks indeed amazing and would help us a lot in refactoring all the lua scripts we have.
@roggervalf Will do. Just give me a day or so to cleanup