DCloud_UNI_JBB
DCloud_UNI_JBB
  • 发布:2025-09-25 19:37
  • 更新:2026-09-24 20:28
  • 阅读:8893

微信、支付宝、抖音等小程序支持分包异步化---跨分包自定义组件引用

分类:uni-app

背景

微信小程序支持分包异步化 跨分包自定义组件引用,但是 uniapp 目前只支持在 pages.json 下的页面中配置 componentPlaceholder, 并不支持在某个具体的组件中添加此配置。

可以通过这个 插件 https://github.com/uni-toolkit/uni-toolkit 来解决此问题

特性

  • 配置简单,开箱即用
  • 支持 vue、nvue、uvue 三种文件
  • 支持 uni-app 和 uni-app-x 项目
  • 支持 组合式 和 选项式 两种写法
  • 支持 cli 项目和 hx 项目
  • 支持通过 mp-* 顶层配置为不同小程序平台生成专属配置

0.4.0 变更

从 0.4.0 起,<component-config> 使用原生 JSON.parse 解析,标签内必须是严格 JSON,不支持注释、尾逗号、单引号以及条件编译指令。

整个 <component-config> 仍可以在外层使用 #ifdef MP 包裹。需要区分具体小程序平台时,请在 JSON 顶层使用 mp-* 配置:

// #ifdef MP  
<component-config>  
{  
  "x": 1,  
  "mp-weixin": { "y": 2 },  
  "mp-alipay": { "z": 3 }  
}  
</component-config>  
// #endif
  • 编译到 mp-weixin 时得到 { "x": 1, "y": 2 }
  • 编译到 mp-alipay 时得到 { "x": 1, "z": 3 }
  • 编译到其他小程序平台时只得到 { "x": 1 }
  • mp-weixin、mp-alipay 等平台键本身不会写入最终产物

外层的 #ifdef MP 用于隔离非小程序构建;具体小程序平台之间的差异应使用 mp-* 配置,不要在 JSON 内部使用条件编译。

Vue3

Node.js >= 20.19.0

安装

npm install @uni_toolkit/vite-plugin-component-config -D  
# 或  
pnpm add @uni_toolkit/vite-plugin-component-config -D  
# 或  
yarn add @uni_toolkit/vite-plugin-component-config -D

使用方法

配置 vite.config.js

// vite.config.js  
import { defineConfig } from 'vite'  
import uni from '@dcloudio/vite-plugin-uni'  
import componentConfig from '@uni_toolkit/vite-plugin-component-config'  

export default defineConfig({  
  plugins: [  
    componentConfig(), // 在 uni 插件之前调用  
    uni(),  
  ]  
})

修改 Vue 文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

<script setup>  
import test from './test.vue'  
</script>  

// #ifdef MP  
<component-config>  
{  
  "componentPlaceholder": {  
    "test": "view"  
  }  
}  
</component-config>  
// #endif

Vue2

Node.js >= 20.19.0

安装

npm install @uni_toolkit/webpack-plugin-component-config -D  
# 或  
pnpm add @uni_toolkit/webpack-plugin-component-config -D  
# 或  
yarn add @uni_toolkit/webpack-plugin-component-config -D

使用方法

配置 vue.config.js

const WebpackComponentConfigPlugin = require('@uni_toolkit/webpack-plugin-component-config').default;  

module.exports = {  
  configureWebpack: {  
    plugins: [  
      new WebpackComponentConfigPlugin()  
    ]  
  }  
};

修改 Vue 文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

<script>  
import test from './test.vue'  

export default {  
  components: {  
    test  
  }  
}  
</script>  

// #ifdef MP  
<component-config>  
{  
  "componentPlaceholder": {  
    "test": "view"  
  }  
}  
</component-config>  
// #endif

Tips

如果你是 vue3 项目,并且单纯只想添加 componentPlaceholder 配置,可以使用 https://github.com/chouchouji/vite-plugin-component-placeholder

