Skip to content

Commit ad4b403

Browse files
authored
Merge pull request #2575 from didi/feat-rn-asyncCommonSubpackage
[feat]support rn asyncCommonSubpackage
2 parents 92cc724 + f2495a5 commit ad4b403

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎.agents/skills/mpx2rn/references/rn-json-reference.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ mpx.navigateTo({
185185

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

188+
被多个异步分包共享的公共 JS 模块默认由构建抽取为独立的 `async-common` 分包并按需懒加载;若在 `mpx.config.js` 的 `pluginOptions.mpx.plugin.rnConfig` 中设置 **`asyncCommonSubpackage: false`**,则 RN 输出不再单独生成该分包,公共模块合并进 app 主入口 bundle,可省去一次 `async-common` 异步下载。
189+
188190
概念与写法与官方 [分包异步化 - 跨分包自定义组件](https://mpxjs.cn/guide/advance/async-subpackage.html) 一致;**微信、支付宝、Web、RN** 等环境下框架对该能力有支持(非支持端会自动降级)。
189191

190192
**示例:**

‎docs-vitepress/api/compile.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,16 @@ module.exports = defineConfig({
842842

843843
为 `true` 时 **禁用** 页面切换动画;设为 **`false`** 可 **开启** 切换过渡效果。
844844

845+
#### webConfig.asyncCommonSubpackage
846+
847+
`boolean = true`
848+
849+
控制 Web 输出中被多个异步分包共享的公共模块如何输出:
850+
851+
- 为 `true`(默认)时,公共模块抽取到 `async-common/index.js`,运行时按需加载。
852+
- 为 `false` 时,公共模块合并进 app 主入口 chunk,不再输出 `async-common/index.js`。
853+
854+
845855
#### webConfig.customBuiltInComponents {#webconfig-custombuiltincomponents}
846856

847857
`Record<string, string> | undefined`
@@ -871,6 +881,7 @@ module.exports = defineConfig({
871881
mpx: {
872882
plugin: {
873883
webConfig: {
884+
asyncCommonSubpackage: false,
874885
transRpxFn: function (match, $1) {
875886
if ($1 === '0') return $1
876887
return `${$1 * +(100 / 750).toFixed(8)}vw`
@@ -905,6 +916,15 @@ module.exports = defineConfig({
905916

906917
为 `true` 时,RN 输出下页面与组件可走异步分包与 `import()` 等逻辑;为 `false` 时关闭相关能力。插件初始化时若未传入则默认为 `true`。
907918

919+
#### rnConfig.asyncCommonSubpackage
920+
921+
`boolean = true`
922+
923+
控制 RN 输出中被多个异步分包共享的公共模块如何输出:
924+
925+
- 为 `true`(默认)时,公共模块抽取到 `async-common/index.js`,运行时按需加载。
926+
- 为 `false` 时,公共模块合并进 app 主入口 chunk,不再输出 `async-common/index.js`。
927+
908928
#### rnConfig.asyncChunk
909929

910930
`{ timeout?: number, fallback?: string, loading?: string } | undefined`
@@ -941,6 +961,7 @@ module.exports = defineConfig({
941961
rnConfig: {
942962
projectName: 'MyMpxApp',
943963
supportSubpackage: true,
964+
asyncCommonSubpackage: false,
944965
asyncChunk: {
945966
timeout: 10000,
946967
fallback: path.resolve(__dirname, 'src/rn/PageFallback.mpx'),

‎packages/webpack-plugin/lib/index.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,10 @@ class MpxWebpackPlugin {
220220
cssLangs: ['css', 'less', 'stylus', 'scss', 'sass']
221221
}, options.nativeConfig)
222222
options.webConfig = options.webConfig || {}
223+
options.webConfig.asyncCommonSubpackage = options.webConfig.asyncCommonSubpackage !== undefined ? options.webConfig.asyncCommonSubpackage : true
223224
options.rnConfig = options.rnConfig || {}
224225
options.rnConfig.supportSubpackage = options.rnConfig.supportSubpackage !== undefined ? options.rnConfig.supportSubpackage : true
226+
options.rnConfig.asyncCommonSubpackage = options.rnConfig.asyncCommonSubpackage !== undefined ? options.rnConfig.asyncCommonSubpackage : true
225227
options.partialCompileRules = options.partialCompileRules || null
226228
options.asyncSubpackageRules = options.asyncSubpackageRules || []
227229
options.optimizeRenderRules = options.optimizeRenderRules ? (Array.isArray(options.optimizeRenderRules) ? options.optimizeRenderRules : [options.optimizeRenderRules]) : []
@@ -1309,10 +1311,14 @@ class MpxWebpackPlugin {
13091311
}
13101312
needInit = true
13111313
}
1312-
if (!hasOwn(splitChunksOptions.cacheGroups, 'async')) {
1313-
splitChunksOptions.cacheGroups.async = {
1314-
chunks: 'async',
1315-
name: 'async-common/index',
1314+
const asyncCommonSubpackage = isWeb(mpx.mode)
1315+
? this.options.webConfig.asyncCommonSubpackage
1316+
: this.options.rnConfig.asyncCommonSubpackage
1317+
const cacheGroupName = asyncCommonSubpackage ? 'async' : 'mainCommon'
1318+
if (!hasOwn(splitChunksOptions.cacheGroups, cacheGroupName)) {
1319+
splitChunksOptions.cacheGroups[cacheGroupName] = {
1320+
chunks: asyncCommonSubpackage ? 'async' : 'all',
1321+
name: asyncCommonSubpackage ? 'async-common/index' : mpx.appInfo.name,
13161322
minChunks: 2,
13171323
minSize: 1
13181324
}

0 commit comments

Comments
 (0)