Skip to content

Commit

Permalink
合并master提交
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiaokou committed Sep 27, 2023
2 parents ff64b39 + bbc7d6a commit 8e074da
Show file tree
Hide file tree
Showing 41 changed files with 489 additions and 756 deletions.
1 change: 1 addition & 0 deletions docs-vuepress/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports = {
description: '深度性能优化的增强型小程序开发框架'
},
},
shouldPrefetch: () => false,
plugins: {
'@vuepress/pwa': {
serviceWorker: true,
Expand Down
24 changes: 24 additions & 0 deletions docs-vuepress/api/app-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,27 @@ mpx.config.webRouteConfig = {
mode: 'history'
}
```

## errorHandler
- **类型**`Function`

- **默认值**`null`

- **用法**

```js
mpx.config.errorHandler = function (errmsg, location, error) {
// errmsg: 框架内部运行报错的报错归类信息,例如当执行一个watch方法报错时,会是 "Unhandled error occurs during execution of watch callback!"
// location: 具体报错的代码路径,可选项,不一定存在
// error: 具体的错误堆栈,可选项,不一定存在
// handle error
}
```

Mpx 框架运行时报错捕获感知处理函数。

* Mpx 框架生命周期执行错误;
* Mpx 中的 computed、watch 等内置方法执行报错;
* Mpx 框架的运行时的检测报错,例如存在目标平台不支持的属性,入参出参类型错误等;

同时被捕获的错误会通过 console.error 输出。
80 changes: 78 additions & 2 deletions docs-vuepress/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ module.exports = defineConfig({
})
```
:::

### externalClasses

- **类型**`Array<string>`
Expand Down Expand Up @@ -488,6 +489,7 @@ module.exports = defineConfig({
})
```
:::

### autoSplit

- **类型**`boolean`
Expand Down Expand Up @@ -1126,8 +1128,6 @@ module.exports = defineConfig({
```
:::
### i18n
```js
Expand Down Expand Up @@ -1404,6 +1404,82 @@ module.exports = defineConfig({
该特性只能用于**开发环境**,默认情况下会阻止所有页面(**入口 app.mpx 除外**)的打包。
:::
### asyncSubpackageRules
- **类型**:
```ts
type Condition = string | Function | RegExp

interface AsyncSubpackageRules {
include: Condition | Array<Condition>
exclude?: Condition | Array<Condition>
root: string
placeholder: string | { name: string, resource?: string}
}
```
* include: 同 webpack include 规则
* exclude: 同 webpack exclude 规则
* root: 匹配规则的组件或js模块的输出分包名
* placeholder: 匹配规则的组件所配置的componentPlaceholder,可支持配置原生组件和自定义组件,原生组件可直接以string类型配置,自定义组件需要配置对象,name 为该自定义组件名, resource 为自定义组件的路径,路径可为绝对路径和相对于项目目录的相对路径
- **详细**:异步分包场景下批量设置组件或 js 模块的异步分包,提升资源异步分包输出的灵活性。
- **示例**:
```js
// include 可以是正则、字符串、函数、数组
new MpxWebpackPlugin({
asyncSubpackageRules: [
{
include: '/project/pages', // 文件路径包含 '/project/pages' 的组件或者 require.async 异步引用的js 模块都会被打包至sub1分包
root: 'sub1',
placeholder: 'view'
}
]
})
// 若配置自定义组件
new MpxWebpackPlugin({
asyncSubpackageRules: [
{
include: '/project/pages', // 文件路径包含 '/project/pages' 的组件或者 require.async 异步引用的js 模块都会被打包至sub1分包
root: 'sub1',
placeholder: {
name: 'other-placeholder',
resource: '/user/xxxx/other.mpx' // 自定义组件的绝对路径
}
}
]
})
```
::: tip @mpxjs/[email protected] 版本配置如下
```js
// vue.config.js
module.exports = defineConfig({
pluginOptions: {
mpx: {
plugin: {
// include 可以是正则、字符串、函数、数组
asyncSubpackageRules: [
{
include: '/project/pages', // 文件路径包含 '/project/pages' 的组件或者 require.async 异步引用的js 模块都会被打包至sub1分包
root: 'sub1',
placeholder: 'view'
}
]
}
}
}
})
```
:::
:::warning
* 该配置匹配的组件,若使用方在引用路径已设置?root或componentPlaceholder,则以引用路径中的?root或componentPlaceholder为最高优先级
* 若placeholder配置使用自定义组件,注意一定要配置 placeholder 中的 resource 字段
* 本功能只会对使用require.async异步引用的js模块生效,若引用路径中已配置?root,则以路径中?root优先
:::
## MpxWebpackPlugin static methods
`MpxWebpackPlugin` 通过静态方法暴露了以下五个内置 loader,详情如下:
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"packages": [
"packages/*"
],
"version": "2.8.40"
"version": "2.8.47"
}
4 changes: 1 addition & 3 deletions packages/api-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpxjs/api-proxy",
"version": "2.8.40",
"version": "2.8.46",
"description": "convert miniprogram API at each end",
"module": "src/index.js",
"types": "@types/index.d.ts",
Expand Down Expand Up @@ -37,8 +37,6 @@
},
"homepage": "https://github.com/didi/mpx#readme",
"dependencies": {
"@mpxjs/core": "^2.8.47",
"@mpxjs/utils": "^2.8.15",
"axios": "^0.21.1"
}
}
27 changes: 13 additions & 14 deletions packages/api-proxy/src/platform/api/event-channel/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class EventChannel {
}
}

off (eventName, EventCallback) {
if (EventCallback) {
off (eventName, listener) {
if (listener) {
const cbs = this.listener[eventName]
const copyCbs = []
if (cbs) {
cbs.forEach((item) => {
if (item.fn !== EventCallback) {
if (item.fn !== listener) {
copyCbs.push(item)
}
})
Expand All @@ -36,26 +36,25 @@ class EventChannel {
}
}

on (eventName, EventCallback) {
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'on' })
on (eventName, listener) {
this._addListener(eventName, listener, 'on')
}

once (eventName, EventCallback) {
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type: 'once' })
once (eventName, listener) {
this._addListener(eventName, listener, 'once')
}

_addListener (eventName, EventCallback, type) {
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: EventCallback, type })
_addListener (eventName, listener, type) {
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: listener, type })
}

_addListeners (events) {
if (Object.prototype.toString.call(events) === '[object Object]') {
Object.keys(events).forEach((eventName) => {
(this.listener[eventName] || (this.listener[eventName] = [])).push({ fn: events[eventName], type: 'on' })
})
}
Object.keys(events).forEach((eventName) => {
this.on(eventName, events[eventName])
})
}
}

export {
EventChannel
}
26 changes: 17 additions & 9 deletions packages/api-proxy/src/platform/api/route/index.web.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { webHandleSuccess, webHandleFail, isTabBarPage } from '../../../common/js'
import { EventChannel } from '../event-channel'

let routeCount = 0

function redirectTo (options = {}) {
const router = global.__mpxRouter
if (router) {
Expand All @@ -10,9 +12,13 @@ function redirectTo (options = {}) {
return Promise.reject(res)
}
router.__mpxAction = { type: 'redirect' }
if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
router.replace(
{
path: options.url
path: options.url,
query: {
routeCount: ++routeCount
}
},
() => {
const res = { errMsg: 'redirectTo:ok' }
Expand Down Expand Up @@ -42,9 +48,13 @@ function navigateTo (options = {}) {
if (options.events) {
eventChannel._addListeners(options.events)
}
if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
router.push(
{
path: options.url
path: options.url,
query: {
routeCount: ++routeCount
}
},
() => {
const res = { errMsg: 'navigateTo:ok', eventChannel }
Expand Down Expand Up @@ -72,19 +82,17 @@ function navigateBack (options = {}) {
}
}

let reLaunchCount = 0

function reLaunch (options = {}) {
const router = global.__mpxRouter
if (router) {
if (reLaunchCount === 0 && router.currentRoute.query.reLaunchCount) reLaunchCount = router.currentRoute.query.reLaunchCount
const delta = router.stack.length - 1
if (routeCount === 0 && router.currentRoute.query.routeCount) routeCount = router.currentRoute.query.routeCount
router.__mpxAction = {
type: 'reLaunch',
path: options.url,
reLaunchCount: ++reLaunchCount,
routeCount: ++routeCount,
replaced: false
}
const delta = router.stack.length - 1
// 在需要操作后退时,先操作后退,在beforeEach中基于当前action通过next()进行replace操作,避免部分浏览器的表现不一致
if (delta > 0) {
router.go(-delta)
Expand All @@ -94,7 +102,7 @@ function reLaunch (options = {}) {
{
path: options.url,
query: {
reLaunchCount
routeCount
}
},
() => {
Expand Down Expand Up @@ -123,12 +131,12 @@ function switchTab (options = {}) {
webHandleFail(res, options.fail, options.complete)
return Promise.reject(res)
}
const delta = router.stack.length - 1
router.__mpxAction = {
type: 'switch',
path: options.url,
replaced: false
}
const delta = router.stack.length - 1
if (delta > 0) {
router.go(-delta)
} else {
Expand Down
26 changes: 17 additions & 9 deletions packages/core/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ type ArrayType<T extends any[]> = T extends Array<infer R> ? R : never;
// Mpx types
type Data = object | (() => object)

type PropType = StringConstructor | NumberConstructor | BooleanConstructor | ObjectConstructor | ArrayConstructor | null
type PropConstructor<T = any> = {
new (...args: any[]): T & {};
} | {
(): T;
}

interface PropOpt {
type: PropType
optionalTypes?: Array<PropType>
value?: any
export type PropType<T> = PropConstructor<T>

observer? (value: any, old: any, changedPath: string): void
type FullPropType<T> = {
type: PropType<T>;
value?: T;
optionalTypes?: PropType<T>[];
}

interface Properties {
[key: string]: WechatMiniprogram.Component.AllProperty
[key: string]: WechatMiniprogram.Component.AllProperty | PropType<any> | FullPropType<any>
}

interface Methods {
Expand Down Expand Up @@ -78,8 +82,12 @@ type PropValueType<Def> = Def extends {
}
? T
: Def extends (...args: any[]) => infer T
? T
: any;
? T
: Def extends FullPropType<infer T>
? T
: Def extends PropType<infer T>
? T
: any;

type GetPropsType<T> = {
readonly [K in keyof T]: PropValueType<T[K]>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mpxjs/core",
"version": "2.8.40",
"version": "2.8.47",
"description": "mpx runtime core",
"keywords": [
"miniprogram",
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/platform/builtInMixins/pageRouteMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ export default function pageRouteMixin (mixinType) {
return {
beforeCreate () {
this.route = this.$options.__mpxPageRoute || ''
},
methods: {
getOpenerEventChannel () {
const router = global.__mpxRouter
const eventChannel = router && router.eventChannelMap[this.route]
return eventChannel || {}
}
}
}
}
return {
methods: {
getOpenerEventChannel () {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ export default function proxyEventMixin () {
const originValue = valuePath.reduce((acc, cur) => acc[cur], $event.detail)
const value = filterMethod ? (innerFilter[filterMethod] ? innerFilter[filterMethod](originValue) : typeof this[filterMethod] === 'function' && this[filterMethod]) : originValue
setByPath(this, expr, value)
},
getOpenerEventChannel () {
const router = global.__mpxRouter
const eventChannel = router && router.__mpxAction && router.__mpxAction.eventChannel
return eventChannel
}
}
}
Expand Down
Loading

0 comments on commit 8e074da

Please sign in to comment.