Skip to content

Commit

Permalink
feat(cli): h5编译时自动修改@tarojs/taro-h5
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Jul 29, 2019
1 parent 98fd51a commit babbf54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 18 additions & 3 deletions packages/taro-cli/src/h5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,14 @@ class Compiler {
const callee = node.callee
const calleeName = toVar(callee)
const parentPath = astPath.parentPath
const arg0 = node.arguments[0]

if (t.isMemberExpression(callee)) {
if (calleeName === 'require' && t.isStringLiteral(arg0)) {
const required = arg0.value
if (required === '@tarojs/taro-h5') {
arg0.value = `@tarojs/taro-h5/dist/index.cjs.js`
}
} else if (t.isMemberExpression(callee)) {
const object = callee.object as t.Identifier
const property = callee.property as t.Identifier
if (object.name === taroImportDefaultName && property.name === 'render') {
Expand Down Expand Up @@ -816,7 +822,7 @@ class Compiler {
const attrName = toVar(attribute.name)
if (attrName === idAttrName) return toVar(attribute.value)
else return false
}, false)
}, false as string | false)
}
const getComponentRef = (node: t.JSXOpeningElement) => {
return node.attributes.find(attribute => {
Expand Down Expand Up @@ -918,9 +924,18 @@ class Compiler {
exit (astPath: NodePath<t.CallExpression>) {
const node = astPath.node
const callee = node.callee
const calleeName = toVar(callee)
let needToAppendThis = false
let funcName = ''
if (t.isMemberExpression(callee)) {

const arg0 = node.arguments[0]

if (calleeName === 'require' && t.isStringLiteral(arg0)) {
const required = arg0.value
if (required === '@tarojs/taro-h5') {
arg0.value = `@tarojs/taro-h5/dist/index.cjs.js`
}
} else if (t.isMemberExpression(callee)) {
const objName = toVar(callee.object)
const tmpFuncName = toVar(callee.property)
if (objName === taroImportDefaultName && APIS_NEED_TO_APPEND_THIS.has(tmpFuncName)) {
Expand Down
14 changes: 8 additions & 6 deletions packages/taro-cli/src/util/astConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function convertSourceStringToAstExpression (str: string, opts: object =
return template(str, Object.assign({}, babylonConfig, opts))()
}

export function convertAstExpressionToVariable (node) {
export function convertAstExpressionToVariable<T=any> (node): T {
if (t.isObjectExpression(node)) {
const obj = {}
const properties = node.properties
Expand All @@ -76,18 +76,20 @@ export function convertAstExpressionToVariable (node) {
obj[key] = value
}
})
return obj
return obj as any as T
} else if (t.isArrayExpression(node)) {
return node.elements.map(convertAstExpressionToVariable)
return node.elements.map(convertAstExpressionToVariable) as any as T
} else if (t.isLiteral(node)) {
return node['value']
} else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
const name = node.name
return name === 'undefined'
? undefined
: name
? undefined as any as T
: name as any as T
} else if (t.isJSXExpressionContainer(node)) {
return convertAstExpressionToVariable(node.expression)
return convertAstExpressionToVariable<T>(node.expression)
} else {
return undefined as any as T
}
}

Expand Down

0 comments on commit babbf54

Please sign in to comment.