question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Script Loader

See original GitHub issue

RFC: 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
manastcommented, Nov 26, 2021

This looks indeed amazing and would help us a lot in refactoring all the lua scripts we have.

1reaction
ccolliecommented, Nov 20, 2021

@roggervalf Will do. Just give me a day or so to cleanup

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found