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

Expose resolvePath #194

Merged
merged 9 commits into from
Jun 20, 2017
Merged

Expose resolvePath #194

merged 9 commits into from
Jun 20, 2017

Conversation

fatfisz
Copy link
Contributor

@fatfisz fatfisz commented Jun 11, 2017

As discussed in #189, there is a need for exposing some of the internal methods (importing from package/lib/* is not really a future-proof solution, because this is an undocumented API).

Also normalizeOptions is rewritten to prevent modifying the passed options object.

@codecov
Copy link

codecov bot commented Jun 11, 2017

Codecov Report

Merging #194 into master will not change coverage.
The diff coverage is 100%.

Impacted Files Coverage Δ
src/resolvePath.js 100% <100%> (ø)
src/transformers/call.js 100% <100%> (ø) ⬆️
src/utils.js 100% <100%> (ø) ⬆️
src/normalizeOptions.js 100% <100%> (ø) ⬆️
src/index.js 100% <100%> (ø) ⬆️

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 11, 2017

I'm adding the docs, will need a review of them.

@tleunen
Copy link
Owner

tleunen commented Jun 12, 2017

Thanks, I'll review this tomorrow.

@tleunen
Copy link
Owner

tleunen commented Jun 12, 2017

Do you think it make sense to expose normalizeOptions?
I wonder if we should change our logic to make getRealPath take the initial option, or maybe just export a new function (resolvePath?) that will normalize and then use our internal getRealPath?

I'm just saying that because normalizing is really internal to our lib (mostly because of the custom cwd values and alias regexes).

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 12, 2017

Makes sense. Let me sleep on that :)

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 13, 2017

Ok, I think I'm for including normalization in getRealPath and renaming it to resolvePath instead of adding another function. What do you say?

@tleunen
Copy link
Owner

tleunen commented Jun 13, 2017

Yep we can do that too

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 13, 2017

It turned out that the options are also used before resolvePath (in the call transformer, for the methods) and so I had an idea to put the normalized options in the state and pass the state as this. If there is normalizedOpts on this, then resolvePath uses it; else it computes normalizedOpts itself. The second scenario might happen if the function is called in the userland.

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 13, 2017

Both cases were already covered by the tests, so I didn't have to add any 😉

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 15, 2017

@tleunen If you're ok, I'd go ahead and merge it.

@tleunen
Copy link
Owner

tleunen commented Jun 16, 2017

I'm checking it that now. Thanks for the update @fatfisz

src/utils.js Outdated
const currentFile = state.file.opts.filename;
const opts = state.opts;

const modulePath = resolvePath.call(state, sourcePath, currentFile, opts);
Copy link
Owner

@tleunen tleunen Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm a fan of using resolvePath with a state context.

I believe I'd prefer that the function we export for the external use would be a new one that normalize and then use the internal resolvePath.

In a way, it feels weird to have resolvePath requiring to be called on a speficic context, it should be stateless.

Let me know what you think, we can discuss about it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it can be stateless; this is just a crutch so that the original resolvePath can reuse the normalized options if they are there. Without state as this the function will just normalize by itself.

I agree that it may be over-optimized (normalizing seems to be quite cheap). Do you think it's better to get rid of this for clarity?

Copy link
Owner

@tleunen tleunen Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of calling it with resolvePath(sourcePath, currentFile, state.normalizedOpts).

And then the function we export in index.js would be a custom function doing:

 export { 
  resolvePath: (sourcePath, currentFile, opts) => {
    const normalizedOpts = normalizeOptions(currentFile, opts);
    return resolvePath(sourcePath, currentFile, normalizedOpts);
  }
 };

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But later (with the new resolvePath option) this call will need opts as opposed to normalizedOpts:

const modulePath = opts.resolvePath(sourcePath, currentFile, opts);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll go ahead and eliminate the context then, since we seem to agree on that 😃

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 17, 2017

I think there's a value in having the same singature for the internal resolvePath and the external one; also options are being normalized specifically because of the needs of resolvePath, so it makes sense to just put it inside instead of exporting a wrapper function. What do you think?

@tleunen
Copy link
Owner

tleunen commented Jun 19, 2017

I see your point, but forcing to normalize the options again and again might impact the performance, especially since it might not be needed.

The pre hook was done on a per file basis, am I right? I never remember that and I can't find the doc on that now.
But if it's the case, we'll normalize the options once, and then multiple times for each import/require statement.

If this is really the way we want to go, to keep the same signature for the internal and external resolvePath, we'd need to add memoization on the normalize fn.

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 19, 2017

Yes, I was thinking the same thing - that's why I've added the state as this, to get the already normalized options from it. But maybe applying a more straightforward memoization is better? I think lodash.memoize could do it.

@tleunen
Copy link
Owner

tleunen commented Jun 19, 2017

Yes, and I believe we could also optimize normalizeOptions and especiallynormalizeCwd if instead of passing the currentFile, we actually use path.dirname(currentFile), don't you think?
That way, we'll really gain benefit from the memoization, since all files from the same directory will reuse the same normalized options.

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 19, 2017

Brilliant!

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 19, 2017

I settled on reselect because it required less configuration to use (lodash.memoize would need a special resolver function and a modified storage, because by default it handles only the first argument).

@tleunen tleunen merged commit 547578f into master Jun 20, 2017
@tleunen
Copy link
Owner

tleunen commented Jun 20, 2017

This will require a new version for the eslint plugin too. I will see if I can move the eslint plugin in this repo too. It will be easier to maintain.

@tleunen tleunen deleted the expose-some-internals branch June 20, 2017 22:28
@fatfisz fatfisz changed the title Expose getRealPath and normalizeOptions Expose resolvePath Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants