Resolver
A plugin type: Turn dependency requests into absolute paths (or exclude them)
Resolvers get called with an asset request (consisting of a source file path
and the specifier of what is being requested) which it then attempts to
resolve. If the resolver isn't sure how to handle a request, it can also return
null
and pass it to the next resolver in the chain.
import { Resolver } from "@parcel/plugin";
export default new Resolver({
async resolve({ filePath, dependency }) {
if (!shouldHandle(filePath)) {
return null;
}
// ...
return {
filePath: doResolve({ from: filePath, to: dependency.moduleSpecifier }),
};
},
});
The result object can also contain sideEffects
(which corresponds to package.json#sideEffects
) code
(used instead of fs.readFile(filePath)
) and isExcluded
(e.g. to exclude node_modules
).
ΒΆ Relevant API
ResolveResult parcel/packages/core/types/index.js:1451
type ResolveResultΒ = {|
+filePath?: FilePath,
An absolute path to the file.
+pipeline?: ?string,
+isExcluded?: boolean,
+priority?: DependencyPriority,
+sideEffects?: boolean,
Corresponds to BaseAsset's sideEffects
.
+code?: string,
A resolver might want to resolve to a dummy, in this case filePath
is rather "resolve from".
+canDefer?: boolean,
Whether this dependency can be deferred by Parcel itself (true by default).
+diagnostics?: Diagnostic | Array<Diagnostic>,
A resolver might return diagnostics to also run subsequent resolvers while still providing a reason why it failed.
+meta?: JSONObject,
Is spread (shallowly merged) onto the request's dependency.meta
+invalidateOnFileCreate?: Array<FileCreateInvalidation>,
+invalidateOnFileChange?: Array<FilePath>,
|}
Referenced by:
ResolverResolver parcel/packages/core/types/index.js:1593
type ResolverΒ = {|
resolve({|
dependency: Dependency,
options: PluginOptions,
logger: PluginLogger,
filePath: FilePath,
pipeline: ?string,
|}): Async<?ResolveResult>,
|}