Seriously, another dependency loader for Roblox? –Somebody
Modules is a simple dependency loader for the Roblox engine. It's a single ModuleScript named "Modules" which exists in ReplicatedStorage, and it is designed to replace the built-in require
function.
Click here to visit the documentation site for Modules →
There's several ways you can get it:
- Take the Model from Roblox.com →
- Download from the GitHub releases page →
- Advanced: build Modules from source using Rojo 0.5.x
Once available, insert the Model into your Roblox place, then move the root "Modules" ModuleScript into ReplicatedStorage.
All ready for blast-off? Check out the Getting Started guide →
Replace require
with the value returned by the "Modules" (the root ModuleScript). It behaves exactly the same way it did before, but in addition to typical arguments types, you can provide strings:
local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"))
local MyClass = require("MyLibrary:MyClass")
local AnotherClass = require("MyLibrary:Something.AnotherClass")
The ModuleLoader looks for a namespace Folder named "MyLibrary" in either ReplicatedStorage or ServerScriptService (if on the server) which contains a ModuleScript named "MyClass".
There's a few patterns that incredibly useful in most Roblox projects, so they're included as modules. They may be required by not specifying a namespace, eg require("Event")
. The included modules are:
class
: provides utility functions for working with idomatic Lua classesEvent
: class similar to Roblox's built-in RBXScriptSignal, it allows any kind of data and hasconnect
,fire
,wait
methodsMaid
: class for deconstructing/deallocating objects; calladdTask
with a connection, function, Instance or other Maid to be disconnected, called or destroyed whencleanup
is calledStateMachine
: a simple implementation of a state machine pattern, event-based or subclass basedState
: a single state in a StateMachine
Each of these modules are documented in-code using LDoc, which also appears on the documentation site.
This section is for development of Modules itself, not using it to make your own stuff. To learn how to do that, check out the documentation site. The rest of this readme will only pertain to developing Modules.
- To build and test this project, you need Rojo 0.5.x and ideally GNU Make.
The Makefile contains a build
target, which creates the file Modules.rbxlx.
# Build Modules.rbxlx
$ make build
# In a new place in Roblox Studio, insert this Model into ReplicatedStorage.
# Start syncing build resources using Rojo
$ rojo serve default.project.json
Using build.project.json, invoke Rojo to build Modules.rbxmx
, a Roblox model file containing only the root ModuleScript (Modules). After it is built and inserted into a Roblox place, you can use the default.project.json Rojo project file to sync changes into the already-installed instance.
To build the documentation for this project, you need Lua 5.1 and LDoc (both of these available in Lua for Windows); additionally Python 3.7 and the libraries in requirements-docs.txt, which can be installed easily using pip.
On a Debian-based operating system, like Ubuntu, you can perhaps use these shell commands to install all the required dependencies:
$ sudo apt update
# Install Lua 5.1, LuaRocks, LuaJson and LDoc
$ sudo apt install lua5.1
$ sudo apt install luarocks
$ sudo luarocks install luajson
$ sudo luarocks install ldoc
# First install Python 3.7. You may have to add the deadsnakes ppa to do this:
$ sudo apt install software-properties-common
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt install python3.7
$ sudo apt install python3.7-venv
# Now create the virtual environment, activate it, and install Python dependencies
$ python3.7 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements-docs.txt
# At this point you're good to go!
$ make docs
# Static HTML becomes available in site/
The source for Modules documentation exists right in its source code using doc comments, as well as the docs directory. To prepare this for the web, a somewhat roundabout process is taken to building the static web content. The Makefile contains a docs
target, which will do the following:
- Using LDoc (Lua 5.1), doc comment data is exported in a raw JSON format. The docs.lua script helps with this process by providing a filter function.
- The ldoc2mkdoc Python module in this repostory converts the raw JSON to Markdown using the Jinja2 template engine.
- This Markdown is then passed to MkDocs to build the static website source (HTML).
The Makefile contains a test
target. It invokes Rojo 0.5.x with the test.project.json file to build a Roblox place file, test.rbxlx, that runs all tests in Roblox Studio.
# Build test.rbxlx
$ make test
# Start syncing test resources using Rojo
$ rojo serve test.project.json
Tests are included in ".test" modules as children of the module they contain tests for. Tests are run using the TestRunner, which is invoked by RunTests.server.lua in "ModuleTests" in ServerScriptService. The TestRunner gathers tests from every ModuleScript whose name ends with ".test". Client tests are run by RunTests.client.lua, in StarterPlayerScripts.
Modules is released under the MIT License, which you can read the complete text in LICENSE.txt. This means you can use this for commercial purposes, distribute it freely, modify it, and use it privately.