Skip to content

Commit

Permalink
fix(mini-runner): 编译时需要移除对组件文件的引用
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jan 7, 2020
1 parent 34100f5 commit acdf816
Showing 1 changed file with 84 additions and 7 deletions.
91 changes: 84 additions & 7 deletions packages/taro-mini-runner/src/loaders/fileParseLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function processAst (
cannotRemoves.push(taroJsComponents)
}
const taroSelfComponents = new Set<string>()
const customComponents = new Set<string>()

traverse(ast, {
ClassDeclaration (astPath) {
Expand Down Expand Up @@ -143,21 +144,54 @@ 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
}
}
},

ClassProperty (astPath) {
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') {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit acdf816

Please sign in to comment.