Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some comments that I've learned except fileSystem. #441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions demo.js
Original file line number Diff line number Diff line change
@@ -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 });
});
19 changes: 19 additions & 0 deletions lib/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -535,6 +545,8 @@ class Resolver {
};
}

/// message example.
/// resolve 'graceful-fs' in './'
const message = `resolve '${request}' in '${path}'`;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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<string> | 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)) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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} */
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down