This repository was archived by the owner on Aug 4, 2021. It is now read-only.
Prefer parent directory over index for module name#208
Merged
Rich-Harris merged 1 commit intorollup:masterfrom Aug 22, 2017
pshrmn:rename-index
Merged
Prefer parent directory over index for module name#208Rich-Harris merged 1 commit intorollup:masterfrom pshrmn:rename-index
Rich-Harris merged 1 commit intorollup:masterfrom
pshrmn:rename-index
Conversation
Contributor
Author
|
I forgot to mention that this would be non-default behavior, which only would be applied when the user specifies to do this in the options. commonjs({
avoidIndexName: true
}) |
Contributor
|
Thanks for the PR @pshrmn — this is a great idea, and I actually think it should be default behaviour rather than opt-in, since there's really no disadvantage to it that I can think of. Any chance you could amend the PR to remove the option? It would need a test, yeah — easiest way is probably to adapt an existing test and use an assertion like this to verify that it's not using |
Contributor
Author
|
I removed the option and added a test. There are three cases that it verifies:
|
Contributor
|
thank you! released as 8.2.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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
basenameof theid[1] to determine the module's name. If theidis/node_modules/SomePackage/module.js, we end up with the module namemodule. If theidwas/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.jsis 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 forindexhas a few dozen results.What this PR does is to check the output of
getNameprior to returning, and if it isindex(and the user has specified to avoidindexnames), 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.