Skip to content

Commit e4944ef

Browse files
authored
Merge pull request #2338 from didi/feat-convert-ks-v2
feat: 支持输出快手小程序
2 parents 3c8030d + 527e4ec commit e4944ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+462
-78
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
jd: 'readonly',
1717
qa: 'readonly',
1818
dd: 'readonly',
19+
ks: 'readonly',
1920
Component: 'readonly',
2021
Page: 'readonly',
2122
App: 'readonly',

packages/core/@types/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// declaration for mpx mode
2-
declare let __mpx_mode__: 'wx' | 'ali' | 'swan' | 'qq' | 'tt' | 'web' | 'dd' | 'qa' | 'jd' | 'android' | 'ios' | 'harmony'
2+
declare let __mpx_mode__: 'wx' | 'ali' | 'swan' | 'qq' | 'tt' | 'web' | 'dd' | 'qa' | 'jd' | 'android' | 'ios' | 'harmony' | 'ks'
33

44
// declaration for mpx env
55
declare let __mpx_env__: string

packages/core/@types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ interface MpxConfig {
412412
rnConfig: RnConfig,
413413
}
414414

415-
type SupportedMode = 'wx' | 'ali' | 'qq' | 'swan' | 'tt' | 'web' | 'qa'
415+
type SupportedMode = 'wx' | 'ali' | 'qq' | 'swan' | 'tt' | 'web' | 'qa'| 'ks' | 'jd' | 'dd'
416416

