Skip to content

Commit 9b3a9bb

Browse files
authored
Merge pull request #2150 from didi/feat-rn-support-disableRequireAsync
[feat]: support web & RN disableRequireAsync
2 parents 2e343f7 + 059aeaa commit 9b3a9bb

5 files changed

Lines changed: 37 additions & 16 deletions

File tree

docs-vitepress/api/compile.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ interface Rules {
4848
}
4949
```
5050

51-
## MpxWebpackPlugin
51+
## MpxWebpackPlugin
5252

5353
Mpx 编译构建跨平台小程序和 web 的 webpack 主插件,安装示例如下:
5454

@@ -1146,7 +1146,7 @@ module.exports = defineConfig({
11461146

11471147
`boolean = false`
11481148

1149-
Mpx 框架在输出 微信小程序、支付宝小程序、字节小程序、Web 平台时,默认支持分包异步化能力,但若在某些场景下需要关闭该能力,可配置该项。
1149+
Mpx 框架在输出 微信小程序、支付宝小程序、字节小程序、Web、RN 平台时,默认支持分包异步化能力,但若在某些场景下需要关闭该能力,可配置该项。
11501150

11511151
```js
11521152
// vue.config.js
@@ -1393,7 +1393,7 @@ module.exports = defineConfig({
13931393

13941394
如果创建项目时未选 unocss,需手动安装,安装示例如下:
13951395

1396-
```bash
1396+
```bash
13971397
npm install -D @mpxjs/unocss-plugin
13981398
pnpm install -D @mpxjs/unocss-plugin
13991399
yarn add -D @mpxjs/unocss-plugin
@@ -1725,7 +1725,7 @@ module.exports = defineConfig({
17251725

17261726
Mpx 内置的 unocss preset,继承自 `@unocss/preset-uno`,并额外提供小程序原子类的预设样式,安装示例如下:
17271727

1728-
```bash
1728+
```bash
17291729
npm install -D @mpxjs/unocss-base
17301730
pnpm install -D @mpxjs/unocss-base
17311731
yarn add -D @mpxjs/unocss-base
@@ -1753,7 +1753,7 @@ yarn add -D @mpxjs/unocss-base
17531753
### baseFontSize
17541754

17551755
`number = 37.5`
1756-
1756+
17571757
同比换算1rem = 37.5rpx适配小程序
17581758
```js
17591759
// uno.config.js
@@ -1867,7 +1867,7 @@ CSS变量的前缀
18671867
```css
18681868
.bg-red-500{--un-bg-opacity:1;background-color:rgba(239,68,68,var(--un-bg-opacity));}
18691869
```
1870-
1870+
18711871
## Request query
18721872

18731873
Mpx中允许用户在request中传递特定query执行特定逻辑,目前已支持的query如下:
@@ -2068,7 +2068,7 @@ module.exports = defineConfig({
20682068
,如果你希望对于部分主包页面或者分包页面配置路由懒加载并想自定义Chunk Name,则可以使用该功能。
20692069

20702070
```html
2071-
// app.mpx
2071+
// app.mpx
20722072
<script type="application/json">
20732073
{
20742074
"pages": [

docs-vitepress/guide/platform/rn.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,11 +2652,11 @@ webviewBridge.invoke('getTime', {
26522652
})
26532653
```
26542654

2655-
#### 异步分包
2655+
#### 分包与异步分包
26562656

2657-
Mpx转RN实现了和微信小程序同等能力的分包异步化功能,基本使用可[参考文档](https://www.mpxjs.cn/guide/advance/async-subpackage.html)
2657+
Mpx转RN实现了和微信小程序同等能力的分包和分包异步化功能,基本使用可[参考文档](https://www.mpxjs.cn/guide/advance/async-subpackage.html)
26582658

2659-
在异步分包的能力实现当中我们借助了RN宿主提供的分包下载执行/分包拉取的 api,因此在你的应用开始使用异步分包的功能之前需要在运行时代码提前部署好RN宿主容器提供的相关 api 以供 Mpx 应用使用:
2659+
在分包和异步分包的能力实现当中我们借助了RN宿主提供的分包下载执行/分包拉取的 api,因此在你的应用开始使用异步分包的功能之前需要在运行时代码提前部署好RN宿主容器提供的相关 api 以供 Mpx 应用使用:
26602660

26612661
```javascript
26622662
mpx.config.rnConfig.loadChunkAsync = function (config) {
@@ -2736,7 +2736,25 @@ module.exports = defineConfig({
27362736
})
27372737
</script>
27382738
```
2739+
关闭输出 RN 分包与异步分包能力:
27392740

2741+
在输出 RN 时,框架默认开启了分包与异步分包能力,如果不希望开启,可以在编译配置中通过 `rnConfig.supportSubpackage = false` 关闭:
2742+
2743+
```javascript
2744+
// mpx.config.js
2745+
module.exports = defineConfig({
2746+
pluginOptions: {
2747+
mpx: {
2748+
plugin: {
2749+
...
2750+
rnConfig: {
2751+
supportSubpackage: false
2752+
}
2753+
}
2754+
}
2755+
}
2756+
})
2757+
```
27402758

27412759
#### 分享
27422760

packages/webpack-plugin/lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class MpxWebpackPlugin {
199199
}, options.nativeConfig)
200200
options.webConfig = options.webConfig || {}
201201
options.rnConfig = options.rnConfig || {}
202+
options.rnConfig.supportSubpackage = options.rnConfig.supportSubpackage !== undefined ? options.rnConfig.supportSubpackage : true
202203
options.partialCompileRules = options.partialCompileRules || null
203204
options.asyncSubpackageRules = options.asyncSubpackageRules || []
204205
options.optimizeRenderRules = options.optimizeRenderRules ? (Array.isArray(options.optimizeRenderRules) ? options.optimizeRenderRules : [options.optimizeRenderRules]) : []
@@ -783,7 +784,7 @@ class MpxWebpackPlugin {
783784
removedChunks: [],
784785
forceProxyEventRules: this.options.forceProxyEventRules,
785786
// 若配置disableRequireAsync=true, 则全平台构建不支持异步分包
786-
supportRequireAsync: !this.options.disableRequireAsync && (this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode) || isReact(this.options.mode)),
787+
supportRequireAsync: !this.options.disableRequireAsync && (this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode) || (isReact(this.options.mode) && this.options.rnConfig.supportSubpackage)),
787788
partialCompileRules: this.options.partialCompileRules,
788789
collectDynamicEntryInfo: ({ resource, packageName, filename, entryType, hasAsync }) => {
789790
const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {

packages/webpack-plugin/lib/react/processScript.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ import { getComponent, getAsyncSuspense } from ${stringifyRequest(loaderContext,
4646
const componentsMap = buildComponentsMap({
4747
localComponentsMap,
4848
loaderContext,
49-
jsonConfig
49+
jsonConfig,
50+
rnConfig
5051
})
5152
output += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, ctorType, jsonConfig, componentsMap, pagesMap, firstPage, hasApp })
5253
output += getRequireScript({ ctorType, script, loaderContext })
@@ -58,7 +59,8 @@ import { getComponent, getAsyncSuspense } from ${stringifyRequest(loaderContext,
5859
localComponentsMap,
5960
builtInComponentsMap,
6061
loaderContext,
61-
jsonConfig
62+
jsonConfig,
63+
rnConfig
6264
})
6365

6466
output += buildGlobalParams({ moduleId, scriptSrcMode, loaderContext, isProduction, ctorType, jsonConfig, componentsMap, outputPath, genericsInfo, componentGenerics, hasApp })

packages/webpack-plugin/lib/react/script-helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function buildPagesMap ({ localPagesMap, loaderContext, jsonConfig, rnConfig })
5959
Object.keys(localPagesMap).forEach((pagePath) => {
6060
const pageCfg = localPagesMap[pagePath]
6161
const pageRequest = stringifyRequest(loaderContext, pageCfg.resource)
62-
if (pageCfg.async) {
62+
if (pageCfg.async && rnConfig.supportSubpackage) {
6363
const moduleId = mpx.getModuleId(pageCfg.resource)
6464
const getFallback = rnConfig.asyncChunk && rnConfig.asyncChunk.fallback && getComponentGetter(getComponent(stringifyRequest(loaderContext, addQuery(rnConfig.asyncChunk.fallback, { isComponent: true })), 'PageFallback'))
6565
const getLoading = rnConfig.asyncChunk && rnConfig.asyncChunk.loading && getComponentGetter(getComponent(stringifyRequest(loaderContext, addQuery(rnConfig.asyncChunk.loading, { isComponent: true })), 'PageLoading'))
@@ -81,14 +81,14 @@ function buildPagesMap ({ localPagesMap, loaderContext, jsonConfig, rnConfig })
8181
}
8282
}
8383

84-
function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig }) {
84+
function buildComponentsMap ({ localComponentsMap, builtInComponentsMap, loaderContext, jsonConfig, rnConfig }) {
8585
const componentsMap = {}
8686
const mpx = loaderContext.getMpx()
8787
if (localComponentsMap) {
8888
Object.keys(localComponentsMap).forEach((componentName) => {
8989
const componentCfg = localComponentsMap[componentName]
9090
const componentRequest = stringifyRequest(loaderContext, componentCfg.resource)
91-
if (componentCfg.async) {
91+
if (componentCfg.async && rnConfig.supportSubpackage) {
9292
const moduleId = mpx.getModuleId(componentCfg.resource)
9393
const placeholder = jsonConfig.componentPlaceholder && jsonConfig.componentPlaceholder[componentName]
9494
let getFallback

0 commit comments

Comments
 (0)