Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( InvalidModuleError = errors.New("Invalid module") IllegalModuleNameError = errors.New("Illegal module name") ModuleFileDoesNotExistError = errors.New("module file does not exist") )
var DefaultSourceFS *fsutils.FSUtils
Functions ¶
func DefaultSourceLoader ¶
DefaultSourceLoader is used if none was set (see WithLoader()). It simply loads files from the host's filesystem.
func RegisterNativeModule ¶
func RegisterNativeModule(name string, loader ModuleLoader)
Types ¶
type Option ¶
type Option func(*Registry)
func WithGlobalFolders ¶
WithGlobalFolders appends the given paths to the registry's list of global folders to search if the requested module is not found elsewhere. By default, a registry's global folders list is empty. In the reference Node.js implementation, the default global folders list is $NODE_PATH, $HOME/.node_modules, $HOME/.node_libraries and $PREFIX/lib/node, see https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders.
func WithLoader ¶
func WithLoader(srcLoader SourceLoader) Option
WithLoader sets a function which will be called by the require() function in order to get a source code for a module at the given path. The same function will be used to get external source maps. Note, this only affects the modules loaded by the require() function. If you need to use it as a source map loader for code parsed in a different way (such as runtime.RunString() or eval()), use (*Runtime).SetParserOptions()
type Registry ¶
Registry contains a cache of compiled modules which can be used by multiple Runtimes
func NewRegistry ¶
func NewRegistryWithLoader ¶
func NewRegistryWithLoader(srcLoader SourceLoader) *Registry
func (*Registry) Enable ¶
func (r *Registry) Enable(runtime *js.Runtime, a ...interface{}) *RequireModule
Enable adds the require() function to the specified runtime.
func (*Registry) RegisterNativeModule ¶
func (r *Registry) RegisterNativeModule(name string, loader ModuleLoader)
type RequireModule ¶
type RequireModule struct { Print func(a ...interface{}) (err error) Println func(a ...interface{}) (err error) // contains filtered or unexported fields }
func (*RequireModule) Require ¶
func (r *RequireModule) Require(p string) (ret js.Value, err error)
Require can be used to import modules from Go source (similar to JS require() function).
func (*RequireModule) SetObj ¶
func (r *RequireModule) SetObj(name string, value interface{})
type SourceLoader ¶
SourceLoader represents a function that returns a file data at a given path. The function should return ModuleFileDoesNotExistError if the file either doesn't exist or is a directory. This error will be ignored by the resolver and the search will continue. Any other errors will be propagated.