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 🙏

© 2025 – Pkg Stats / Ryan Hefner

vite-inset-loader

v2.0.1

Published

在Vite构建的Vue应用中,编译阶段在SFC模板指定位置插入自定义内容,适用于需要在每个页面引入组件的小程序场景。

Readme

vite-inset-loader

一个专为 UniApp(Vue3 + Vite)设计的编译期注入插件集合,现提供两个入口(互斥,二选一):

  • UniViteRootInjector(推荐):高性能、类型友好,支持按页面/按需注入与自动路由类型生成。
  • UniViteInsetLoader(兼容):沿用旧版 vite-inset-loader 的 pages.json 驱动注入方式。

文档入口:

性能建议:大型项目优先使用 UniViteRootInjector,具备路由映射缓存与增量计算优化,整体构建体验更好。

demo

demo 仓库

安装

pnpm add vite-inset-loader -D
# 或
npm i vite-inset-loader -D
# 或
yarn add vite-inset-loader -D

插件入口(二选一)

| 入口函数 | 适用场景 | 特性 | 性能 | | --- | --- | --- | --- | | UniViteRootInjector(推荐) | 需要灵活注入、类型提示、按需配置 | 缓存 pages.json、按需注入、自动生成路由类型 | ⭐️⭐️⭐️⭐️⭐️ | | UniViteInsetLoader | 兼容旧版行为、沿用 pages.json 的标签注入 | 不改动旧配置、快速接入 | ⭐️⭐️⭐️ |

注意:两个入口不能同时启用。若在同一 Vite 配置中同时使用,会抛出互斥错误。

使用 UniViteRootInjector(推荐)

import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import { UniViteRootInjector } from 'vite-inset-loader';
import { resolve } from 'node:path';
// 若配置 dts,请在项目中显式引用生成的类型
import type { Path } from './types/auto-page.d';

const components = {
  privacyModal: '<privacyModal></privacyModal>',
  message: '<GyMessage ref="messageRef"></GyMessage>',
  dialog: '<GyDialog ref="dialogRef"></GyDialog>',
  messageBox: '<wd-message-box></wd-message-box>',
  toast: '<wd-toast />',
} as const;

export default defineConfig({
  plugins: [
    uni(),
    UniViteRootInjector<Path, typeof components>({
      dts: resolve(__dirname, 'types/auto-page.d.ts'),
      components,
      insertPos: {
        mode: 'GLOBAL',
        exclude: ['login' as Path], // Path 类型:pages/login/index -> login
        handlePos: [
          { page: 'home' as Path, insert: ['message'] }, // Path 类型:pages/home/index -> home
          { page: 'sub_initiateEvaluation' as Path, insert: ['toast'] }, // 分包:subPackages/sub/initiateEvaluation/index -> sub_initiateEvaluation
        ],
      },
    }),
  ],
});

RootInjector 配置项(节选)

  • dts: 路由类型文件生成路径,缺省生成到默认位置
  • components: 组件别名到组件字符串的映射,支持类型推断
  • insertPos: 注入策略配置
    • mode: 'GLOBAL'(全局模式)
    • exclude: Path[] - 排除的页面路径,使用 Path 类型枚举值
      • 主包页面:pages/home/indexhome
      • 分包页面:subPackages/sub/initiateEvaluation/indexsub_initiateEvaluation
    • handlePos: 页面特定配置,其中 page 参数为 Path 类型枚举值(生成规则同上)
  • includes/watchFile: 可选的包含与监听配置

使用 UniViteInsetLoader(兼容模式)

import { defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import { UniViteInsetLoader } from 'vite-inset-loader';

export default defineConfig(() => ({
  plugins: [
    UniViteInsetLoader({ include: 'src' }),
    uni(),
  ],
}));

viteInsetLoader 方法配置项

| 属性 | 说明 | 类型 | 默认值 | 必填 | | --- | --- | --- | --- | --- | | include | 过滤需要插入组件的目录路径(错误路径将导致不生效) | string | string[] | 'src' | 否 |

pages.json 配置(用于 InsetLoader)

insetLoader 依赖 pages.json 中的配置来注入标签:

{
  "insetLoader": {
    "config": {
      "message": "<GyMessage ref='messageRef'></GyMessage>",
      "dialog": "<GyDialog ref='dialogRef'></GyDialog>"
    },
    "label": ["message", "dialog"],
    "package": {
      "label": "span",
      "options": {
        "class": "dev-style",
        "style": { "font-size": "24px" },
        "data-attr": "content"
      }
    }
  }
}

页面级覆盖示例(优先级高于全局配置):

{
  "pages": [
    {
      "path": "pages/home/index",
      "style": {
        "label": ["message"],
        "package": {
          "label": "span",
          "options": {
            "class": "dev-style",
            "style": { "font-size": "24px" },
            "data-attr": "123468"
          }
        }
      }
    }
  ]
}

注册全局组件(仅 InsetLoader 场景)

import { createSSRApp } from 'vue';
import App from './App.vue';
import GyMessage from './components/GyMessage/index.vue';

export const createApp = () => {
  const app = createSSRApp(App);
  app.component('GyMessage', GyMessage); // 需与 pages.json 的 config 别名匹配
  return { app };
};

常见问题

  • 两个入口不能同时使用:内部会检测并抛出错误,用任意一个即可。
  • RootInjector 性能更好:更适合页面较多或注入规则复杂的项目。
  • 仅命名导出:从 v1.0.10 起包只提供命名导出(不再有默认导出)。

进一步阅读

  • UniViteRootInjector 使用与配置(推荐):./docs/UniViteRootInjector.md
  • UniViteInsetLoader 使用与配置(兼容):./docs/UniViteInsetLoader.md

致谢

  • vite-inset-loader 的灵感来源于 vue-inset-loader(早期适配 Webpack + Vue2)。本项目在其基础上适配了 Vite + Vue3 并进行了能力扩展与性能优化。

欢迎提出你的建议与 PR:GitHub 仓库