提交 40c59751 编写于 作者: fxy060608's avatar fxy060608

fix(mp): support app.component #2872

上级 1807150c
const t = require('@babel/types')
const babelTraverse = require('@babel/traverse').default
const {
parseComponents
} = require('./util')
const { parseComponents } = require('./util')
const uniI18n = require('@dcloudio/uni-cli-i18n')
module.exports = function (ast, state = {}) {
const imports = []
let bindings = false
let parentPath = false
let nodePath = false
babelTraverse(ast, {
CallExpression (path) {
const callee = path.node.callee
if (!callee.object || !callee.property) {
return
}
if (callee.object.name === 'Vue' && callee.property.name === 'component') {
parentPath = path.parentPath
bindings = path.scope.bindings
const objectName = callee.object.name
const propertyName = callee.property.name
if (
propertyName === 'component' &&
(objectName === 'Vue' || objectName === 'app')
) {
const args = path.node.arguments
const nameNode = args[0]
const valueNode = args[1]
nodePath = path
if (!t.isStringLiteral(nameNode)) {
throw new Error(uniI18n.__('mpLoader.firstParameterNeedStaticString', { 0: 'Vue.component()' }))
throw new Error(
uniI18n.__('mpLoader.firstParameterNeedStaticString', {
0: objectName + '.component()'
})
)
}
if (!t.isIdentifier(valueNode)) {
throw new Error(uniI18n.__('mpLoader.requireTwoParameter', { 0: 'Vue.component()' }))
throw new Error(
uniI18n.__('mpLoader.requireTwoParameter', {
0: objectName + '.component()'
})
)
}
imports.push({
name: nameNode.value,
......@@ -37,7 +46,7 @@ module.exports = function (ast, state = {}) {
}
})
if (imports.length) {
state.components = parseComponents(imports, bindings, parentPath)
state.components = parseComponents(imports, nodePath)
} else {
state.components = []
}
......
......@@ -61,7 +61,7 @@ function handleComponentsObjectExpression (componentsObjExpr, path, state, prepe
name: prop.key.name || prop.key.value,
value: prop.value.name
}
}), path.scope.bindings, path)
}), path)
state.components = prepend ? components.concat(state.components) : components
}
......
......@@ -15,8 +15,8 @@ const hyphenate = cached((str) => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
})
function findImportDeclaration (identifierName, bindings) {
const binding = bindings[identifierName]
function findImportDeclaration (identifierName, path) {
const binding = path.scope.getBinding(identifierName)
if (!binding) {
return
}
......@@ -26,16 +26,18 @@ function findImportDeclaration (identifierName, bindings) {
}
}
function parseComponents (names, bindings, path) {
function parseComponents (names, path) {
const components = []
const dynamicImportMap = new Map()
names.forEach(({
name,
value
}) => {
const importDeclaration = findImportDeclaration(value, bindings)
const importDeclaration = findImportDeclaration(value, path)
if (!importDeclaration) {
throw new Error(uniI18n.__('mpLoader.componentReferenceErrorOnlySupportImport', { 0: name }))
throw new Error(uniI18n.__('mpLoader.componentReferenceErrorOnlySupportImport', {
0: name
}))
}
let source = importDeclaration.node.source.value
if (process.UNI_LIBRARIES && process.UNI_LIBRARIES.includes(source)) {
......@@ -83,4 +85,4 @@ module.exports = {
addDynamicImport,
findBabelLoader,
parseComponents
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册