npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lough/vite-plugin-import

v0.3.0

Published

An on-demand import plug-in that is exclusive to Vite.

Downloads

6

Readme

@lough/vite-plugin-import

An on-demand import plug-in that is exclusive to Vite.

Install

npm i @lough/vite-plugin-import -D

or

yarn add @lough/vite-plugin-import -D

Usage

配置会按层级往下兜底

  • libName 预设配置: presetLibConfig => presetConfig => defaultConfig => 插件

  • libConfig 自定义配置: customConfig => presetConfig => defaultConfig => 插件

presetLibConfig 预设库配置

插件内置了 antd 以及 @lyrical/react 的预设库配置

import { defineConfig } from 'vite'
import loughVitePluginImport from '@lough/vite-plugin-import'

export default defineConfig({
  plugins: [
    loughVitePluginImport({
      libList: ['antd', '@lyrical/react']
    })
  ]
})

presetConfig 预设配置

可以统一预设配置

import { defineConfig } from 'vite'
import loughVitePluginImport from '@lough/vite-plugin-import'

export default defineConfig({
  plugins: [
    loughVitePluginImport({
      presetConfig: {
        style: {
          transform: ({ name, directory, importComponentName }) =>
            `${name}/${directory}/components/${importComponentName}/index.css`
        }
      },
      libList: ['null']
    })
  ]
})

defaultConfig 默认配置

没有预设库配置以及预设配置的参数将使用默认配置

import { defineConfig } from 'vite'
import loughVitePluginImport from '@lough/vite-plugin-import'

export default defineConfig({
  plugins: [
    loughVitePluginImport({
      libList: ['null']
    })
  ]
})

customConfig 自定义配置

除了一系列兜底配置处理,还可以自定义特殊处理

import { defineConfig } from 'vite'
import loughVitePluginImport from '@lough/vite-plugin-import'

export default defineConfig({
  plugins: [
    loughVitePluginImport({
      libList: [
        {
          name: 'antd',
          namedType: map => map['dash-named']
        },
        {
          name: '@lyrical/react',
          directory: 'es/components'
        }
      ]
    })
  ]
})

高级的

高级属性可进行复杂的计算,例如:屏蔽单一库等

import { defineConfig } from 'vite'
import loughVitePluginImport from '@lough/vite-plugin-import'

export default defineConfig({
  plugins: [
    loughVitePluginImport(
      {
        name: 'antd',
        namedType: map => map['dash-named'],
        component: false
      },
      {
        libList: [
          {
            name: '@lyrical/react',
            component: {
              transform: ({ name, directory, importComponentName }) =>
                `${name}/${directory}/components/${importComponentName}`
            },
            style: {
              transform: ({ name, directory, importComponentName }) =>
                `${name}/${directory}/components/${importComponentName}/style`,
              excludeNotExistFile: true
            }
          }
        ]
      }
    )
  ]
})

Interface

配置都有类型提示,这里不详细描述了,详情看类型源文件 lib/typings/index.d.ts

/**
 * 引入组件配置
 */
export interface IComponentConfig extends ILibConfig {
  /**
   * 引入组件名称
   */
  importComponentName: string
  /**
   * 引入文件使用组件名称 (因为可能重命名)
   */
  localComponentName: string
}

/**
 * 库处理组件配置
 */
export interface ILibComponentConfig {
  /**
   * 组件引入地址转换
   * @param config 库组件配置
   * @default ({name, directory, importComponentName}) => `${name}/${directory}/${importComponentName}`
   */
  transform: (config: Omit<IComponentConfig, 'component'>) => string
}

/**
 * 库处理样式配置
 */

export interface ILibStyleConfig {
  /**
   * 样式引入地址转换
   * @param config 库组件配置
   * @default ({name, directory, importComponentName}) => `${name}/${directory}/${importComponentName}/style`
   */
  transform: (config: Omit<IComponentConfig, 'style'>) => string
  /**
   * 排除不存在文件
   * @default true
   */
  excludeNotExistFile?: boolean
}

/**
 * 库配置
 */
export interface ILibConfig {
  /**
   * 库名称
   */
  name: string
  /**
   * 库目录
   * @default 'es'
   */
  directory?: string
  /**
   * 命名类型
   * @default 'BigHumpNamed'
   */
  namedType?: NamedTypeFuncMapOrMap
  /**
   * 组件配置
   */
  component?: boolean | ILibComponentConfig
  /**
   * 样式配置
   */
  style?: boolean | ILibStyleConfig
}

/**
 * 插件配置
 */
export interface IPluginConfig {
  /**
   * 默认设置
   */
  presetConfig?: Omit<ILibConfig, 'name'>
  /**
   * 库列表
   */
  libList:
    | Array<ILibConfig | FuncMap<typeof PRESET_LIB_CONFIG, PRESET_LIB_CONFIG>>
    | Array<ILibConfig | keyof typeof PRESET_LIB_CONFIG>
    | Array<ILibConfig | string>
}