417417
interface ImplementOptions {
418418
modes?: Array<SupportedMode>

packages/core/src/convertor/convertor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import wxToTtRule from './wxToTt'
99
import wxToDdRule from './wxToDd'
1010
import wxToJdRule from './wxToJd'
1111
import wxToReactRule from './wxToReact'
12+
import wxToKsRule from './wxToKs'
1213

1314
/**
1415
* 转换规则包含四点
@@ -38,7 +39,8 @@ const rulesMap = {
3839
wxToJd: extend({}, defaultConvertRule, wxToJdRule),
3940
wxToIos: extend({}, defaultConvertRule, wxToReactRule),
4041
wxToAndroid: extend({}, defaultConvertRule, wxToReactRule),
41-
wxToHarmony: extend({}, defaultConvertRule, wxToReactRule)
42+
wxToHarmony: extend({}, defaultConvertRule, wxToReactRule),
43+
wxToKs: extend({}, defaultConvertRule, wxToKsRule)
4244
}
4345

4446
export function getConvertRule (convertMode) {

packages/core/src/convertor/getConvertMode.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const convertModes = {
88
'wx-dd': 'wxToDd',
99
'wx-ios': 'wxToIos',
1010
'wx-android': 'wxToAndroid',
11-
'wx-harmony': 'wxToHarmony'
11+
'wx-harmony': 'wxToHarmony',
12+
'wx-ks': 'wxToKs'
1213
}
1314

1415
export function getConvertMode (srcMode) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { error } from '@mpxjs/utils'
2+
3+
const BEHAVIORS_MAP = [
4+
'wx://form-field',
5+
'wx://form-field-group',
6+
'wx://form-field-button',
7+
'wx://component-export'
8+
]
9+
10+
export default {
11+
convert (options) {
12+
if (options.behaviors) {
13+
options.behaviors.forEach((behavior, idx) => {
14+
if (BEHAVIORS_MAP.includes(behavior)) {
15+
error(`Built-in behavior "${behavior}" is not supported in ks environment!`, global.currentResource || global.currentModuleId)
16+
options.behaviors.splice(idx, 1)
17+
}
18+
})
19+
}
20+
}
21+
}

packages/core/src/core/proxy.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import {
3131
wrapMethodsWithErrorHandling,
3232
warn,
3333
error,
34-
getEnvObj
34+
getEnvObj,
35+
def
3536
} from '@mpxjs/utils'
37+
import { renderHelperDefs } from '../platform/builtInMixins/renderHelperMixin'
3638
import {
3739
BEFORECREATE,
3840
CREATED,
@@ -190,6 +192,7 @@ export default class MpxProxy {
190192
this.callHook(CREATED)
191193

192194
if (!isWeb && !isReact) {
195+
this.initRenderHelpers()
193196
this.initRender()
194197
}
195198

@@ -728,6 +731,15 @@ export default class MpxProxy {
728731
this.toggleRecurse(true)
729732
}
730733

734+
initRenderHelpers () {
735+
if (this.options.__nativeRender__ || __mpx_mode__ !== 'ks') return
736+
Object.keys(renderHelperDefs).forEach((key) => {
737+
if (!hasOwn(this.target, key)) {
738+
def(this.target, key, renderHelperDefs[key])
739+
}
740+
})
741+
}
742+
731743
initRender () {
732744
if (this.options.__nativeRender__) return this.doRender()
733745

packages/core/src/platform/builtInMixins/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,18 @@ export default function getBuiltInMixins ({ type, rawOptions = {} }) {
5757
}
5858
// 此为纯增强类mixins,原生模式下不需要注入
5959
if (!rawOptions.__nativeRender__) {
60-
bulitInMixins = bulitInMixins.concat([
61-
renderHelperMixin(),
60+
const enhancedMixins = [
6261
showMixin(type),
6362
i18nMixin(),
6463
dynamicRenderHelperMixin(),
6564
dynamicSlotMixin(),
6665
dynamicRefsMixin()
67-
])
66+
]
67+
if (__mpx_mode__ !== 'ks') {
68+
// ks methods 不支持 _ 或者 $ 开头的方法名,所以 ks 不走 methods mixin
69+
enhancedMixins.unshift(renderHelperMixin())
70+
}
71+
bulitInMixins = bulitInMixins.concat(enhancedMixins)
6872
}
6973
}
7074
return bulitInMixins.filter(item => item)
Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
import { getByPath, hasOwn, isObject } from '@mpxjs/utils'
22

3-
export default function renderHelperMixin () {
4-
return {
5-
methods: {
6-
_i (val, handler) {
7-
let i, l, keys, key
8-
if (Array.isArray(val) || typeof val === 'string') {
9-
for (i = 0, l = val.length; i < l; i++) {
10-
handler.call(this, val[i], i)
11-
}
12-
} else if (typeof val === 'number') {
13-
for (i = 0; i < val; i++) {
14-
handler.call(this, i + 1, i)
15-
}
16-
} else if (isObject(val)) {
17-
keys = Object.keys(val)
18-
for (i = 0, l = keys.length; i < l; i++) {
19-
key = keys[i]
20-
handler.call(this, val[key], key, i)
21-
}
22-
}
23-
},
24-
// collect
25-
_c (key, value) {
26-
if (hasOwn(this.__mpxProxy.renderData, key)) {
27-
return this.__mpxProxy.renderData[key]
28-
}
29-
if (value === undefined) {
30-
value = getByPath(this, key)
31-
}
32-
this.__mpxProxy.renderData[key] = value
33-
return value
34-
},
35-
// simple collect
36-
_sc (key) {
37-
return (this.__mpxProxy.renderData[key] = this[key])
38-
},
39-
_r (skipPre, vnode) {
40-
this.__mpxProxy.renderWithData(skipPre, vnode)
3+
export const renderHelperDefs = {
4+
_i (val, handler) {
5+
let i, l, keys, key
6+
if (Array.isArray(val) || typeof val === 'string') {
7+
for (i = 0, l = val.length; i < l; i++) {
8+
handler.call(this, val[i], i)
9+
}
10+
} else if (typeof val === 'number') {
11+
for (i = 0; i < val; i++) {
12+
handler.call(this, i + 1, i)
4113
}
14+
} else if (isObject(val)) {
15+
keys = Object.keys(val)
16+
for (i = 0, l = keys.length; i < l; i++) {
17+
key = keys[i]
18+
handler.call(this, val[key], key, i)
19+
}
20+
}
21+
},
22+
// collect
23+
_c (key, value) {
24+
if (hasOwn(this.__mpxProxy.renderData, key)) {
25+
return this.__mpxProxy.renderData[key]
26+
}
27+
if (value === undefined) {
28+
value = getByPath(this, key)
4229
}
30+
this.__mpxProxy.renderData[key] = value
31+
return value
32+
},
33+
// simple collect
34+
_sc (key) {
35+
return (this.__mpxProxy.renderData[key] = this[key])
36+
},
37+
_r (skipPre, vnode) {
38+
this.__mpxProxy.renderWithData(skipPre, vnode)
39+
}
40+
}
41+
42+
export default function renderHelperMixin () {
43+
return {
44+
methods: renderHelperDefs
4345
}
4446
}

packages/utils/src/env.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function getEnvObj () {
1616
return qa
1717
case 'dd':
1818
return dd
19+
case 'ks':
20+
return ks
1921
default:
2022
return {}
2123
}

0 commit comments

Comments
 (0)