Skip to content

require in bundled scripts #488

@ThistleSifter

Description

@ThistleSifter

I've been looking into a better solution for the usage of require in bundled scripts and I've come up with the following plan:

  • Add all bundled modules as package loaders.
  • Remove the overridden require function and just use the standard one.

This should mean that the loading of any other modules (eg luasocket) will be unaffected by the bundling process.

There are two ways of directly including package loaders.

  1. package.preload
package.preload["library.utils"] = package.preload["library.utils"] or function()
-- module contents...
end
  1. package.searchers
table.insert(package.loaders, function(package_name)
-- return module wrapped in function from here
end)

By default, require looks for a package loader in the following order (according to package.searchers):

  1. package.preload
  2. package.path
  3. package.cpath

That order can be changed by reordering the table or inserting a function earlier in the package.searchers table for loading bundled modules.

Let me know if you have any thoughts as to which option is better.

(Edited because I initially misread the manual)

Cc @rpatters1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions