0.2.0 ā€¢ Published 2 months ago

unplugin-generate-component-name v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

unplugin-generate-component-name

A plugin for auto generate vue component name.

Features
  • šŸ’š Supports Vue 3 out-of-the-box.
  • āš”ļø Supports Vite, Webpack, Rspack, Vue CLI, Rollup, esbuild and more, powered by unplugin.
  • šŸŖ Folder names and Setup extend two patterns.
  • šŸ¦¾ Full TypeScript support.

Install

# Yarn
$ yarn add unplugin-generate-component-name -D

# Pnpm
$ pnpm i unplugin-generate-component-name -D
// vite.config.ts
import GenerateComponentName from 'unplugin-generate-component-name/vite'

export default defineConfig({
  plugins: [
    GenerateComponentName({ /* options */ }),
  ],
})

// rollup.config.js
import GenerateComponentName from 'unplugin-generate-component-name/rollup'

export default {
  plugins: [
    GenerateComponentName({ /* options */ }),
  ],
}

// webpack.config.js
module.exports = {
  /* ... */
  plugins: [
    require('unplugin-generate-component-name/webpack').default({ /* options */ }),
  ],
}

Usage

Folder names

  • You can use the name of the directory where the Index Component resides as the name of the component.

Generating Vue Component Names

In Vue, we can use the unplugin-generate-component-name plugin to automatically generate component names based on directory names. This plugin makes it easier and more intuitive to find and manage components in a large codebase. For instance, let's say we have a Vue component named Index.vue, and this component is in the Home directory. With the unplugin-generate-component-name plugin, this component will automatically be named Home.

src/home/
ā”œā”€ā”€ index.vue // component name is Home
ā”œā”€ā”€ about.vue
ā””ā”€ā”€ users/
    ā”œā”€ā”€ index.vue // component name is Users
    ā””ā”€ā”€ info.vue

Setup Extend

Continuing with the Setup Extend demo is writing the name="Home" on the script tag.

<template>
  <!-- Your component marker -->
</template>

<script setup name="Home">
  // Your script logic
</script>

<style>
  <!-- Your component style -->
</style>

In the <script setup> tag, we set the name attribute to "Home". This explicitly names the component "Home", and the unplugin-generate-component-name plugin will use this name instead of "Index".

Please note that you should first install and correctly configure the plugin in your vite.config.js or webpack.config.js file.

Options

type GenComponentName = (opt: {
    filePath: string;
    dirname: string;
    originalName: string;
    attrName: string | undefined;
}) => string;
interface PattenOptions {
    include?: string | RegExp | (string | RegExp)[];
    exclude?: string | RegExp | (string | RegExp)[];
    genComponentName: GenComponentName;
}
interface Options extends Omit<PattenOptions, 'genComponentName'> {
    enter?: PattenOptions[];
}

include

The include option is utilized to specifically state the files that should be processed for component name auto-generation. This safeguard can be specified using a string, a regular expression, or an array that can hold a collection of both.

exclude

On the flip side, the exclude option operates by dictating the files that should abstain from the auto-generation process. This restriction can also be imposed using a string, a regular expression, or a combination of both held in an array.

enter

In the Options interface, there's an enter option. enter is an array where each object acts as a specific set of rules for handling different file paths.

Each set of rules can include include and exclude options which specify which files need special handling. Their value can be a string, a RegExp, or an array consisting of strings and RegExps. You can also specify a genComponentName function for custom component name generation.

Here's an example:

// vite.config.ts
import GenerateComponentName from 'unplugin-generate-component-name/vite'

export default defineConfig({
  plugins: [
     GenerateComponentName({
      include: ['**/*.vue'],
      enter: [{
        include: ["**/*index.vue"],
        genComponentName: ({ attrName, dirname }) => attrName ?? dirname
      }, {
        exclude: ['**/*.index.vue'],
        include: ["src/components/**/*.vue"],
        genComponentName: ({ dirname, originalName }) => `${dirname}-${originalName}`
      }]
    }),
  ],
});

In this example, the unplugin-generate-component-name plugin is configured to process all .vue files. There are two objects within the enter option for different file paths:

  • The first object covers all files that end with "index.vue". The genComponentName function returns the component name. If a name is already specified in the script setup tag, it will be prioritized; otherwise, the directory name (dirname) will be used.

  • The second object excludes all files ending with "index.vue" and only includes .vue files within the "src/components/" directory. A genComponentName function is used to generate the component name in the format of ${dirname}-${originalName}.For instance, for a file namedMyButton.vue in src/components/Button, it will beButton-MyButton.

0.2.0

2 months ago

0.1.9

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.4

5 months ago

0.1.3

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.0

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago