Improve plugin resolution when path does not start with ./#4451
Improve plugin resolution when path does not start with ./#4451azz merged 6 commits intoprettier:masterfrom kachkaev:fix-plugin-resolution
Conversation
|
What about if someone wants to use a plugin that doesn't match the auto-load pattern? e.g. |
|
Please note that I'm keeping |
|
@azz good point. I guess we should decide between allowing people to use Practically speaking, how likely would people use |
|
Maybe we can just offer a better error message if you forget the |
|
That can be a good option too. So do I just try/catch and then throw a custom error message if the original one starts with |
|
We could search locally if the provided string ends in |
|
We may also want to support the string ending in |
|
What if we do this? let requirePath;
try {
// try local files
requirePath = resolve.sync(path.resolve(process.cwd(), pluginName));
} catch (e) {
// try node modules
requirePath = resolve.sync(pluginName, { basedir: process.cwd() });
}If the second attempt fails, Prettier will crash. This can make things smooth for both types of uses IMO. The reason why we may want As of |
|
That's a clean solution, I like it 👍 |
|
@suchipi that's done now, just waiting for the tests to pass (Travis builds are a bit bumpy today). |
I noticed that passing
--plugin=path/to/pluginrather than--plugin=./path/to/plugincrashes Prettier. This happens because it tries to resolvepath/to/pluginas a node module rather than a relative path. The error says:This PR fixes that and thus makes usage of
--pluginmore predictable. ~~~It is unlikely that someone would want to refer to a local node module, because a plugin from there would be autoloaded anyway (as long as the instance of Prettier is local).~~~ The new behaviour supports both node module names and paths without leading./.I also tweak a couple of minor things given the opportunity, please see the commit log.