@@ -3,7 +3,32 @@ local fs = require("luarocks.fs")
33
44local log = require (" luarocks.build.lls-addon.log" )
55
6- local PRELOAD_FORMAT = ' package.preload["%s"] = assert(load([%s[\n %s\n ]%s], "%s"))'
6+ local OVERRIDE_REQUIRE = [[
7+ assert(type((...)) == "function", "attempt to require a plugin")
8+
9+ do
10+ local loaded = setmetatable({}, { __index = function() return false end })
11+ local preload = {}
12+ local oldRequire = require
13+ function require(path, ...)
14+ local loadedModule = loaded[path]
15+ if loadedModule ~= false then
16+ return loadedModule
17+ end
18+
19+ local moduleLoader = preload[path]
20+ if not moduleLoader then
21+ return oldRequire(path, ...)
22+ end
23+
24+ loadedModule = moduleLoader(path, ":preload:")
25+ loaded[path] = loadedModule
26+ return loadedModule
27+ end]]
28+
29+ local PRELOAD_FORMAT = ' preload["%s"] = assert(load([%s[\n %s\n ]%s], "%s", "t"))'
30+
31+ local OVERRIDE_REQUIRE_END = " end"
732
833local function assertContext (context , ...)
934 -- luacov: disable
@@ -39,10 +64,6 @@ local function addDirectory(strings, modulePath, dirName)
3964 fs .change_dir (dirName )
4065 table.insert (modulePath , dirName )
4166 for entry in fs .dir (" ." ) do
42- if entry == " ." or entry == " .." then
43- goto continue
44- end
45-
4667 if fs .is_file (entry ) then
4768 local filename = string.match (entry , " ^([^%.]+)%.lua$" )
4869 if filename then
@@ -72,7 +93,9 @@ local function bundle(filename, destination)
7293 local strings = {} --- @type string[]
7394 if fs .is_dir (dir .path (fs .current_dir (), filename )) then
7495 log .info (" found bundled files directory at " .. filename .. " /, adding to bundle..." )
96+ table.insert (strings , OVERRIDE_REQUIRE )
7597 addDirectory (strings , {}, filename )
98+ table.insert (strings , OVERRIDE_REQUIRE_END )
7699 end
77100
78101 log .info (" adding main file " .. filename .. " .lua to bundle..." )
0 commit comments