This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
Prefer parent directory over index for module name #208
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is purely an aesthetic request, but one that I think would make reading the output of Rollup much easier.
Currently, when this plugin is determining a module's name [0], it uses the
basename
of theid
[1] to determine the module's name. If theid
is/node_modules/SomePackage/module.js
, we end up with the module namemodule
. If theid
was/node_modules/SomePackage/index.js
, then we end up with the module nameindex
.The difference between these two is that for the former, we have to explicitly state that we want to import from that file, while for the latter, we only have to provide the package, since
index.js
is the default file to resolve to.Anecdotally, I do the latter a lot more, which means that I end up with a lot of
index
(and deconflictedindex$1
, etc.) variables throughout the bundle. Functionally this is fine, but it makes it difficult to follow when actually reading through the bundle's code and Ctrl+f forindex
has a few dozen results.What this PR does is to check the output of
getName
prior to returning, and if it isindex
(and the user has specified to avoidindex
names), then the name of the parent segment from the directory path is used instead.This also takes into account directory names that are invalid as variable names.
Currently, this PR doesn't have any tests. I took a look at the current tests and TBH I wasn't exactly sure how I should be testing this. If this is something that you decide to move forward with, I'll obviously add tests, but I could use some guidance on their setup.