resolve

package
v0.0.0-...-674084c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(debug bool)

Debug will cause the resolver code to trace what it's doing

func IsStdModule

func IsStdModule(name string) bool

IsStdModule returns `true` if the module name given is part of the standard library, false otherwise. Useful for checking the referrer (importing module), to restrict access to internals.

func ScriptBase

func ScriptBase(path string) vfs.Location

ScriptBase returns a base location representing the filesystem under the path given. It is so called because it's used to create the initial base for resolving modules relative to the script being run.

Types

type Candidate

type Candidate struct {
	Path string
	Rule string
}

Candidate is a path that was considered when resolving an import, and the explanation (resolution rule) for why it was considered.

type FileImporter

type FileImporter struct {
	// contains filtered or unexported fields
}

FileImporter is an importer sourcing from a filesystem. Modules are expected to be arranged at the root of the filesystem, in the directory structure implied by the import path. A specifier `foo/bar` will be resolved to (in the order attempted):

  • `/foo/bar`
  • `/foo/bar.{js,mjs}`
  • `/foo/bar/index.{js,mjs}

func NewFileImporter

func NewFileImporter(vfs vfs.FileSystem) *FileImporter

NewFileImporter constructs a FileImport given a filesystem

func (*FileImporter) Import

func (fi *FileImporter) Import(base vfs.Location, specifier, referrer string) ([]byte, vfs.Location, []Candidate)

Import implements importer. Note that the file import only ever cares to look in the root of the given filesystem. It doesn't care where the importing module is located.

type Importer

type Importer interface {
	// Resolve a specifier (e.g., `my-module/foo') to a specific path
	// and file contents. Also returns a list of the interpretations
	// of the specifier attempted, including that returned.
	Import(base vfs.Location, specifier, referrer string) (data []byte, resolved vfs.Location, candidates []Candidate)
}

Importer is a object resolving a import to actual JS code.

type Loader

type Loader interface {
	LoadModule(scriptName string, code string, resolve v8.ModuleResolverCallback) error
}

Loader is an object able to load a ES 2015 module.

type MagicImporter

type MagicImporter struct {
	Specifier string
	Generate  func(vfs.Location) ([]byte, string)
	Public    bool // can this be imported from any module?
}

MagicImporter handles importing "magic" modules, that is modules that are calculated wherever they are imported.

func (*MagicImporter) Import

func (m *MagicImporter) Import(base vfs.Location, specifier, referrer string) ([]byte, vfs.Location, []Candidate)

Import implements the Importer interface for MagicImporter.

type NodeImporter

type NodeImporter struct {
	// contains filtered or unexported fields
}

NodeImporter is an implementation of Importer that uses a resolution algorithm adapted from Node.JS's, in order to support modules installed with npm.

func NewNodeImporter

func NewNodeImporter(vfs vfs.FileSystem) *NodeImporter

NewNodeImporter constructs a NodeImporter using the given filesystem

func (*NodeImporter) Import

func (n *NodeImporter) Import(base vfs.Location, specifier, referrer string) ([]byte, vfs.Location, []Candidate)

Import is the entry point into the module resolution algorithm.

type Relative

type Relative struct {
}

Relative resolves specifiers that start with './' or '../', by looking for a file relative to the importing module (as given by the base argument).

func (Relative) Import

func (r Relative) Import(base vfs.Location, specifier, referrer string) ([]byte, vfs.Location, []Candidate)

Import implements Importer with the relative import rules.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver implements module resolution by deferring to the set of importers that it's given.

func NewResolver

func NewResolver(loader Loader, base vfs.Location, importers ...Importer) *Resolver

NewResolver creates a new Resolver.

func (Resolver) ResolveModule

func (r Resolver) ResolveModule(specifier, referrer string) (string, int)

ResolveModule imports the specifier from an import statement located in the referrer module.

func (*Resolver) SetRecorder

func (r *Resolver) SetRecorder(recorder *record.Recorder)

SetRecorder instructs Resolver to record actions in the specified recoder. Call with nil to disable.

type StaticImporter

type StaticImporter struct {
	Specifier string
	Resolved  string
	Source    []byte
}

StaticImporter is an importer mapping an import specifier to a static string.

func (*StaticImporter) Import

func (si *StaticImporter) Import(basePath, specifier, referrer string) ([]byte, string, []Candidate)

Import implements importer.

type StdImporter

type StdImporter struct {
	// PublicModules are modules users are allowed to import.
	PublicModules []string
}

StdImporter is the standard library importer.

func (*StdImporter) Import

func (i *StdImporter) Import(base vfs.Location, specifier, referrer string) ([]byte, vfs.Location, []Candidate)

Import implements importer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL