Skip to content

feat: directory namespaces support #8

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

Merged
merged 18 commits into from
Nov 4, 2020
Merged

Conversation

Subwaytime
Copy link
Contributor

Hello there,
so ive created a smaller Pull Request that basically implements some Features that i had in my own vite-autoloader.
Due to it not beeing in Typescript ive converted all Features that were missing now over to this Plugin!

Features:

  1. Allow Folder Names ( boolean )

    This is just a simple Idea, if you for example got the following Folder Structure:

    src/components
    	book.vue
    	ui
    		button.vue
    	layout
    		content.vue
    	transition
    		fade.vue
    

    Components within these Subfolders would be generated like the following:

    book.vue, uiButton.vue , layoutContent.vue, transitionFade.vue

    This allows Users to have Componentnames based on their Foldestructure.

  2. Namespacing ( array with strings )
    An Additional Feature to the first one: the user can allow specific Foldernames to be ignored within their Folderstructure:

     src/components
    	global
    		avatar.vue
    	partials
    		listItem.vue
    

    This would result in the components beeing named as: avatar.vue, listItem.vue

  3. Include_dirs and exclude_dirs ( array with strings )
    Pretty Simple, ive just changed dirs to include_dirs to make it more recognizable.
    I wanted to allow the Options also to exclude_dirs. This would mean that a User could ignore certain Folders within the include_dirs from getting autoloaded.

    src/components
    	test.vue (loaded)
    	ui
    		button.vue (loaded)
    	partials
    		listItem.vue (ignored)
    

    Unfortunately i didnt had the time to implement this in the current Repo, but i will try to add it later on!

  4. Bugs:
    Currently there is only one Bug existent which is, if a component is loaded its name will normalize from context.ts.

    src/components
    	ui
    		button.vue
    

    Ends up with: Uibutton.vue rather then uiButton.vue. I could change this behavior from normalize to kebabCase, but this would enforce a specific Loadingstyle. It would be great if a component could be loaded no matter how it is written: ui-button.vue, uiButton.vue, Uibutton.vue, UiButton.vue etc
    But due to my Typescript beeing a littly rusty i couldnt find a quick Solution that didnt break everything! I also fixed one Issue that index-named components were not loaded properly if they got their parent directory name.

    Last but not least: I dont like the current allowFolderNames Option, so if someone finds a better name for it, feel free to change it! and please verify if this works on MACOS and Linux, because i am currently limited to Windows 10 64Bit.

    Greetings,

    Subwaytime

Change "dirs" to "include_dirs" (easier to understand)
Add Namespaces as array filled with strings
Add allowFolderNames as boolean
Add Split, Join and isEmpty Helpers
Update getNameFromFilePath to support new Features: Namespacing and allowFolderNames
Fix Windows Issue with "index-named" components
Adjust new Options (dirs -> include_dirs)
Call new Options within getNameFromFilePath
@Subwaytime
Copy link
Contributor Author

Subwaytime commented Oct 29, 2020

Oh and small Editnote:
My Typescript isnt really good, so there are potential some fixes need to be made, but it works like this atm. Also my VSCode couldve maaaaybe changed the Linting quite a bit.. ^^

@antfu
Copy link
Member

antfu commented Oct 30, 2020

Thanks and the feature is good to have! I will have a closer look next Monday, before that, can you revert the formatting changes first? I would like to use 2 spaces for indentation and no semi. Thanks.

@Subwaytime
Copy link
Contributor Author

Sure will do!

Copy link
Member

@antfu antfu left a comment

Choose a reason for hiding this comment

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

Looks good but a few naming things.

I will pull down to test out when we made these changes.

Thanks!

README.md Outdated
ViteComponents()
]
}
plugins: [ViteComponents()],
Copy link
Member

Choose a reason for hiding this comment

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

Could you revet formatting change in this file? Thanks.

src/types.ts Outdated
@@ -6,22 +6,40 @@ export interface Options {
* Relative paths to the directory to search for components.
* @default 'src/components'
*/
dirs: string | string[]
include_dirs: string | string[]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
include_dirs: string | string[]
dirs: string | string[]

It's better to not introduce a breaking change. And I think it's ok to call it dirs for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have you thought about the one Feature that i didnt had time to figure out?
exclude_dirs, thats basically the only reason why i swapped this to include_dirs, other then that i agree!
Atm it would be breaking, but if the exclude feature isnt available for now, i would leave it at dirs!

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, maybe we can just expose inlcudeGlobs & excludeGlobs later on to it so users can have better controls on this.

src/types.ts Outdated
* @default false
*/

allowFolderNames: boolean
Copy link
Member

Choose a reason for hiding this comment

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

Not very share about the naming. But allow something does not sounds good to an optional feature. Maybe:

  • folderToPrefix
  • folderNamespace

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah right, both options i didnt like!
folderNamespace would be good

src/types.ts Outdated
* Only works if Foldernames are allowed
* @default "['global', 'partials']"
*/
namespaces: string[]
Copy link
Member

Choose a reason for hiding this comment

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

namespaces might be too general for this feature. Maybe

  • globalNamespaces
  • globalPrefixes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

globalNamespaces sounds better here!

src/utils.ts Outdated
Comment on lines 23 to 29
export function split(str: string, sep: string) {
return str == '' ? [] : str.split(sep)
}

export function join(arr: string[], sep: string) {
return arr == null ? '' : Array.prototype.join.call(arr, sep)
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not necessarily, just added them for convience, made the code a little bit more readable

{ label: 'First Level' },
{ label: 'First Level', children: [{ label: 'Second Level' }] },
{ label: "First Level" },
{ label: "First Level", children: [{ label: "Second Level" }] },
Copy link
Member

Choose a reason for hiding this comment

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

Please revert the formating, thanks!

src/utils.ts Outdated
@@ -28,17 +44,51 @@ export function matchGlobs(filepath: string, globs: string[]) {
return false
}

export function getNameFromFilePath(filePath: string): string {
export function getNameFromFilePath(filePath: string, includedFrom: string | string[], allowFolderNames: boolean, namespaces: string[]): string {
Copy link
Member

Choose a reason for hiding this comment

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

We can pass the entire options here

Suggested change
export function getNameFromFilePath(filePath: string, includedFrom: string | string[], allowFolderNames: boolean, namespaces: string[]): string {
export function getNameFromFilePath(filePath: string, options: Options): string {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right! Completely oversaw that 👍

@antfu
Copy link
Member

antfu commented Nov 4, 2020

A few changes:

  • folderNamespace -> directoryAsNamespace
  • the default value of globalNamespaces is now an empty array.
  • namespaces will now require camelCases for constructing. e.g. ui/button.vue will be used as <UiButton> or <ui-button> instead of <Uibutton>
  • some logic changes to handle more edge cases (nest names, etc), check the fixture for more details.

Thanks for your work!

@antfu antfu changed the title Add new Features for Component Autoloading and Naming feat: directory namespaces support Nov 4, 2020
@antfu antfu merged commit 5e9b3ba into unplugin:master Nov 4, 2020
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.

2 participants