Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .agents/skills/mpx2rn/references/rn-json-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ mpx.navigateTo({

跨分包使用其他分包内的自定义组件时,需在 **`usingComponents`** 的路径上声明 **`?root=对方分包名`**,并在 **`componentPlaceholder`** 中指定**已在本包 `usingComponents` 注册**的同步占位组件或基础组件(如 view / text 等);占位组件本身不能再标记为异步。构建侧需开启 **`mpx.config.rnConfig.supportSubpackage`**,并可配置 `asyncChunk.loading` / `fallback` 等。

被多个异步分包共享的公共 JS 模块默认由构建抽取为独立的 `async-common` 分包并按需懒加载;若在 `mpx.config.js` 的 `pluginOptions.mpx.plugin` 中设置 **`asyncCommonSubpackage: false`**,则不再单独输出该分包,公共模块合并进 app 主入口 bundle(**RN 与 Web 输出下均生效**),可省去一次 `async-common` 异步下载。

概念与写法与官方 [分包异步化 - 跨分包自定义组件](https://mpxjs.cn/guide/advance/async-subpackage.html) 一致;**微信、支付宝、Web、RN** 等环境下框架对该能力有支持(非支持端会自动降级)。

**示例:**
Expand Down
22 changes: 22 additions & 0 deletions docs-vitepress/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,28 @@ module.exports = defineConfig({
* 本功能只会对使用require.async异步引用的js模块生效,若引用路径中已配置?root,则以路径中?root优先
:::

### asyncCommonSubpackage

`boolean = true`

在 Web 与 RN 输出中,控制被多个异步分包共享的公共模块如何输出:

- 为 `true`(默认)时,公共模块由 `SplitChunksPlugin` 的 `async` cacheGroup 抽取到 `async-common/index.js`,运行时按需加载。
- 为 `false` 时,公共模块直接合并进 app 主入口 chunk,不再输出 `async-common/index.js`,异步分包可直接复用主包中的公共模块。

```js
// mpx.config.js
module.exports = defineConfig({
pluginOptions: {
mpx: {
plugin: {
asyncCommonSubpackage: false
}
}
}
})
```

### transSubpackageRules

`Array`
Expand Down
5 changes: 3 additions & 2 deletions packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class MpxWebpackPlugin {
options.forceMainPackageRules = options.forceMainPackageRules || {}
options.forceProxyEventRules = options.forceProxyEventRules || {}
options.disableRequireAsync = options.disableRequireAsync || false
options.asyncCommonSubpackage = options.asyncCommonSubpackage !== undefined ? options.asyncCommonSubpackage : true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个在webConfiig和rnConfig中分包新增一个同名配置分别控制吧

options.miniNpmPackages = options.miniNpmPackages || []
options.normalNpmPackages = options.normalNpmPackages || []
options.fileConditionRules = options.fileConditionRules || {
Expand Down Expand Up @@ -1308,8 +1309,8 @@ class MpxWebpackPlugin {
}
if (!hasOwn(splitChunksOptions.cacheGroups, 'async')) {
splitChunksOptions.cacheGroups.async = {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个cacheGroup在不是async-common的情况下,命名也改一下吧,还叫async有点奇怪

chunks: 'async',
name: 'async-common/index',
chunks: this.options.asyncCommonSubpackage ? 'async' : 'all',
name: this.options.asyncCommonSubpackage ? 'async-common/index' : mpx.appInfo.name,
minChunks: 2,
minSize: 1
}
Expand Down
Loading