diff --git a/demo.js b/demo.js new file mode 100644 index 0000000..d0070b7 --- /dev/null +++ b/demo.js @@ -0,0 +1,12 @@ +const resolve = require("./lib"); + +const folder = "./"; +const myTarget = "graceful-fs"; + +const demoResolve = resolve.create({ + extensions: [".ts", ".js"] +}); + +demoResolve(folder, myTarget, (err, result) => { + console.log({ result }); +}); diff --git a/lib/Resolver.js b/lib/Resolver.js index fdb73dc..5c3fc68 100644 --- a/lib/Resolver.js +++ b/lib/Resolver.js @@ -502,6 +502,16 @@ class Resolver { if (!resolveContext) return callback(new Error("resolveContext argument is not set")); + /// obj would look like below. + // { + // "context": { + // "environments": [ + // "node+es3+es5+process+native" + // ] + // }, + // "path": "./", + // "request": "graceful-fs" + // } /** @type {ResolveRequest} */ const obj = { context: context, @@ -535,6 +545,8 @@ class Resolver { }; } + /// message example. + /// resolve 'graceful-fs' in './' const message = `resolve '${request}' in '${path}'`; /** @@ -567,6 +579,7 @@ class Resolver { return callback(error); }; + /// In this demo case, running demo.js, this was undefined. if (resolveContext.log) { // We need log anyway to capture it in case of an error const parentLog = resolveContext.log; @@ -661,6 +674,7 @@ class Resolver { } } + /// The case of most often this function is used I am knowing is when called from a plugin. Almost plugin has resolver.doResolve(). /** * @param {ResolveStepHook} hook hook * @param {ResolveRequest} request request @@ -670,10 +684,13 @@ class Resolver { * @returns {void} */ doResolve(hook, request, message, resolveContext, callback) { + /// stackEntry example. + /// resolve: (./) graceful-fs const stackEntry = Resolver.createStackEntry(hook, request); /** @type {Set | undefined} */ let newStack; + /// This is an object that has sequenced stackEntry objects. So after the second time visiting here will be true. if (resolveContext.stack) { newStack = new Set(resolveContext.stack); if (resolveContext.stack.has(stackEntry)) { @@ -712,6 +729,8 @@ class Resolver { }, message ); + /// This is going to call the hook. + /// For example, for the first time, this will be resolve hook, and the hook has a plugin: ParsePlugin.js return hook.callAsync(request, innerContext, (err, result) => { if (err) return callback(err); if (result) return callback(null, result); diff --git a/lib/ResolverFactory.js b/lib/ResolverFactory.js index 59558c7..6b137fa 100644 --- a/lib/ResolverFactory.js +++ b/lib/ResolverFactory.js @@ -309,6 +309,7 @@ exports.createResolver = function (options) { const plugins = userPlugins.slice(); + /// if you came from demo.js, customResolver is undefined. since when creating normalizedOptions, didn't specify useSyncFileSystemCalls in options. const resolver = customResolver ? customResolver : new Resolver(fileSystem, normalizedOptions); @@ -687,6 +688,7 @@ exports.createResolver = function (options) { //// RESOLVER //// + /// This will apply each plugin to the hook specified as a source hook. It took so long time to understand the concept that there are hooks and plugins that will be attached to a hook and actually run function compiled in tapable. for (const plugin of plugins) { if (typeof plugin === "function") { /** @type {function(this: Resolver, Resolver): void} */ diff --git a/lib/index.js b/lib/index.js index ab02cfa..bf6dcf3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,12 +36,14 @@ const nodeContext = { environments: ["node+es3+es5+process+native"] }; +/// This will create a resolver factory, which is written in ResolverFactory.js const asyncResolver = ResolverFactory.createResolver({ conditionNames: ["node"], extensions: [".js", ".json", ".node"], fileSystem: nodeFileSystem }); +/// Land here when invoking resolve function. /** * @type {ResolveFunctionAsync} */