Skip to content

Commit

Permalink
fix(mini-runner): 修复页面 hooks config 失效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jan 7, 2020
1 parent 376138b commit 545f327
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/taro-mini-runner/src/loaders/fileParseLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getOptions } from 'loader-utils'
import { transform, transformFromAst } from 'babel-core'
import * as t from 'babel-types'
import generate from 'better-babel-generator'
import traverse from 'babel-traverse'
import traverse, { NodePath } from 'babel-traverse'
import * as _ from 'lodash'

import {
Expand Down Expand Up @@ -49,6 +49,8 @@ function processAst (
let hasComponentDidHide
let hasComponentDidShow
let hasComponentWillMount
let needSetConfigFromHooks = false
let configFromHooks
if (isQuickApp) {
cannotRemoves.push(taroJsComponents)
}
Expand Down Expand Up @@ -354,6 +356,19 @@ function processAst (
}
},

AssignmentExpression (astPath) {
const node = astPath.node
const left = node.left
if (t.isMemberExpression(left) && t.isIdentifier(left.object)) {
if (left.object.name === componentClassName
&& t.isIdentifier(left.property)
&& left.property.name === 'config') {
needSetConfigFromHooks = true
configFromHooks = node.right
}
}
},

Program: {
exit (astPath) {
astPath.traverse({
Expand Down Expand Up @@ -398,6 +413,15 @@ function processAst (
`Taro.eventCenter.off('TaroEvent:setNavigationBar')`
)]), false, false))
}
if (needSetConfigFromHooks) {
const classPath = astPath.findParent((p: NodePath<t.Node>) => p.isClassExpression() || p.isClassDeclaration()) as NodePath<t.ClassDeclaration>
classPath.node.body.body.unshift(
t.classProperty(
t.identifier('config'),
configFromHooks as t.ObjectExpression
)
)
}
},
ClassMethod (astPath) {
if (isQuickApp) {
Expand Down
46 changes: 46 additions & 0 deletions packages/taro-mini-runner/src/utils/parseAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function parseAst (
let hasEnablePageScroll
const taroSelfComponents = new Set<string>()
const isQuickApp = buildAdapter === BUILD_TYPES.QUICKAPP
let componentClassName: string = ''

const newAst = transformFromAst(ast, '', {
plugins: [
Expand Down Expand Up @@ -62,6 +63,40 @@ export default function parseAst (
}
}
})
if (node.id === null) {
componentClassName = '_TaroComponentClass'
} else if (node.id.name === 'App') {
componentClassName = '_App'
} else {
componentClassName = node.id.name
}
}
}
},
ClassExpression (astPath) {
const node = astPath.node
if (node.superClass) {
let hasCreateData = false
astPath.traverse({
ClassMethod (astPath) {
if (astPath.get('key').isIdentifier({ name: '_createData' })) {
hasCreateData = true
}
}
})
if (hasCreateData) {
if (node.id === null) {
const parentNode = astPath.parentPath.node as any
if (t.isVariableDeclarator(astPath.parentPath)) {
componentClassName = parentNode.id.name
} else {
componentClassName = '_TaroComponentClass'
}
} else if (node.id.name === 'App') {
componentClassName = '_App'
} else {
componentClassName = node.id.name
}
}
}
},
Expand Down Expand Up @@ -115,6 +150,17 @@ export default function parseAst (
astPath.remove()
}
}
},
AssignmentExpression (astPath) {
const node = astPath.node
const left = node.left
if (t.isMemberExpression(left) && t.isIdentifier(left.object)) {
if (left.object.name === componentClassName
&& t.isIdentifier(left.property)
&& left.property.name === 'config') {
configObj = traverseObjectNode(node.right, buildAdapter)
}
}
}
})

Expand Down

0 comments on commit 545f327

Please sign in to comment.