安装

// npm  
npm i @binbinji/vite-plugin-component-placeholder -D  

// yarn  
yarn add @binbinji/vite-plugin-component-placeholder -D  

// pnpm  
pnpm add @binbinji/vite-plugin-component-placeholder -D

使用

1. 引入插件 @binbinji/vite-plugin-component-placeholder

// vite.config.*  
import componentPlaceholderPlugin from '@binbinji/vite-plugin-component-placeholder'  
import uni from '@dcloudio/vite-plugin-uni'  
import { defineConfig } from 'vite'  

// https://vitejs.dev/config/  
export default defineConfig({  
  plugins: [uni(), componentPlaceholderPlugin()],  
})

2. 在 vue/nvue/uvue 文件中添加配置

在 vue 相关文件中添加 componentPlaceholder 配置

组合式文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

<script setup>  
import test from './test.vue'  

defineOptions({  
  componentPlaceholder: {  
    test: 'view',  
  }  
})  
</script>

选项式文件

<template>  
  <view>test page</view>  
  <test></test>  
</template>  

<script>  
import test from './test.vue'  

export default {  
  components: {  
    test,  
  },  
  componentPlaceholder: {  
    test: 'view',  
  }  
}  
</script>
7 赞 关注 分享
7***@qq.com julytian [已删除] zzdev twentyLee 4***@qq.com 3***@qq.com

要回复文章请先登录或注册

DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 x***@gmail.com :
我刚刚更新了文档,你可以升级到 0.4.0
2026-09-24 20:28
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 x***@gmail.com :
https://github.com/uni-toolkit/uni-toolkit 提个issue,附带上可复现demo
2026-09-24 16:33
x***@gmail.com

x***@gmail.com

回复 x***@gmail.com :
我查了一下这个插件的源码,问题可能是:插件在 succeedModule 阶段就解析 <component-config>,这时候 uni-app 的条件编译上下文可能还没初始化完成,导致 MP-WEIXIN 判断为 false,整个 // #ifdef 块被直接移除,所以生成的 JSON 里根本没有这段配置。

你看到的"要么都生效要么都不生效",很可能就是因为上下文为空时所有平台判断都是 false。

验证方法
编译时看控制台有没有这类警告:


Failed to parse component-config in ...
如果没有报错、但 JSON 里也没内容,基本就是上下文没初始化,#ifdef 被当成 false 移除了。

ai排查的
2026-09-24 15:14
x***@gmail.com

x***@gmail.com

条件编译不行啊,要么都没有编译进去,要么都编译进去了,
<component-config>
// #ifdef MP-WEIXIN
{
"usingComponents": {},
"componentPlaceholder": {}
}
// #endif
</component-config>

// #ifdef MP-WEIXIN 写在哪个地方都试过了,支付宝小程序和微信小程序的编译结果都是一样的
2026-09-24 15:05
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 x***@163.com :
vue2还是vue3?有没有可复现代码
2026-09-20 17:43
x***@163.com

x***@163.com

回复 x***@163.com :
编译成微信小程序报错了
2026-08-09 16:44
x***@163.com

x***@163.com

ERROR Unexpected token (Note that you need plugins to import files that are not JavaScript)
at pages/componentsPage/chart/chart.vue:4:24
2: // 此处必须是标准的 json 对象,支持条件编译
3: {
4: "componentPlaceholder": {
^
5: "test": "view"
6: }


ERROR Build failed with errors.
2026-08-09 16:42
ksd123

ksd123

回复 ksd123 :
最近一直有个疑问,好想向您请教下,方便留个微信号吗
2026-05-12 22:42
ksd123

ksd123

回复 DCloud_UNI_JBB :
哥,群二维码过期了
2026-05-12 22:39
DCloud_UNI_JBB

DCloud_UNI_JBB (作者)

回复 g***@163.com :
h5 你正常写就行,和不包含 uniapp 的 vue 项目一样
2026-04-07 17:07