Why isn't Lua `require` supported?

I was surprised to find that Lua require isn’t supported on the playdate, and that we’re expected to use import instead.

Anyone know why that is? I expect there to be a good reason for it, but haven’t been able to find it.

I wouldn't say it's a good reason per se, but the main reason is that we don't have a complete Lua runtime. In particular, we don't have package, os, or io because we're not running on an OS that provides the support for those.

Is there something you'd normally use require for and can't figure out how to get it done on Playdate? playdate.file.load() and playdate.file.run() allow for loading (compiled) code at runtime, might be useful here.

I have two gripes with the current lack of require support:

  1. linters and the language-server doesn’t understand import, so they’re of limited use. This degrades the developer experience for me.
  2. it becomes harder to write code that works outside of the simulator or device. I have a clean seperation between code that requires the playdate API, and code that is pure “business” logic. It’d be nice to load the business logic into the lua repl to test out some stuff. Same applies to luaunit, though I believe there’s a patched version of luaunit somewhere that works with import.

I’ve also recently been experimenting with the fennelprogramming language, which is a lisp that compiles to lua. If you compile fennel with –require-as-include, it will turn an expression equal to local test = require(“map1”) into:

package.preload["map1"] = package.preload["map1"] or function(...)
  <source of map1.lua>
end
local test = require("map1")

If it cannot determine the source of the module at compile time, compilation will fail.

Of course, this doesn’t work as neither package.preload or require is supported by playdate’s lua runtime, even if the above should work fine without os and filesystem apis.

Maybe this could be an inspiration on how to support require in playdate?

In any case, supporting require would measurably improve the dev experience. At least for me.