# vite-plugin-style-import

> A plug-in that imports component library styles on demand

Latest version **2.0.0** (published 2022-01-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-plugin-style-import
pnpm add vite-plugin-style-import
yarn add vite-plugin-style-import
bun add vite-plugin-style-import
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-01-29 |
| First published | 2021-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 21.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 438 |
| Author | Vben |
| Maintainers | vvben |
| Keywords | vite, style, import, css |

## Links

- npm: https://www.npmjs.com/package/vite-plugin-style-import
- Repository: https://github.com/anncwb/vite-plugin-style-import
- Homepage: https://github.com/anncwb/vite-plugin-style-import/tree/master/#readme
- Issues: https://github.com/anncwb/vite-plugin-style-import/issues
- npm.io page: https://npm.io/package/vite-plugin-style-import

## Dependencies (7)

- [pathe](https://npm.io/package/pathe.md) ^0.2.0
- [console](https://npm.io/package/console.md) ^0.7.2
- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.0
- [change-case](https://npm.io/package/change-case.md) ^4.1.2
- [magic-string](https://npm.io/package/magic-string.md) ^0.25.7
- [es-module-lexer](https://npm.io/package/es-module-lexer.md) ^0.9.3
- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^4.1.2

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2022-01-29
- 2.0.0-beta.1 — 2022-01-29
- 1.4.1 — 2021-12-28
- 1.4.0 — 2021-11-25
- 1.3.0 — 2021-11-02
- 1.2.2-beta.1 — 2021-11-02
- 1.2.1 — 2021-08-23
- 1.2.1-beta.1 — 2021-08-23
- 1.2.0 — 2021-08-23
- 1.2.0-beta.2 — 2021-08-23
- 1.2.0-beta.1 — 2021-08-23
- 1.1.1 — 2021-08-04
- 1.1.0 — 2021-08-02
- 1.0.1 — 2021-06-25
- 1.0.0 — 2021-06-16
- … 41 more at https://npm.io/package/vite-plugin-style-import/versions

## README

# vite-plugin-style-import

**English** | [中文](./README.zh_CN.md)

[![npm][npm-img]][npm-url] [![node][node-img]][node-url]

A plug-in that imports component library styles on demand

## Why only import styles

Because vite itself has imported the component library on demand, only the style is not on demand, so just import the style on demand.

## Install (yarn or npm)

**node version:** >=12.0.0

**vite version:** >=2.0.0

```
yarn add vite-plugin-style-import -D
```

or

```
npm i vite-plugin-style-import -D
```

## Effect

```ts

import { Button } from 'ant-design-vue';

        ↓ ↓ ↓ ↓ ↓ ↓

import { Button } from 'ant-design-vue';
import 'ant-design-vue/es/button/style/index.js';

```

```ts

import { ElButton } from 'element-plus';

        ↓ ↓ ↓ ↓ ↓ ↓

// dev
import { Button } from 'element-plus';
import 'element-plus/lib/theme-chalk/el-button.css`;

// prod
import Button from 'element-plus/lib/el-button';
import 'element-plus/lib/theme-chalk/el-button.css';

```

## Usage

- Config plugin in vite.config.ts

```ts
import { UserConfigExport } from 'vite'
import {
  createStyleImportPlugin,
  AndDesignVueResolve,
  VantResolve,
  ElementPlusResolve,
  NutuiResolve,
  AntdResolve,
} from 'vite-plugin-style-import'

export default (): UserConfigExport => {
  return {
    // 1. If you are using the ant-design series, you need to configure this
    // 2. Make sure less is installed in the dependency `yarn add less -D`
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
    },
    plugins: [
      createStyleImportPlugin({
        resolves: [
          AndDesignVueResolve(),
          VantResolve(),
          ElementPlusResolve(),
          NutuiResolve(),
          AntdResolve(),
        ],
        libs: [
          // If you don’t have the resolve you need, you can write it directly in the lib, or you can provide us with PR
          {
            libraryName: 'ant-design-vue',
            esModule: true,
            resolveStyle: (name) => {
              return `ant-design-vue/es/${name}/style/index`
            },
          },
        ],
      }),
    ],
  }
}
```

### Options

| param    | type                                                  | default                                          | description                                               |
| -------- | ----------------------------------------------------- | ------------------------------------------------ | --------------------------------------------------------- |
| include  | `string、RegExp、(string、RegExp)[]、null、undefined` | `['**/*.js', '**/*.ts', '**/*.tsx', '**/*.jsx']` | Code directory and file format to be converted            |
| exclude  | `string、RegExp、(string、RegExp)[]、null、undefined` | `'node_modules/**'`                              | Excluded files/folders                                    |
| libs     | `Lib[]`                                               |                                                  | List of libraries to be imported                          |
| resolves | `Lib[]`                                               |                                                  | List of libraries to be imported (built-in by the plugin) |

**Lib**

```ts
{
  // Import names that meet this rule will take effect. The default is null, which can be applied to resolveComponent and resolveStyle at the same time
  importTest?: Regexp;

  // Need to imported  library name
  libraryName: string;

  // Custom style file conversion
  resolveStyle: (name: string) => string;

  // Name conversion for library export
  // default: paramCase
  libraryNameChangeCase?: LibraryNameChangeCase;

  // If the style file is not .css suffix. Need to turn on this option
  // default: false
  esModule?: boolean;

  /**
   * There may be some component libraries that are not very standardized.
   * You can turn on this to ignore to determine whether the file exists. Prevent errors when importing non-existent css files.
   * Performance may be slightly reduced after it is turned on, but the impact is not significant
   * default: false
   */
  ensureStyleFile?: boolean;


}

// LibraryNameChangeCase

export type LibraryNameChangeCase = ChangeCaseType | ((name: string) => string);

export type ChangeCaseType =
  | 'camelCase'
  | 'capitalCase'
  | 'constantCase'
  | 'dotCase'
  | 'headerCase'
  | 'noCase'
  | 'paramCase'
  | 'pascalCase'
  | 'pathCase'
  | 'sentenceCase'
  | 'snakeCase';


```

## Example

**Run Example**

```bash
pnpm install
cd packages/playground/basic
pnpm run dev
pnpm run build
```

## Sample project

[Vben Admin](https://github.com/anncwb/vue-vben-admin)

## License

MIT

[npm-img]: https://img.shields.io/npm/v/vite-plugin-style-import.svg
[npm-url]: https://npmjs.com/package/vite-plugin-style-import
[node-img]: https://img.shields.io/node/v/vite-plugin-style-import.svg
[node-url]: https://nodejs.org/en/about/releases/

---
_Source: https://npm.io/package/vite-plugin-style-import · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
