Skip to content

require.SourceLoader taking script path as well? #656

@SolarLune

Description

@SolarLune

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions