Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support require async options #1686

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions docs-vuepress/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
9 changes: 8 additions & 1 deletion docs-vuepress/guide/advance/async-subpackage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

具体功能介绍和功能目的可 [点击查看](https://developers.weixin.qq.com/miniprogram/dev/framework/subpackages/async.html), Mpx对于分包异步化功能进行了完整支持。

当前 Mpx 框架默认支持以下平台的分包异步化能力:
* 微信小程序
* 支付宝小程序
* 字节小程序
* Web

在非上述平台,异步分包代码会默认降级。

## 跨分包自定义组件引用
>一个分包使用其他分包的自定义组件时,由于其他分包还未下载或注入,其他分包的组件处于不可用的状态。通过为其他分包的自定义组件设置 占位组件,
我们可以先渲染占位组件作为替代,在分包下载完成后再进行替换。
Expand Down Expand Up @@ -88,5 +96,4 @@ require.async('../commonPackage/index.js?root=subPackageB').then(pkg => {
})
</script>
```
- 注意项:目前该能力仅微信平台下支持,其他平台下框架将会自动降级

4 changes: 3 additions & 1 deletion packages/webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] || {
Expand Down
Loading