-
Notifications
You must be signed in to change notification settings - Fork 205
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
Feat: warn when the package from resolved alias is not available #160
Conversation
This is not a breaking change, because regular expressions are not really released yet.
Breaking change: aliased paths are resolved again using root and cwd.
Codecov Report
Continue to review full report at Codecov.
|
Codecov Report
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting simplification to transform all alias into regexp and then use them for the path resolver.
I think in most cases, it would resolve the issue, but I wonder if some people specify relative alias path in their babel config without using ./
. Probably not because it would have been taken as a node module by the plugin... (And thus the issue you found).
I believe it's ok. Not sure yet if it's a good thing to do, or warn that the alias doesn't exist in node_modules or in the project, but it exists in ${root}/${alias}
, and then warn the user to update its config.
src/normalizeOptions.js
Outdated
.map((key) => { | ||
let value = alias[key]; | ||
|
||
if (value.startsWith('npm:') && !warnedAboutNpmPrefix) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets drop this for good :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you say so :) I'm just worried about breaking some more configs :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be a 3.0 so no problem removing the deprecated things :) Especially since there's a way to do it without using the prefix.
src/normalizeOptions.js
Outdated
opts.regExps.push([new RegExp(key), substitute]); | ||
const regExpAliases = aliasKeys | ||
.filter(isRegExp) | ||
.map(key => getAliasPair(key, alias[key])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be best to do both case in the same map
loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, will join those cases.
So do you think I should roll back the double-resolve and there should be just a warning if the package is missing? Sorry, maybe it's too late (almost midnight in Poland :D), but I didn't your intent from the second part of the comment. |
Breaking change: support for the "npm:" prefix is removed
Hehe. What I was trying to say is if we should should really resolve with the root config after the alias, or only warn the user the alias config requires relative paths starting with I'm not saying to do that. Just thinking out loud here. Could you explain how the existing code in the PR will work for this config? I'm not sure I fully understand the alias myself. |
Thanks for clearing that up! In the config you've linked all the aliases point at packages or files that are in the packages, so in this case aliases should behave as they did up till now. I have to say though that this config is very confusing now that I've taken a clear look at it - The conflict is only possible when after aliasing you end up with a path that is both a filename available in one of the roots and a package name. But! This is also possible for resolving with the root on its own. At this point however I'm tempted to go with the warning instead. After aliasing a simple So what, should I give it a go? |
Heads up: I'm adjusting the tests and going with the warning. |
Yeah, let's try how a warning will work. (Feel free to keep the code from this PR in case we need it later ;)) We can even simplify it at first by warning when normalizing the options, instead of trying to resolve it |
I'm not sure if warning at the option processing time is a good idea, resolve results may vary depending on the file where the import is. |
Depends what/why we warn though. If we check that the node_module exists OR the path exists, it should be fine doing it in |
What if there is
In this case in |
Hmm, your exemple is a bit weird, since it should probably not really happen. At least for this plugin. Even if you can have that kind of structure with sub modules, I doubt you'll have a alias on something on another module ;) Go for what you have in mind, that's fine :) We'll see how it works |
Ok, let's make life more complicated (sorry, I just love coming up with those absurd scenarios):
Let's say that in the subpackages we want to alias |
ff27386
to
7c71d3e
Compare
@tleunen Finished! |
Thanks @fatfisz, I'll check the PR a bit later today. Sorry for the delay again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's pretty cool.
Just a nit comment, otherwise I'm ok with this update.
Do you have any other changes in mind or we're good to deploy a beta version? If so I'll do in the next few days, after testing it quickly in my project at work.
src/normalizeOptions.js
Outdated
} | ||
}); | ||
|
||
opts.alias = [...nonRegExpAliases, ...regExpAliases]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of this?
opts.alias = aliasKeys.reduce((acc, key) => {
acc.push(
isRegExp(key)
? getAliasPair(key, alias[key])
: getAliasPair(`^${key}((?:/|).*)`, `${alias[key]}\\1`)
);
return acc;
}, []);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to preserve the order: standard aliases before regexps. At this point I think it could go either way, as the order should be taken from the object.
Note: according to the spec, the order of enumerating the object keys is not guaranteed to be the same as in the literal, so in the future it may be needed to allow an array-type declaration. We're currently relying on V8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, one or the other, we'll have an issue if a user expects the order to be kept. With an object, it's not guaranteed as you say. We'll figure out a way later if really needed. For now, it doesn't seem like users need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll go with the single array then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(btw. map
is even better than forEach
here, will use that instead)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed.
src/getRealPath.js
Outdated
try { | ||
resolve.sync(modulePath, { | ||
basedir: currentFile, | ||
extensions: nodeExtensions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed that, but actually it should be the custom extensions the user sets, or the babel extension.
so extensions
from opts
.
And we should reuse this function in findPathInRoots
to remove the duplicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this one - the packages are 3rd party and so they will probably have standard extensions.
OTOH it shouldn't hurt much to include more files in the lookup, as long as they are package-ish (what should be ensured by require.resolve
). In the worst case there will be false positives, which would result in some warnings being silenced, but it doesn't sound very bad.
I'll go with your suggested change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. Maybe I missed a detail, but it was used in the alias config to test the alias was actually pointing to an existing file (or node_modules). By 3rd party, you mean a node_module package, right?
If we really want to be safe, we could use the user/babel extensions for local files/modules and then the node extensions for the ones under node_modules (the ones who don't start with ./). But would it be really useful? Doesn't seem like it's really worth it to me. Again, we'll iterate if really needed, otherwise I'm ok with potential false positives. Anyway, this is only a helper for the user. We never checked the alias was correct in any previous versions so it's already a bit better now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a node_modules
package. And yes, let's just see if anyone complains at all :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, good to merge once you answered my small comment :)
test/index.test.js
Outdated
'regexp-priority$': './miss', | ||
'regexp-priority': './hit', | ||
'regexp-priority': './miss', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coul you explain why this changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test was invalidated because the order is now from the top to the bottom instead of aliases first, then reg exps :)
Breaking change: aliased paths are resolved again using root and cwd.
Breaking change: The "npm:" prefix has been removed.
This is the last feature branch for now. Thanks for bearing with me!
In #144 some users reported that the plugin stopped working for them and the reason was a fix for applying the plugin twice (fixed in #146).
This was not necessarily a problem on the plugin side, but rather bad configuration. Still, an expectation was that the plugin would somehow be working in that case. Here's an example of such configuration:
The repo where this configuration was being used didn't have a package named "components", but rather this was the name of a directory in that repo. The expectation was that "components/1-basic" (bad) would be treated as "./src/components/1-basic" (good).
This change does that - after resolving using the alias, the path is resolved again using root.
I understand that this behavior may be confusing, but this should be alleviated by a "debug" functionality.