Created
March 6, 2020 21:46
-
-
Save vicapow/00240218b1816fb9d78f11cc9f9d8504 to your computer and use it in GitHub Desktop.
convert lodash-es back to lodash
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
export default function transformer(file, api) { | |
const j = api.jscodeshift; | |
const root = j(file.source); | |
let body; | |
root.find(j.Program).map(program => { | |
body = program.value.body; | |
}); | |
const lodashImports = root | |
.find(j.ImportDeclaration) | |
.filter(nodePath => { | |
return nodePath.value.source.value.startsWith("lodash-es") | |
}); | |
lodashImports.forEach(nodePath => { | |
const names = nodePath.value.specifiers.forEach(specifier => { | |
const [name, local] = [specifier.imported.name, specifier.local.name]; | |
body.unshift(j.importDeclaration([j.importDefaultSpecifier(j.identifier(local))], j.stringLiteral('lodash/' + name))); | |
}); | |
}); | |
lodashImports.forEach(nodePath => nodePath.prune()); | |
return root.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment