Which is Better Vite Plugins for Import Optimization?
vite-plugin-style-import vs vite-plugin-imp
1 Year
vite-plugin-style-importvite-plugin-imp
What's Vite Plugins for Import Optimization?

Vite plugins are essential tools that enhance the development experience by optimizing the way modules and styles are imported in a Vite application. Both 'vite-plugin-imp' and 'vite-plugin-style-import' serve the purpose of improving the efficiency of imports, but they do so in slightly different ways. 'vite-plugin-imp' focuses on optimizing the import of components and styles from libraries, ensuring that only the necessary parts are included in the final bundle. On the other hand, 'vite-plugin-style-import' specifically targets the import of styles, allowing for a more granular control over how styles are included, which can lead to reduced bundle sizes and improved load times. Both plugins aim to streamline the development process and enhance performance, making them valuable additions to any Vite project.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
vite-plugin-style-import24,29844421.9 kB35-MIT
vite-plugin-imp18,55223146 kB15a year agoMIT
Feature Comparison: vite-plugin-style-import vs vite-plugin-imp

Import Optimization

  • vite-plugin-style-import: This plugin focuses specifically on optimizing style imports. It allows you to import styles on-demand, which means that only the styles that are actually used in your components are included in the final bundle, minimizing the CSS footprint.
  • vite-plugin-imp: This plugin intelligently analyzes your imports and ensures that only the necessary components and styles are included in the final build. By reducing the amount of unused code, it helps in creating leaner bundles that load faster, improving overall application performance.

Ease of Use

  • vite-plugin-style-import: Similarly, 'vite-plugin-style-import' is straightforward to implement. It provides clear documentation and examples, allowing developers to easily manage their style imports without complicating their build process.
  • vite-plugin-imp: Designed to be user-friendly, 'vite-plugin-imp' requires minimal configuration. Developers can quickly integrate it into their Vite projects without extensive setup, making it accessible for both beginners and experienced developers.

Performance Impact

  • vite-plugin-style-import: Focusing solely on styles, this plugin can drastically reduce the CSS size in your application. By ensuring that only the necessary styles are imported, it enhances the performance of your application, particularly in terms of rendering speed.
  • vite-plugin-imp: By optimizing both component and style imports, this plugin significantly reduces the size of the final bundle. This leads to faster load times and improved performance, especially in large applications with many dependencies.

Compatibility

  • vite-plugin-style-import: While primarily focused on styles, this plugin also supports various CSS-in-JS solutions and pre-processors, ensuring that it fits well within modern development workflows.
  • vite-plugin-imp: This plugin is compatible with a wide range of libraries and frameworks, making it a versatile choice for developers working in diverse environments. It integrates seamlessly with existing projects, ensuring that you can leverage its benefits without major changes.

Community Support

  • vite-plugin-style-import: This plugin also enjoys strong community backing, with active discussions and contributions that help improve its functionality and documentation, making it easier for new users to adopt.
  • vite-plugin-imp: With a growing user base, 'vite-plugin-imp' benefits from community contributions and support. Developers can find a wealth of resources, including tutorials and troubleshooting guides, enhancing the overall experience of using the plugin.
How to Choose: vite-plugin-style-import vs vite-plugin-imp
  • vite-plugin-style-import: Choose 'vite-plugin-style-import' if your primary concern is the efficient management of styles in your project. This plugin allows you to selectively import styles, which can be particularly useful in projects where style management is crucial for performance.
  • vite-plugin-imp: Choose 'vite-plugin-imp' if you want a comprehensive solution that optimizes both component and style imports from libraries, ensuring that only the required code is included in your bundle, thus improving performance and reducing load times.
README for vite-plugin-style-import

vite-plugin-style-import

English | 中文

npm node

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


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

        ↓ ↓ ↓ ↓ ↓ ↓

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


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
import { UserConfigExport } from 'vite'
import styleImport, {
  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: [
      styleImport({
        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

{
  // 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;

  // https://github.com/anncwb/vite-plugin-style-import/pull/5
  // Used in some situations where components may need to be introduced on demand, not just to introduce styles.(Libraries that don't support Esm well)
  // Only work in production
  resolveComponent?: (name: string) => string;

  // https://github.com/anncwb/vite-plugin-style-import/issues/12
  // `import ${libName} from 'xxxx';`
  // Used for custom import name
  // Only work in production
  transformComponentImportName?: (name: string) => string;
}

// 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


cd ./example

yarn install

yarn serve

Sample project

Vben Admin

License

MIT