Skip to content

Commit

Permalink
feat(platform): add xhs platform
Browse files Browse the repository at this point in the history
  • Loading branch information
琼恩 committed Apr 13, 2022
1 parent 789e873 commit d0415f0
Show file tree
Hide file tree
Showing 21 changed files with 4,047 additions and 32 deletions.
1 change: 1 addition & 0 deletions packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class CLI {
case 'tt':
case 'qq':
case 'jd':
case 'xhs':
kernel.optsPlugins = [
...kernel.optsPlugins,
`@tarojs/plugin-platform-${platform}`,
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-xhs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
types
!types/shims-tt.d.ts
3 changes: 3 additions & 0 deletions packages/taro-xhs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `@tarojs/plugin-platform-xhs`

Taro 插件。用于支持编译为小红书小程序。
3 changes: 3 additions & 0 deletions packages/taro-xhs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = require('./dist/index.js').default
module.exports.default = module.exports
module.exports.XHS = require('./dist/index.js').XHS
37 changes: 37 additions & 0 deletions packages/taro-xhs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@tarojs/plugin-platform-xhs",
"version": "1.0.0",
"description": "小红书小程序平台插件",
"author": "@xhs",
"homepage": "https://github.com/nervjs/taro/tree/master/packages/taro-xhs#readme",
"license": "MIT",
"main": "index.js",
"keywords": [
"taro",
"xhs"
],
"files": [
"index.js",
"dist",
"types"
],
"types": "types/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/NervJS/taro.git"
},
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w"
},
"bugs": {
"url": "https://github.com/NervJS/taro/issues"
},
"dependencies": {
"@rollup/plugin-json": "^4.1.0",
"@tarojs/service": "3.3.15",
"@tarojs/shared": "3.3.15",
"rollup-plugin-typescript2": "^0.31.1",
"typescript": "^4.5.2"
}
}
49 changes: 49 additions & 0 deletions packages/taro-xhs/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json from '@rollup/plugin-json'
const { join } = require('path')
const typescript = require('rollup-plugin-typescript2')

const cwd = __dirname

const base = {
external: ['@tarojs/shared', '@tarojs/service'],
plugins: [typescript({
useTsconfigDeclarationDir: true
}),
json()]
}

// 供 CLI 编译时使用的 Taro 插件入口
const comileConfig = {
input: join(cwd, 'src/index.ts'),
output: {
file: join(cwd, 'dist/index.js'),
format: 'cjs',
sourcemap: true,
exports: 'named'
},
...base
}

// 供 Loader 使用的运行时入口
const runtimeConfig = {
input: join(cwd, 'src/runtime.ts'),
output: {
file: join(cwd, 'dist/runtime.js'),
format: 'es',
sourcemap: true
},
...base
}

// 供继承的包使用,为了能 tree-shaking
const runtimeUtilsConfig = {
input: join(cwd, 'src/runtime-utils.ts'),
output: {
file: join(cwd, 'dist/runtime-utils.js'),
format: 'es',
sourcemap: true
},
...base
}

module.exports = [comileConfig, runtimeConfig, runtimeUtilsConfig]
11 changes: 11 additions & 0 deletions packages/taro-xhs/src/apis-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const needPromiseApis = new Set([
'checkFollowState',
'exitMiniProgram',
'followOfficialAccount',
'getMenuButtonLayout',
'hideInteractionBar',
'pay',
'navigateToVideoView',
'showInteractionBar',
'openEcGood'
])
10 changes: 10 additions & 0 deletions packages/taro-xhs/src/apis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { processApis } from '@tarojs/shared'
// import { needPromiseApis } from './apis-list'

declare const xhs: any

export function initNativeApi (taro) {
processApis(taro, xhs, {

})
}
40 changes: 40 additions & 0 deletions packages/taro-xhs/src/components.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { singleQuote } from '@tarojs/shared'
// singleQuote 给字符串增加一个单引号
export const components = {
// ======== 调整属性 ========
Icon: {
size: '23'
},
Button: {
bindgetphonenumber: '',
'data-channel': ''
},
Form: {
'report-submit-timeout': '0'
},
Slider: {
color: singleQuote('#e9e9e9'),
'selected-color': singleQuote('#1aad19')
},
WebView: {
'progressbar-color': singleQuote('#51a0d8')
},
Video: {
'play-btn-position': singleQuote('center'),
'pre-roll-unit-id': '',
'post-roll-unit-id': '',
bindAdStart: '',
bindAdEnded: '',
bindAdLoad: '',
bindAdClose: '',
bindAdError: ''
},
Ad: {
fixed: '',
type: singleQuote('banner'),
scale: '100'
},
Textarea: {
'disable-default-padding': 'false'
}
}
16 changes: 16 additions & 0 deletions packages/taro-xhs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XHS from './program'
import type { IPluginContext } from '@tarojs/service'

// 让其它平台插件可以继承此平台
export { XHS }

export default (ctx: IPluginContext) => {
ctx.registerPlatform({
name: 'xhs',
useConfigName: 'mini',
async fn ({ config }) {
const program = new XHS(ctx, config)
await program.start()
}
})
}
43 changes: 43 additions & 0 deletions packages/taro-xhs/src/program.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { TaroPlatformBase } from '@tarojs/service'
import { Template } from './template'
import { components } from './components'

const PACKAGE_NAME = '@tarojs/plugin-platform-xhs'

export default class XHS extends TaroPlatformBase {
platform = 'xhs'
globalObject = 'xhs'
projectConfigJson = 'project.config.json'
runtimePath = `${PACKAGE_NAME}/dist/runtime`
fileType = {
templ: '.xhsml',
style: '.css',
config: '.json',
script: '.js'
}

template = new Template()

/**
* 1. setupTransaction - init
* 2. setup
* 3. setupTransaction - close
* 4. buildTransaction - init
* 5. build
* 6. buildTransaction - close
*/
constructor (ctx, config) {
super(ctx, config)

this.setupTransaction.addWrapper({
close: this.modifyTemplate
})
}

/**
* 增加组件或修改组件属性
*/
modifyTemplate () {
this.template.mergeComponents(this.ctx, components)
}
}
8 changes: 8 additions & 0 deletions packages/taro-xhs/src/runtime-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { initNativeApi } from './apis'

export { initNativeApi }
export * from './components'
export * from './apis-list'
export const hostConfig = {
initNativeApi
}
5 changes: 5 additions & 0 deletions packages/taro-xhs/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { mergeReconciler, mergeInternalComponents } from '@tarojs/shared'
import { hostConfig, components } from './runtime-utils'

mergeReconciler(hostConfig)
mergeInternalComponents(components)
23 changes: 23 additions & 0 deletions packages/taro-xhs/src/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RecursiveTemplate } from '@tarojs/shared/dist/template'
export class Template extends RecursiveTemplate {
supportXS = false
Adapter = {
if: 'xhs:if',
else: 'xhs:else',
elseif: 'xhs:elif',
for: 'xhs:for',
forItem: 'xhs:for-item',
forIndex: 'xhs:for-index',
key: 'xhs:key',
type: 'xhs'
}

replacePropName (name: string, value: string) {
if (value === 'eh') {
const nameLowerCase = name.toLowerCase()
if (nameLowerCase === 'bindlongtap') return 'bindlongpress'
return nameLowerCase
}
return name
}
}
Loading

0 comments on commit d0415f0

Please sign in to comment.