-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
14 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -411,4 +411,17 @@ function M.merge(...) | |
return ret | ||
end | ||
|
||
function M.lazy_require(module) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
folke
Author
Owner
|
||
local mod = nil | ||
-- if already loaded, return the module | ||
-- otherwise return a lazy module | ||
return type(package.loaded[module]) == "table" and package.loaded[module] | ||
or setmetatable({}, { | ||
__index = function(_, key) | ||
mod = mod or require(module) | ||
return mod[key] | ||
end, | ||
}) | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This function doesn't allow functions (or any non-table value) being returned from a module.
I suggest replacing
type(package.loaded[module]) == "table"
with a simple non-nil checkpackage.loaded[module]
and adding a __call method to the metatable (to make functions work):