Skip to content

Commit

Permalink
add workaround to ignore fatal errors during root paths resolving to …
Browse files Browse the repository at this point in the history
…avoid a breaking change
  • Loading branch information
sokra committed Jan 11, 2021
1 parent 8b0b411 commit f33b2c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ For more examples creating different types resolvers (sync/async, context, etc)
| mainFields | ["main"] | A list of main fields in description files |
| mainFiles | ["index"] | A list of main files in directories |
| modules | ["node_modules"] | A list of directories to resolve modules from, can be absolute path or folder name |
| roots | [] | A list of directories to resolve request starting with `/` from |
| ignoreRootsErrors | false | Ignore fatal errors happening during handling of `roots` (allows to add `roots` without a breaking change) |
| unsafeCache | false | Use this cache object to unsafely cache the successful requests |
| plugins | [] | A list of additional resolve plugins which should be applied |
| symlinks | true | Whether to resolve symlinks to their symlinked location |
Expand Down
12 changes: 11 additions & 1 deletion lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ exports.createResolver = function(options) {
// A list of root paths
const roots = options.roots || [];

// Ignore errors happening when resolving roots
const ignoreRootsErrors = options.ignoreRootsErrors || false;

const restrictions = options.restrictions || [];

// Use this cache object to unsafely cache the successful requests
Expand Down Expand Up @@ -222,7 +225,14 @@ exports.createResolver = function(options) {
});
plugins.push(new ModuleKindPlugin("after-described-resolve", "raw-module"));
roots.forEach(root => {
plugins.push(new RootPlugin("after-described-resolve", root, "relative"));
plugins.push(
new RootPlugin(
"after-described-resolve",
root,
"relative",
ignoreRootsErrors
)
);
});
plugins.push(new JoinRequestPlugin("after-described-resolve", "relative"));

Expand Down
19 changes: 17 additions & 2 deletions lib/RootPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class RootPlugin {
* @param {string | ResolveStepHook} source source hook
* @param {Array<string>} root roots
* @param {string | ResolveStepHook} target target hook
* @param {boolean=} ignoreErrors ignore error during resolving of root paths
*/
constructor(source, root, target) {
constructor(source, root, target, ignoreErrors) {
this.root = root;
this.source = source;
this.target = target;
this._ignoreErrors = ignoreErrors;
}

/**
Expand All @@ -44,7 +46,20 @@ class RootPlugin {
obj,
`root path ${this.root}`,
resolveContext,
callback
this._ignoreErrors
? (err, result) => {
if (err) {
if (resolveContext.log) {
resolveContext.log(
`Ignored fatal error while resolving root path:\n${err}`
);
}
return callback();
}
if (result) return callback(null, result);
callback();
}
: callback
);
});
}
Expand Down
1 change: 1 addition & 0 deletions test/roots.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("roots", () => {
},
roots: [__dirname, fixtures],
fileSystem,
ignoreRootsErrors: true,
plugins: [
{
apply(resolver) {
Expand Down

0 comments on commit f33b2c9

Please sign in to comment.