Skip to content

Commit

Permalink
Merge branch 'master' into feat-bind-this
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki authored Nov 1, 2023
2 parents ee6b4a8 + ec36a09 commit 2c820d5
Show file tree
Hide file tree
Showing 30 changed files with 579 additions and 506 deletions.
51 changes: 0 additions & 51 deletions docs-vuepress/api/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -1600,57 +1600,6 @@ npm install -D pug pug-plain-loader
在构建过程中忽略特定 `pre-loader` 对文件的处理,仅支持正则表达式,默认值为 `/eslint-loader/`
### proxyComponentEventsRules
- **类型**:`Array<Object> | Object`
- `option.include` 同webpack的include规则
- `option.exclude` 同webpack的exclude规则
- `option.events` 需要在支付宝环境代理的事件,数组形式的支付宝事件名,例如['onTap', 'onTouchMove']
- **详细**:
在微信小程序中,自定义组件本身的那个节点是一个“普通”的节点,使用时可以在这个节点上设置 class style、事件、 flex 布局等,就如同普通的 view 组件节点一样。
在支付宝平台下,自定义组件节点默认是虚拟节点,会展示自定义组件内部的第一层节点,自定义组件节点本身设置的 class style 等不会生效。
跨端输出支付宝时,当自定义组件未开启 virtualHost 时,为了保持和微信一直,Mpx 框架会在自定义组件根节点包裹添加一个 view 节点来保持和微信一致,并将用户在自定义组件节点上设置的
style、class、绑定事件等 copy 至包裹节点,对于事件来说,由于在编译自定义组件时,我们无法感知外部父组件是否绑定事件,我们不能无脑给所有自定义组件包裹节点都绑定上小程序的事件监听,
这样会导致整体运行时开销很大,因此当你需要在支付宝环境中自定义组件根节点上绑定事件时,需要通过次配置来告诉框架需要帮你代理抹平哪些事件。
跨端输出Web时,为了和微信小程序保持一致,在非virtualHost的自定义组件节点上,默认也会插入一个包裹节点,同理对于事件的代理也需要用户进行配置告诉框架来对哪些自定义组件的哪些事件进行代理。
- **示例**:
当我们在小程序开发时存在以下代码时
```html
<!--自定义组件list-->
<!--src/packageA/pages/index.mpx-->
<list bindtap="tapHandler"></list>
```
支付宝环境的自定义组件节点绑定事件并不会触发,但微信环境可以,若想在支付宝环境下使用该功能,则需配置此规则
```js
const path = require('path')

new MpxWebpackPlugin({
proxyComponentEventsRules: [
{
include: path.resolve('src/packageA'), // 输出支付宝时,对src/packageA文件夹中的所有自定义组件添加 onTap, onToucheMove 事件代理
exclude: path.resolve('lib'),
events: ['bindtap', 'bindtouchstart']
},
{
include: path.resolve('src/packageB'), // 输出支付宝时,对src/packageB文件夹中的所有自定义组件添加 onTap, onLongTap 事件代理
events: ['bindtap']
},
]
})
```
需要注意的是,所有的事件名配置,我们以微信事件为基准,例如虽然你是跨端输出支付宝,也请配置bindtap,而不是onTap。
请注意,在跨端输出 Web 时,只支持配置 tap 、longtap、longpress 这三个事件代理,且最好不要同时配置了两个事件,不然有可能会导致同一个事件触发两次。
### MpxWebpackPlugin.pluginLoader
:::warning
Expand Down
4 changes: 2 additions & 2 deletions docs-vuepress/guide/advance/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ createComponent({
// 全局locale变更,生效范围为项目全局,locale 是一个 ref 变量
locale.value = 'zh-CN'
// 或者通过mpx.i18n来修改,也能起到全局locale修改作用
mpx.i18n.global.locale.value = 'zh-CN'
mpx.i18n.locale = 'zh-CN'
}, 1000)
return {
t
Expand Down Expand Up @@ -313,7 +313,7 @@ import mpx, { createComponent } from '@mpxjs/core'
createComponent({
ready () {
// 修改全局 locale
mpx.i18n.global.locale = 'en-US'
mpx.i18n.locale = 'en-US'
}
})
```
Expand Down
2 changes: 1 addition & 1 deletion examples/mpx-webview/H5/webviewbridge.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.53"
"version": "2.8.58"
}
2 changes: 1 addition & 1 deletion 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.49",
"version": "2.8.58",
"description": "convert miniprogram API at each end",
"module": "src/index.js",
"types": "@types/index.d.ts",
Expand Down
14 changes: 12 additions & 2 deletions packages/api-proxy/src/web/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ function request (options = { url: '' }) {
headers: header,
responseType,
timeout,
cancelToken: source.token
cancelToken: source.token,
transitional: {
// silent JSON parsing mode
// `true` - ignore JSON parsing errors and set response.data to null if parsing failed (old behaviour)
// `false` - throw SyntaxError if JSON parsing failed (Note: responseType must be set to 'json')
silentJSONParsing: true, // default value for the current Axios version
// try to parse the response string as JSON even if `responseType` is not 'json'
forcedJSONParsing: false,
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
clarifyTimeoutError: false
}
}

if (method === 'GET') {
Expand All @@ -49,7 +59,7 @@ function request (options = { url: '' }) {

const promise = axios(rOptions).then(res => {
let data = res.data
if (responseType === 'text' && dataType === 'json') {
if (dataType === 'json' && typeof data === 'string') {
try {
data = JSON.parse(data)
} catch (e) {
Expand Down
6 changes: 5 additions & 1 deletion packages/api-proxy/src/web/api/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ function navigateTo (options = {}) {
function navigateBack (options = {}) {
const router = global.__mpxRouter
if (router) {
const delta = options.delta || 1
let delta = options.delta || 1
const stackLength = router.stack.length
if (stackLength > 1 && delta >= stackLength) {
delta = stackLength - 1
}
router.__mpxAction = {
type: 'back',
delta
Expand Down
10 changes: 8 additions & 2 deletions packages/core/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type PropValueType<Def> = Def extends {
: Def extends FullPropType<infer T>
? T
: Def extends PropType<infer T>
? T
? T
: any;

type GetPropsType<T> = {
Expand Down Expand Up @@ -250,6 +250,11 @@ interface AnyConstructor {
prototype: any
}

interface WebviewConfig {
hostWhitelists?: Array<string>
apiImplementations?: object
}

interface MpxConfig {
useStrictDiff: boolean
ignoreWarning: boolean | string | RegExp | ((msg: string, location: string, e: Error) => boolean)
Expand All @@ -259,7 +264,8 @@ interface MpxConfig {
proxyEventHandler: (e: Event) => any | null
setDataHandler: (data: object, target: ComponentIns<{}, {}, {}, {}, []>) => any | null
forceFlushSync: boolean,
webRouteConfig: object
webRouteConfig: object,
webviewConfig?: WebviewConfig
}

type SupportedMode = 'wx' | 'ali' | 'qq' | 'swan' | 'tt' | 'web' | 'qa'
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.53",
"version": "2.8.56",
"description": "mpx runtime core",
"keywords": [
"miniprogram",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/core/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,9 @@ export default class MpxProxy {

export let currentInstance = null

export const getCurrentInstance = () => currentInstance?.target
export const getCurrentInstance = () => {
return currentInstance && { proxy: currentInstance?.target }
}

export const setCurrentInstance = (instance) => {
currentInstance = instance
Expand Down
32 changes: 10 additions & 22 deletions packages/core/src/platform/builtInMixins/proxyEventMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,22 @@ export default function proxyEventMixin () {
}
if (__mpx_mode__ === 'ali') {
Object.assign(methods, {
triggerEvent (eventName, eventDetail, e) {
triggerEvent (eventName, eventDetail) {
const handlerName = eventName.replace(/^./, matched => matched.toUpperCase()).replace(/-([a-z])/g, (match, p1) => p1.toUpperCase())
const handler = this.props && (this.props['on' + handlerName] || this.props['catch' + handlerName])
if (handler && typeof handler === 'function') {
let eventObj = {}
if (e) {
e.detail = Object.assign(e.detail, eventDetail)
eventObj = e
} else {
const dataset = collectDataset(this.props)
const id = this.props.id || ''
const timeStamp = +new Date()
eventObj = {
type: eventName,
timeStamp,
target: { id, dataset, targetDataset: dataset },
currentTarget: { id, dataset },
detail: eventDetail
}
const dataset = collectDataset(this.props)
const id = this.props.id || ''
const timeStamp = +new Date()
const eventObj = {
type: eventName,
timeStamp,
target: { id, dataset, targetDataset: dataset },
currentTarget: { id, dataset },
detail: eventDetail
}
handler.call(this, eventObj)
}
},
__proxyEvent (e) {
const eventName = e.type
// 保持和微信一致
e.target = e.currentTarget
this.triggerEvent(eventName, {}, e)
}
})
}
Expand Down
Loading

0 comments on commit 2c820d5

Please sign in to comment.