From f0cfbf995238a42064e119bd1daa694fd1683ea3 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 8 Oct 2023 20:22:01 +0200 Subject: [PATCH] perf: lazy require commands --- lua/lazy/core/util.lua | 13 +++++++++++++ lua/lazy/view/commands.lua | 1 + 2 files changed, 14 insertions(+) diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index d1d6bec5..a11d3ae7 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -411,4 +411,17 @@ function M.merge(...) return ret end +function M.lazy_require(module) + 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 diff --git a/lua/lazy/view/commands.lua b/lua/lazy/view/commands.lua index 1fa7cc1b..c66611d6 100644 --- a/lua/lazy/view/commands.lua +++ b/lua/lazy/view/commands.lua @@ -1,3 +1,4 @@ +local require = require("lazy.core.util").lazy_require local View = require("lazy.view") local Manage = require("lazy.manage") local Util = require("lazy.util")