更新记录

2.3.0(2026-01-14) 下载此版本

Full Changelog: https://github.com/cnguu/vite-plugin-uni-cdn/compare/v2.2.0...v2.3.0

2.2.0(2026-01-06) 下载此版本

Full Changelog: https://github.com/cnguu/vite-plugin-uni-cdn/compare/v2.1.1...v2.2.0

2.1.1(2025-12-26) 下载此版本

Full Changelog: https://github.com/cnguu/vite-plugin-uni-cdn/compare/v2.1.0...v2.1.1

查看更多

平台兼容性

uni-app(4.75)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟

uni-app x(4.75)

Chrome Safari Android iOS 鸿蒙 微信小程序

vite-plugin-uni-cdn

Vite 插件,在 uni-app 中替换静态资源链接为 CDN 链接

release license

安装

pnpm i -D @cnguu/vite-plugin-uni-cdn

使用

// vite.config.ts
import UniCdn from '@cnguu/vite-plugin-uni-cdn'
import Uni from '@dcloudio/vite-plugin-uni'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    UniCdn({
      cdn: 'https://cdn.jsdelivr.net/gh/cnguu/vite-plugin-uni-cdn@main/packages/playground/src/static/cdn',
      sourceDir: 'src/static/cdn',
    }),
    Uni(),
  ],
  build: {
    // 控制 sourcemap 生成
    sourcemap: true,
  },
})

ts 中使用辅助函数

import { withCdn } from 'virtual:vite-plugin-uni-cdn'

console.log(withCdn('/static/cdn/7.png'))

注意:

  1. 目前只支持绝对路径,如 /static/xxx/xxx.png
  2. 微信小程序平台会被 uni-app 自动转成 Base64,无法被替换为 cdn 链接

参数

  • cdn
    • cdn 地址
    • 默认 ''
  • sourceDir
    • 替换资源目录,不在该目录下的资源不会被替换
    • 默认 'static/cdn'
  • include
    • 扫描白名单 GLOB 格式
    • 默认 ['**/*.vue']
  • exclude
    • 扫描黑名单 GLOB 格式
    • 默认 ['**/node_modules/**', '**/uni_modules/**', '**/dist/**', '**/unpackage/**']
  • deleteOutputFiles
    • 是否删除替换资源目录对应的输出目录
    • 默认 true
  • verbose
    • 是否输出命令行信息
    • 默认 true
  • aliOSS
    • 配置上传阿里云 OSS
    • 默认 undefined
    • enable
      • 启用
      • 默认 false
    • options
      • ali-oss 实例初始化参数,文档
      • 默认 undefined
    • headers
      • ali-oss 上传请求头,文档
      • 默认 undefined

tsconfig 配置

{
  "compilerOptions": {
    "types": [
      "@cnguu/vite-plugin-uni-cdn/virtual"
    ]
  }
}

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2025 cnguu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.