Skip to content

Script loading (ctx.script.load) ​

ctx.script.load(name) runs another Script of your tenant as a module and returns the value that Script returns. It replaces require, which does not exist.

text
ctx.script.load(name: string) -> module
  • It is synchronous and returns one value: the loaded module. There is no ok flag.
  • A Script that does not exist raises an error. Wrap the call in pcall if the module is optional.
  • Each module runs once per run; loading the same name again returns the same value.
  • A run can load at most 16 modules.

Writing a module ​

A module is an ordinary Script (role SCRIPT) that returns a table of functions:

lua
-- Script named "pricing-helpers"
local M = {}

function M.volume_discount(quantity)
  if quantity >= 500 then return 0.15 end
  if quantity >= 100 then return 0.10 end
  return 0.05
end

return M

Keep modules free of side effects: they run on load, and only once per run.

Using a module ​

lua
local QUANTITY = 120
local UNIT_PRICE = 18.5

local loaded, discount = pcall(function()
  local helpers = coalesce(ctx.script.load("pricing-helpers"))
  return helpers.volume_discount(QUANTITY)
end)

if not loaded then
  ctx.log.info("pricing-helpers not installed, using the default rate", {
    reason = tostring(discount),
  })
  discount = 0.05
end

return {
  usedHelper = loaded,
  netPrice = math.round(QUANTITY * UNIT_PRICE * (1 - discount), 2),
}

The loaded module is not type-checked together with your script, so its exports arrive untyped. coalesce(value) passes the value through unchanged.

Availability ​

  • In integrations, ctx.script.load resolves the libraries declared in the integration manifest, the integration's own scripts and the Script resources bound on the instance, not arbitrary tenant Scripts.
  • Product-template resolver scripts can load tenant Scripts.
  • The print modules lib/jdf and lib/jmf are also loadable in the SCRIPT, CUSTOM_APP and INTEGRATION roles; see JDF and JMF.

Connect machines, automate workflows, and build custom apps for print production.