Skip to content

Commit

Permalink
fix(cli, h5): 修复PureComponent中获取不到$router和$app的问题. fix #1857
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Mar 11, 2019
1 parent a95f600 commit b8b81f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/taro-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"babel-types": "^6.26.0",
"babylon": "^6.18.0",
"better-babel-generator": "^6.26.1",
"chalk": "^2.3.2",
"chalk": "2.4.2",
"chokidar": "^2.0.3",
"commander": "^2.19.0",
"cross-spawn": "^6.0.5",
Expand Down
15 changes: 11 additions & 4 deletions packages/taro-cli/src/h5.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const PACKAGES = {

const taroApis = [
'Component',
'PureComponent',
'getEnv',
'ENV_TYPE',
'eventCenter',
Expand Down Expand Up @@ -137,7 +138,9 @@ function processEntry (code, filePath) {
if (!node.superClass) return
if (
node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName
node.superClass.object.name === taroImportDefaultName &&
(node.superClass.property.name === 'Component' ||
node.superClass.property.name === 'PureComponent')
) {
node.superClass.object.name = taroImportDefaultName
if (node.id === null) {
Expand All @@ -151,7 +154,8 @@ function processEntry (code, filePath) {
)
)
}
} else if (node.superClass.name === 'Component') {
} else if (node.superClass.name === 'Component' ||
node.superClass.name === 'PureComponent') {
resetTSClassProperty(node.body.body)
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
Expand Down Expand Up @@ -578,7 +582,9 @@ function processOthers (code, filePath, fileType) {
if (!node.superClass) return
if (
node.superClass.type === 'MemberExpression' &&
node.superClass.object.name === taroImportDefaultName
node.superClass.object.name === taroImportDefaultName &&
(node.superClass.property.name === 'Component' ||
node.superClass.property.name === 'PureComponent')
) {
node.superClass.object.name = taroImportDefaultName
if (node.id === null) {
Expand All @@ -592,7 +598,8 @@ function processOthers (code, filePath, fileType) {
)
)
}
} else if (node.superClass.name === 'Component') {
} else if (node.superClass.name === 'Component' ||
node.superClass.name === 'PureComponent') {
resetTSClassProperty(node.body.body)
if (node.id === null) {
const renameComponentClassName = '_TaroComponentClass'
Expand Down
18 changes: 18 additions & 0 deletions packages/taro-h5/src/taro/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ class Component extends Nerv.Component {
}
}

class PureComponent extends Nerv.PureComponent {
get $router () {
return taro.getRouter()
}
set $router (args) {
console.warn('Property "$router" is read-only.')
}

get $app () {
return taro.getApp()
}

set $app (app) {
console.warn('Property "$app" is read-only.')
}
}

const initPxTransform = originalInitPxTransform.bind(taro)
const requirePlugin = permanentlyNotSupport('requirePlugin')
const _set$app = function (app) {
Expand All @@ -65,6 +82,7 @@ const canIUseWebp = function () {
}

taro.Component = Component
taro.PureComponent = PureComponent
taro.initPxTransform = initPxTransform
taro.requirePlugin = requirePlugin
taro._set$app = _set$app
Expand Down

0 comments on commit b8b81f6

Please sign in to comment.