Skip to content

Instantly share code, notes, and snippets.

@Henkoglobin
Last active March 30, 2020 00:05
Show Gist options
  • Save Henkoglobin/ced7f06d3e6bda643b8affffbddd12e2 to your computer and use it in GitHub Desktop.
Save Henkoglobin/ced7f06d3e6bda643b8affffbddd12e2 to your computer and use it in GitHub Desktop.
Provides a function that can be used to reload all required modules.
local standard = {}
for k in pairs(package.loaded) do
standard[k] = true
end
function R()
local loadThese = {}
-- Unload all loaded packages...
for k in pairs(package.loaded) do
-- ... unless they're considered 'standard lib'
if not standard[k] then
-- Also save the old instance in order to replace global variable values
loadThese[k] = package.loaded[k]
package.loaded[k] = nil
end
end
for lib, prevLoaded in pairs(loadThese) do
-- Re-require the package
local loaded = require(lib)
-- Iterate all globals, if they match the old instance, replace the value
for name, value in pairs(_G) do
if value == prevLoaded then
_G[name] = loaded
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment