diff --git a/docs-vuepress/api/compile.md b/docs-vuepress/api/compile.md index 260b736b5..f9eca8aee 100644 --- a/docs-vuepress/api/compile.md +++ b/docs-vuepress/api/compile.md @@ -1097,6 +1097,25 @@ module.exports = defineConfig({ }) ``` +### disableRequireAsync + +`boolean = false` + +Mpx 框架在输出 微信小程序、支付宝小程序、字节小程序、Web 平台时,默认支持分包异步化能力,但若在某些场景下需要关闭该能力,可配置该项。 + +```js +// vue.config.js +module.exports = defineConfig({ + pluginOptions: { + mpx: { + plugin: { + disableRequireAsync: true + } + } + } +}) +``` + ### optimizeSize `boolean = false` diff --git a/docs-vuepress/guide/advance/async-subpackage.md b/docs-vuepress/guide/advance/async-subpackage.md index 4a401d666..72b24681d 100644 --- a/docs-vuepress/guide/advance/async-subpackage.md +++ b/docs-vuepress/guide/advance/async-subpackage.md @@ -5,6 +5,14 @@ 具体功能介绍和功能目的可 [点击查看](https://developers.weixin.qq.com/miniprogram/dev/framework/subpackages/async.html), Mpx对于分包异步化功能进行了完整支持。 +当前 Mpx 框架默认支持以下平台的分包异步化能力: +* 微信小程序 +* 支付宝小程序 +* 字节小程序 +* Web + +在非上述平台,异步分包代码会默认降级。 + ## 跨分包自定义组件引用 >一个分包使用其他分包的自定义组件时,由于其他分包还未下载或注入,其他分包的组件处于不可用的状态。通过为其他分包的自定义组件设置 占位组件, 我们可以先渲染占位组件作为替代,在分包下载完成后再进行替换。 @@ -88,5 +96,4 @@ require.async('../commonPackage/index.js?root=subPackageB').then(pkg => { }) ``` -- 注意项:目前该能力仅微信平台下支持,其他平台下框架将会自动降级 diff --git a/packages/webpack-plugin/lib/index.js b/packages/webpack-plugin/lib/index.js index 0ff066af6..2b231cd55 100644 --- a/packages/webpack-plugin/lib/index.js +++ b/packages/webpack-plugin/lib/index.js @@ -173,6 +173,7 @@ class MpxWebpackPlugin { options.subpackageModulesRules = options.subpackageModulesRules || {} options.forceMainPackageRules = options.forceMainPackageRules || {} options.forceProxyEventRules = options.forceProxyEventRules || {} + options.disableRequireAsync = options.disableRequireAsync || false options.miniNpmPackages = options.miniNpmPackages || [] options.fileConditionRules = options.fileConditionRules || { include: () => true @@ -708,7 +709,8 @@ class MpxWebpackPlugin { useRelativePath: this.options.useRelativePath, removedChunks: [], forceProxyEventRules: this.options.forceProxyEventRules, - supportRequireAsync: this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode), + // 若配置disableRequireAsync=true, 则全平台构建不支持异步分包 + supportRequireAsync: !this.options.disableRequireAsync && (this.options.mode === 'wx' || this.options.mode === 'ali' || this.options.mode === 'tt' || isWeb(this.options.mode)), partialCompileRules: this.options.partialCompileRules, collectDynamicEntryInfo: ({ resource, packageName, filename, entryType, hasAsync }) => { const curInfo = mpx.dynamicEntryInfo[packageName] = mpx.dynamicEntryInfo[packageName] || {