Skip to content

Commit 3a17e5f

Browse files
committed
feat(bundle): give each plugin its own preload table
1 parent a4b68dc commit 3a17e5f

5 files changed

Lines changed: 138 additions & 19 deletions

File tree

spec/plugins/many-with-closing-brackets/expected.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
package.preload["plugin.submodule"] = assert(load([=[
1+
assert(type((...)) == "function", "attempt to require a plugin")
2+
3+
do
4+
local loaded = setmetatable({}, { __index = function() return false end })
5+
local preload = {}
6+
local oldRequire = require
7+
function require(path, ...)
8+
local loadedModule = loaded[path]
9+
if loadedModule ~= false then
10+
return loadedModule
11+
end
12+
13+
local moduleLoader = preload[path]
14+
if not moduleLoader then
15+
return oldRequire(path, ...)
16+
end
17+
18+
loadedModule = moduleLoader(path, ":preload:")
19+
loaded[path] = loadedModule
20+
return loadedModule
21+
end
22+
23+
preload["plugin.submodule"] = assert(load([=[
224
return [[
325
some text
426
]]
527
6-
]=], "plugin.submodule"))
28+
]=], "plugin.submodule", "t"))
29+
30+
end
731

832
local submodule = require("plugin.submodule")
933

spec/plugins/many-with-eq/expected.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
package.preload["plugin.another"] = assert(load([======[
1+
assert(type((...)) == "function", "attempt to require a plugin")
2+
3+
do
4+
local loaded = setmetatable({}, { __index = function() return false end })
5+
local preload = {}
6+
local oldRequire = require
7+
function require(path, ...)
8+
local loadedModule = loaded[path]
9+
if loadedModule ~= false then
10+
return loadedModule
11+
end
12+
13+
local moduleLoader = preload[path]
14+
if not moduleLoader then
15+
return oldRequire(path, ...)
16+
end
17+
18+
loadedModule = moduleLoader(path, ":preload:")
19+
loaded[path] = loadedModule
20+
return loadedModule
21+
end
22+
23+
preload["plugin.another"] = assert(load([======[
224
return [=====[another]=====]
325
4-
]======], "plugin.another"))
26+
]======], "plugin.another", "t"))
527

6-
package.preload["plugin.some"] = assert(load([======[
28+
preload["plugin.some"] = assert(load([======[
729
return [=====[some]=====]
830
9-
]======], "plugin.some"))
31+
]======], "plugin.some", "t"))
32+
33+
end
1034

1135
local another = require("plugin.another")
1236
local some = require("plugin.some")

spec/plugins/many/expected.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
package.preload["plugin.another"] = assert(load([[
1+
assert(type((...)) == "function", "attempt to require a plugin")
2+
3+
do
4+
local loaded = setmetatable({}, { __index = function() return false end })
5+
local preload = {}
6+
local oldRequire = require
7+
function require(path, ...)
8+
local loadedModule = loaded[path]
9+
if loadedModule ~= false then
10+
return loadedModule
11+
end
12+
13+
local moduleLoader = preload[path]
14+
if not moduleLoader then
15+
return oldRequire(path, ...)
16+
end
17+
18+
loadedModule = moduleLoader(path, ":preload:")
19+
loaded[path] = loadedModule
20+
return loadedModule
21+
end
22+
23+
preload["plugin.another"] = assert(load([[
224
return "another"
325
4-
]], "plugin.another"))
26+
]], "plugin.another", "t"))
527

6-
package.preload["plugin.some"] = assert(load([[
28+
preload["plugin.some"] = assert(load([[
729
return "some"
830
9-
]], "plugin.some"))
31+
]], "plugin.some", "t"))
32+
33+
end
1034

1135
local another = require("plugin.another")
1236
local some = require("plugin.some")

spec/plugins/nested/expected.lua

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
package.preload["plugin.some"] = assert(load([[
1+
assert(type((...)) == "function", "attempt to require a plugin")
2+
3+
do
4+
local loaded = setmetatable({}, { __index = function() return false end })
5+
local preload = {}
6+
local oldRequire = require
7+
function require(path, ...)
8+
local loadedModule = loaded[path]
9+
if loadedModule ~= false then
10+
return loadedModule
11+
end
12+
13+
local moduleLoader = preload[path]
14+
if not moduleLoader then
15+
return oldRequire(path, ...)
16+
end
17+
18+
loadedModule = moduleLoader(path, ":preload:")
19+
loaded[path] = loadedModule
20+
return loadedModule
21+
end
22+
23+
preload["plugin.some"] = assert(load([[
224
return "some"
325
4-
]], "plugin.some"))
26+
]], "plugin.some", "t"))
527

6-
package.preload["plugin.subplugin.another"] = assert(load([[
28+
preload["plugin.subplugin.another"] = assert(load([[
729
return "another"
830
9-
]], "plugin.subplugin.another"))
31+
]], "plugin.subplugin.another", "t"))
32+
33+
end
1034

1135
local another = require("plugin.subplugin.another")
1236
local some = require("plugin.some")

src/luarocks/build/lls-addon/bundle.lua

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,32 @@ local fs = require("luarocks.fs")
33

44
local 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

833
local 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

Comments
 (0)