Home

Awesome

English | 简体中文

vite-plugin-auto-alias

automatically generate alias based on path

<p align="center"> <img src="https://img.shields.io/npm/v/vite-plugin-auto-alias" alt='version'> <img src="https://img.shields.io/npm/dm/vite-plugin-auto-alias" alt='download'> <img src="https://img.shields.io/github/issues/jwyGithub/vite-plugin-auto-alias" alt='issues'> <img src="https://img.shields.io/github/license/jwyGithub/vite-plugin-auto-alias" alt='license'> </p> <br />

Features

Install

pnpm

pnpm add vite-plugin-auto-alias -D

yarn

yarn add vite-plugin-auto-alias -D

npm

npm install vite-plugin-auto-alias -D

Use

vite.config.ts / vite.config.js

import autoAlias from 'vite-plugin-auto-alias';

export default defineConfig(({ command, mode }) => {
    return {
        plugins: [autoAlias()]
    };
});

Option

export interface AutoAlias {
    /**
     * @description the root directory where the alias needs to be generated is src by default
     * @default src
     */
    root?: string;

    /**
     * @description prefix for generating aliases
     * @default @
     */
    prefix?: string;

    /**
     * @description synchronize the mode of json configuration
     * @default all
     */
    mode?: 'sync' | 'off';

    /**
     * @description alias configuration file path
     * @default tsconfig.json
     */
    aliasPath?: string;
}

Mode

vite.config.ts / vite.config.js

import autoAlias from 'vite-plugin-auto-alias';

export default defineConfig(({ command, mode }) => {
    return {
        plugins: [
            autoAlias({
                // ...
                mode: 'sync'
            })
        ]
    };
});

tsconfig.json / jsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./"
        // ...
    }
}

Example

|-- src
    |-- plugins
    |-- router
    |-- scss
    |-- store
    |-- utils
    |-- views
    |-- ....
import xxx from '@plugins/xxx';
import xxx from '@router/xxx';
import xxx from '@scss/xxx';
import xxx from '@store/xxx';
import xxx from '@utils/xxx';
import xxx from '@views/xxx';