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

require.SourceLoader taking script path as well? #656

Open
SolarLune opened this issue Jan 29, 2025 · 1 comment
Open

require.SourceLoader taking script path as well? #656

SolarLune opened this issue Jan 29, 2025 · 1 comment
Labels

Comments

@SolarLune
Copy link

SolarLune commented Jan 29, 2025

Hello~

When I create a script registry, I'm using a loading function through require.WithLoader() to manually load the script and return the []byte, which works fine:

g.ScriptRequireRegistry = require.NewRegistry(require.WithLoader(func(path string) ([]byte, error) {
	correctPath := filepath.Join("assets/scripts/", path)
	res, err := fs.ReadFile(assetFileSystem, correctPath)
	if err != nil {
		log.Println("load required module:", correctPath, "failed:", err)
		return nil, err
	}
	log.Println("load required module:", correctPath, "succeeded")
	return res, nil
}))

However, if the module is requested through a relative path from the script, which is in a subdirectory (e.g. something like const Actions = require("../libraries/Actions.mjs");), then I need to know where the requesting script is loading it from.

Would it be possible to make require.SourceLoader take the requesting script's path as well as the module path?

@dop251
Copy link
Owner

dop251 commented Mar 7, 2025

The require module from goja_nodejs resolves module paths relative to the path of the currently running script. In order to make it work you need to make sure that the initial script receives the full path. Depending on how you run this initial script, you need either to pass the full path to RunScript() or to RequireModule.Require().

@dop251 dop251 added the question label Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants