Skip to content

Commit

Permalink
fix(cli router): 修复页面组件没有componentDidMount/componentDidShow时不更新页面标题的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Dec 6, 2018
1 parent b7cead3 commit 503ba20
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
42 changes: 38 additions & 4 deletions packages/taro-cli/src/h5.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,7 @@ function processEntry (code, filePath) {
const keyName = getObjKey(key)
if (keyName === 'componentWillMount') {
hasComponentWillMount = true
}
if (keyName === 'componentDidMount') {
} else if (keyName === 'componentDidMount') {
hasComponentDidMount = true
} else if (keyName === 'componentDidShow') {
hasComponentDidShow = true
Expand Down Expand Up @@ -537,7 +536,7 @@ function processEntry (code, filePath) {
}
}

function processOthers (code, filePath) {
function processOthers (code, filePath, fileType) {
let ast = wxTransformer({
code,
sourcePath: filePath,
Expand All @@ -548,6 +547,9 @@ function processOthers (code, filePath) {
let taroImportDefaultName
let hasAddNervJsImportDefaultName = false
let hasJSX = false
let isPage = fileType === FILE_TYPE.PAGE
let hasComponentDidMount = false
let hasComponentDidShow = false

ast = babel.transformFromAst(ast, '', {
plugins: [
Expand Down Expand Up @@ -592,9 +594,38 @@ function processOthers (code, filePath) {
}
}

const programExitVisitor = {
ClassBody: {
exit (astPath) {
if (!hasComponentDidMount) {
astPath.pushContainer('body', t.classMethod(
'method', t.identifier('componentDidMount'), [],
t.blockStatement([]), false, false))
}
if (!hasComponentDidShow) {
astPath.pushContainer('body', t.classMethod(
'method', t.identifier('componentDidShow'), [],
t.blockStatement([]), false, false))
}
}
}
}

traverse(ast, {
ClassExpression: ClassDeclarationOrExpression,
ClassDeclaration: ClassDeclarationOrExpression,
ClassMethod: isPage ? {
exit (astPath) {
const node = astPath.node
const key = node.key
const keyName = getObjKey(key)
if (keyName === 'componentDidMount') {
hasComponentDidMount = true
} else if (keyName === 'componentDidShow') {
hasComponentDidShow = true
}
}
} : {},
ImportDeclaration: {
enter (astPath) {
const node = astPath.node
Expand Down Expand Up @@ -653,6 +684,9 @@ function processOthers (code, filePath) {
},
Program: {
exit (astPath) {
if (isPage) {
astPath.traverse(programExitVisitor)
}
const node = astPath.node
if (hasJSX && !hasAddNervJsImportDefaultName) {
node.body.unshift(
Expand Down Expand Up @@ -753,7 +787,7 @@ function processFiles (filePath) {
const content = file.toString()
const transformResult = fileType === FILE_TYPE.ENTRY
? processEntry(content, filePath)
: processOthers(content, filePath)
: processOthers(content, filePath, fileType)
const jsCode = unescape(transformResult.code.replace(/\\u/g, '%u'))
fs.ensureDirSync(distDirname)
fs.writeFileSync(distPath, Buffer.from(jsCode))
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-router/src/router/createWrappedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const createWrappedComponent = (component: Types.PageComponent) => {
ctx.componentDidShow = newComponentDidShow
tryToCall(superComponentDidMount, this)
if (this.wrappedInstance) {
const originalComponentDidMount = ctx.componentDidMount
const originalComponentDidMount = ctx.componentDidMount
if (!this.wrappedInstance['__cdm_modified']) {
this.wrappedInstance['__cdm_modified'] = true
this.wrappedInstance.componentDidMount = () => {
Expand Down

0 comments on commit 503ba20

Please sign in to comment.