Ambox notice.png
Scheduled Maintenance
The wiki will be going down for routine maintenance on Friday, October 4th, 2024, at approximately 3:30 PM Central Time (15:30) or 1:30 PM Pacific Time. The site may be inaccessible during this time and the database will be locked from editing. We expect the maintenance to take about thirty minutes. We strongly encourage joining our Discord for updates.

Module:Utilities/require when needed

Jump to navigation Jump to search

Documentation for this module may be created at Module:Utilities/require when needed/doc

local loaded = package.loaded
local require = require
local setmetatable = setmetatable

return function(text, key)
	local module = loaded[text]
	
	if module then
		return key and module[key] or module
	end
	
	local mt = {}
	
	function mt:__index(k)
		module = module or key and require(text)[key] or require(text)
		return module[k]
	end
	
	function mt:__call(...)
		module = module or key and require(text)[key] or require(text)
		return module(...)
	end
	
	return setmetatable({}, mt)
end