From e7c3d43fe5f84791f92ebc485042eb29528885be Mon Sep 17 00:00:00 2001 From: luckyadam Date: Mon, 23 Dec 2019 16:24:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(mini-runner):=20=E7=BC=96=E8=AF=91=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=A7=BB=E9=99=A4=E5=AF=B9=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/loaders/fileParseLoader.ts | 91 +++++++++++++++++-- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts index 9cf080515df6..ae600aad6087 100644 --- a/packages/taro-mini-runner/src/loaders/fileParseLoader.ts +++ b/packages/taro-mini-runner/src/loaders/fileParseLoader.ts @@ -55,6 +55,7 @@ function processAst ( cannotRemoves.push(taroJsComponents) } const taroSelfComponents = new Set() + const customComponents = new Set() traverse(ast, { ClassDeclaration (astPath) { @@ -143,13 +144,40 @@ function processAst ( }, ClassMethod (astPath) { + const node = astPath.node const keyName = (astPath.get('key').node as t.Identifier).name - if (keyName === 'componentWillMount') { - hasComponentWillMount = true - } else if (keyName === 'componentDidShow') { - hasComponentDidShow = true - } else if (keyName === 'componentDidHide') { - hasComponentDidHide = true + if (node.kind === 'constructor') { + astPath.traverse({ + ExpressionStatement (astPath) { + const node = astPath.node + if (node.expression && + node.expression.type === 'AssignmentExpression' && + node.expression.operator === '=') { + const left = node.expression.left + if (left.type === 'MemberExpression' && + left.object.type === 'ThisExpression' && + left.property.type === 'Identifier' && + left.property.name === 'customComponents') { + const right = node.expression.right + if (t.isArrayExpression(right)) { + right.elements.forEach(item => { + if (t.isStringLiteral(item)) { + customComponents.add(item.value) + } + }) + } + } + } + } + }) + } else { + if (keyName === 'componentWillMount') { + hasComponentWillMount = true + } else if (keyName === 'componentDidShow') { + hasComponentDidShow = true + } else if (keyName === 'componentDidHide') { + hasComponentDidHide = true + } } }, @@ -157,7 +185,13 @@ function processAst ( const node = astPath.node const keyName = node.key.name const valuePath = astPath.get('value') - if (valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression()) { + if (keyName === 'customComponents' && valuePath.isArrayExpression()) { + valuePath.node.elements.forEach(item => { + if (t.isStringLiteral(item)) { + customComponents.add(item.value) + } + }) + } else if (valuePath.isFunctionExpression() || valuePath.isArrowFunctionExpression()) { if (keyName === 'componentWillMount') { hasComponentWillMount = true } else if (keyName === 'componentDidShow') { @@ -433,6 +467,49 @@ function processAst ( node.body.body.unshift(convertSourceStringToAstExpression(`this.__offListenToSetNavigationBarEvent()`)) } } + }, + ImportDeclaration (astPath) { + const node = astPath.node + const source = node.source + let value = source.value + const specifiers = node.specifiers + let needRemove = false + if (!isNpmPkg(value)) { + specifiers.forEach(item => { + if (customComponents.has(item.local.name)) { + needRemove = true + } + }) + if (needRemove) { + astPath.remove() + } + } + }, + CallExpression (astPath) { + const node = astPath.node + const callee = node.callee as t.Identifier + if (callee.name === 'require') { + const parentNode = astPath.parentPath.node as t.VariableDeclarator + const args = node.arguments as t.StringLiteral[] + let value = args[0].value + let needRemove = false + if (!isNpmPkg(value)) { + const id = parentNode.id + if (t.isObjectPattern(id)) { + const properties = id.properties + properties.forEach(property => { + if (t.isObjectProperty(property) && customComponents.has((property.value as t.Identifier).name)) { + needRemove = true + } + }) + } else if (t.isIdentifier(id) && customComponents.has(id.name)) { + needRemove = true + } + } + if (needRemove) { + astPath.parentPath.parentPath.remove() + } + } } }) const node = astPath.node as t.Program