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§
- Stores various cached info about file paths.
- A common package.json “exports” field.
- A package.json top-level entry field.
- Bitflags that describe path metadata.
- Resolution features to enable.
- Tracks the files that are involved with a resolution, in order to invalidate caches.
- Default operating system file system implementation.
- The resolved path and query string from the original specifier, if any.
- Options for individual resolution requests.
- The result of a resolution request, and list of files that should invalidate the cache.
- Implements the Node.js module resolution algorithm.
Enums§
- A list of file extensions to try when resolving.
- Files that should invalidate the cache when they are created.
- Describes which modules in node_modules should be resolved.
- Whether the module is ESM, CommonJS, or JSON according to its extension or the package.json “type” field.
- An error that occurred in a package.json.
- Describes the result of a resolution request.
- An error that occcured during resolution.
- Represents a module specifier.
- An error that occurred while parsing a specifier.
- Indicates how a specifier should be parsed.
Traits§
- A trait that provides the functions needed to read files and retrieve metadata from a file system.