Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External module support for require #422

Merged
merged 3 commits into from
Sep 19, 2023
Merged

Conversation

thelindat
Copy link
Member

Consider this file structure for below examples.

- resources/
  - mylib/
    - import.lua
    - module.lua (required by import)
  - myresource/
    - server.lua

Improved module resolution for imported files

  • myresouce uses fxmanifest.lua to import a file from mylib i.e. server_script '@mylib/import.lua'
  • mylib/import.lua includes require 'module'
  • module is loaded in the context of myresource, and tries to load myresource/module.lua instead of mylib/module.lua

With this change, modules will now load from the correct resource.

External module paths

  • allows you to load files from another resource using require, the same way you would in fxmanifest
  • server_script '@mylib/import.lua' can be replaced with require '@mylib.import'

Though you don't get the benefits of IntelliSense when using LLS, you can at least gain the other benefits of require

  • controlled file loading order and error handling
  • files cannot be loaded more than once
  • optional file loading (i.e. frameworks and other dependencies)
  • deferred file loading (lazy-loading)
  • reduced global variables

If loading an external file with fxmanifest (e.g. server_file '@src/file.lua'),
require will load from @src rather than the current resource context.
Allow require '@src.path' to load an external file into the calling resource.
@thelindat thelindat merged commit 699884a into master Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant