Last active
August 12, 2019 06:45
-
-
Save Jokcy/22c42e11fbc56bbccbc2f10d43f10a3a to your computer and use it in GitHub Desktop.
把从@tarojs/taro引入的hooks API转变成从nervjs引入
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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