Crate parcel_resolver

Source
Expand description

parcel-resolver implements the Node.js module resolution algorithm. It supports both CommonJS and ES module resolution, along with many additional features supported by various tools in the JavaScript ecosystem, such as TypeScript’s tsconfig paths and extension rewriting, the “alias” and “browser” fields used by bundlers, absolute and tilde paths, and more. These can be individually turned on or off using feature flags.

For a full description of all supported resolution features, see Parcel’s documentation.

§Example

To create a resolver, first create a Cache. This stores information about the files in a FileSystem, and can be reused between multiple resolvers. A fresh cache should generally be created once per build to ensure information is up to date.

Next, create a Resolver using one of the constructors. For example, Resolver::node creates a Node.js compatible CommonJS resolver, Resolver::node_esm creates an ESM resolver, and Resolver::parcel creates a Parcel-compatible resolver. From there you can customize individual features such as extensions or index files by setting properties on the resolver.

Finally, call resolver.resolve to resolve a specifier. This returns a result, along with Invalidations describing the files that should invalidate any resolution caches.

use parcel_resolver::{Cache, Resolver, SpecifierType, ResolutionAndQuery};
use std::path::Path;

let cache = Cache::default();
let resolver = Resolver::node_esm(Path::new("/path/to/project-root"), &cache);

let res = resolver.resolve(
  "lodash",
  Path::new("/path/to/project-root/index.js"),
  SpecifierType::Esm
);

if let Ok(ResolutionAndQuery { resolution, query }) = res.result {
  // Do something with the resolution!
}

Structs§

Cache
Stores various cached info about file paths.
ExportsCondition
A common package.json “exports” field.
Fields
A package.json top-level entry field.
FileKind
Bitflags that describe path metadata.
Flags
Resolution features to enable.
Invalidations
Tracks the files that are involved with a resolution, in order to invalidate caches.
OsFileSystem
Default operating system file system implementation.
ResolutionAndQuery
The resolved path and query string from the original specifier, if any.
ResolveOptions
Options for individual resolution requests.
ResolveResult
The result of a resolution request, and list of files that should invalidate the cache.
Resolver
Implements the Node.js module resolution algorithm.

Enums§

Extensions
A list of file extensions to try when resolving.
FileCreateInvalidation
Files that should invalidate the cache when they are created.
IncludeNodeModules
Describes which modules in node_modules should be resolved.
ModuleType
Whether the module is ESM, CommonJS, or JSON according to its extension or the package.json “type” field.
PackageJsonError
An error that occurred in a package.json.
Resolution
Describes the result of a resolution request.
ResolverError
An error that occcured during resolution.
Specifier
Represents a module specifier.
SpecifierError
An error that occurred while parsing a specifier.
SpecifierType
Indicates how a specifier should be parsed.

Traits§

FileSystem
A trait that provides the functions needed to read files and retrieve metadata from a file system.