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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-uni-wx-copy

v0.0.5

Published

一个帮助你在 uni-app 项目中复用原生微信小程序页面的插件

Readme

vite-plugin-uni-wx-copy

一个帮助你在 uni-app 项目中复用原生微信小程序页面的混合开发插件。

阅读 《uni-app 与原生小程序混合开发方案》 了解详细背景。

为什么需要这个插件?

满足以下场景:

  • 需要在 uni-app 项目中集成部分原生微信小程序页面
  • 正在将原生微信小程序项目迁移到 uni-app,需要渐进式重构

特性

  • 完美支持原生微信小程序和 uni-app 混合开发模式
  • 自动将原生微信小程序页面和组件复制到 uni-app 构建产物中
  • 支持主包和分包页面
  • 自定义文件内容重写
  • 自动路径解析和替换
  • 开发模式下支持热重载

安装

pnpm add vite-plugin-uni-wx-copy -D

最佳实践

项目结构

推荐的项目结构如下:

.
├── src/                 # uni-app 项目主目录
│   ├── pages/          # uni-app 页面
│   ├── components/     # uni-app 组件
│   └── ...
├── miniprogram/        # 原生微信小程序代码目录(可选)
│   ├── components/     # 需要复用的原生组件
│   ├── pages/          # 需要复用的原生页面
│   └── ...
├── vite.config.ts      # Vite 配置文件
└── ...

配置步骤

  1. 在 uni-app 项目的 vite.config.ts 中添加插件:
import uni from '@dcloudio/vite-plugin-uni'
import { defineConfig } from 'vite'
import uniWxCopy from 'vite-plugin-uni-wx-copy'

export default defineConfig({
  plugins: [
    uni(),
    uniWxCopy({
      rootDir: '../miniprogram',
      // 复制共享资源
      copy: [
        {
          sources: [
            'components',
            'static',
            'utils',
          ],
          dest: 'shared/',
          shared: true,
        },
      ],
      // 主包页面
      pages: [
        'pages/index',
        'pages/page1',
        'pages/page2',
      ],
      // 分包配置
      subPackages: [
        {
          root: 'subpackages',
          pages: ['detail'],
        },
      ],
    })
  ]
})

配置

插件选项

| 选项 | 类型 | 默认值 | 说明 | |--------|------|---------|-------------| | debug | boolean | false | 启用调试模式,显示详细日志 | | rootDir | string | '' | 原生微信小程序代码的根目录 | | pages | string[] | [] | 需要复制的主包页面路径列表 | | subPackages | Array<{ root: string, pages: string[] }> | [] | 需要复制的分包页面列表 | | copy | Array<CopyItem> | [] | 需要复制的文件和目录 | | rewrite | Array<WriteItem> | [] | 文件内容重写配置 | | replaceRules | Array<ReplaceRule> | [] | 路径替换规则 |

类型定义

interface CopyItem {
  // 需要复制的源目录列表
  sources: string[]
  // 目标目录
  dest: string
  // 是否为共享资源
  shared?: boolean
}

interface WriteItem {
  // 目标文件路径
  file: string
  // 修改文件内容的函数
  write: (content: string) => string
}

interface ReplaceRule {
  // 要匹配的正则表达式或字符串
  from: RegExp | string
  // 替换的目标字符串或函数
  to: string | ((match: string, ...args: string[]) => string)
  // 要处理的文件(glob 模式)
  files: string[]
}

实际开发示例

完整配置示例

以下是一个包含常见场景的完整配置示例:

import uni from '@dcloudio/vite-plugin-uni'
import { defineConfig } from 'vite'
import uniWxCopy from 'vite-plugin-uni-wx-copy'

export default defineConfig({
  plugins: [
    uni(),
    uniWxCopy({
      rootDir: '../miniprogram',
      // 1. 复制共享资源到独立命名空间
      copy: [
        {
          sources: [
            'components', // 公共组件
            'static', // 静态资源
            'utils', // 工具库
          ],
          dest: 'shared/',
          shared: true,
        },
      ],
      // 2. 配置页面
      pages: [
        'pages/index',
        'pages/page1',
        'pages/page2',
      ],
      // 3. 分包配置
      subPackages: [
        {
          root: 'subpackages',
          pages: ['detail'],
        },
      ],
      // 4. 重写配置文件
      rewrite: [
        {
          // 注册全局组件
          file: 'app.json',
          write: (code) => {
            const appJson = JSON.parse(code)
            appJson.usingComponents = {
              ...appJson.usingComponents,
              'app-btn': '/shared/components/app-btn/app-btn',
            }
            return JSON.stringify(appJson, null, 2)
          },
        },
        {
          // 修改环境配置
          file: 'shared/config/index.js',
          write: (code) => {
            return code.replace(
              /export const DEV =(.+)/,
              `export const DEV = ${process.env.NODE_ENV === 'development'}`
            )
          },
        },
      ],
      // 5. 路径替换规则
      replaceRules: [
        {
          // 替换鉴权方法调用
          from: /auth\.getUserInfo\(/g,
          to: 'getApp().getUserInfo(',
          files: ['pages/**/*.js', 'shared/**/*.js'],
        },
      ],
    }),
  ],
})

2. 分包配置

vite.config.ts 中配置分包:

  1. 开发调试
    • 启动开发服务器,插件会自动处理文件同步
    • 修改原生代码时自动触发热重载
    • 查看构建日志确保配置正确

你可以参考 playground 目录下的示例项目来进行配置和开发。