From 503ba20973fb1019a00d93c6d1ee5e4a9caaa5a8 Mon Sep 17 00:00:00 2001 From: littly <544028951@qq.com> Date: Thu, 6 Dec 2018 22:10:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli=20router):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E7=BB=84=E4=BB=B6=E6=B2=A1=E6=9C=89component?= =?UTF-8?q?DidMount/componentDidShow=E6=97=B6=E4=B8=8D=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=A0=87=E9=A2=98=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/h5.js | 42 +++++++++++++++++-- .../src/router/createWrappedComponent.tsx | 2 +- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/packages/taro-cli/src/h5.js b/packages/taro-cli/src/h5.js index 6a3200f1e8d1..fd33ffac1ce4 100644 --- a/packages/taro-cli/src/h5.js +++ b/packages/taro-cli/src/h5.js @@ -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 @@ -537,7 +536,7 @@ function processEntry (code, filePath) { } } -function processOthers (code, filePath) { +function processOthers (code, filePath, fileType) { let ast = wxTransformer({ code, sourcePath: filePath, @@ -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: [ @@ -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 @@ -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( @@ -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)) diff --git a/packages/taro-router/src/router/createWrappedComponent.tsx b/packages/taro-router/src/router/createWrappedComponent.tsx index 3553f09ea4cb..2d5074c7f55b 100644 --- a/packages/taro-router/src/router/createWrappedComponent.tsx +++ b/packages/taro-router/src/router/createWrappedComponent.tsx @@ -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 = () => {