Skip to content

Instantly share code, notes, and snippets.

@Jokcy
Last active August 12, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jokcy/22c42e11fbc56bbccbc2f10d43f10a3a to your computer and use it in GitHub Desktop.
Save Jokcy/22c42e11fbc56bbccbc2f10d43f10a3a to your computer and use it in GitHub Desktop.
把从@tarojs/taro引入的hooks API转变成从nervjs引入
module.exports = function(babel) {
const t = babel.types
const hookTest = /^use[A-Z]/
let hooksImport = new Set()
return {
name: 'babel-plugin-transform-taro-hooks',
visitor: {
ImportDeclaration(path) {
const n = path.node
if (t.isStringLiteral(n.source, { value: '@tarojs/taro' })) {
path.traverse({
ImportSpecifier(iPath) {
const node = iPath.node
// console.log(node.local.name)
if (hookTest.test(node.local.name)) {
hooksImport.add(node.local.name)
iPath.remove()
}
},
})
}
},
Program: {
enter() {
hooksImport = new Set()
console.log('start transform +++++++++++++++')
},
exit(ast) {
const iptd = t.importDeclaration([], t.StringLiteral('nervjs'))
for (let spec of hooksImport) {
iptd.specifiers.push(
t.importSpecifier(t.identifier(spec), t.identifier(spec))
)
}
// console.log('----------', iptd)
ast.node.body.unshift(iptd)
},
},
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment