Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created March 6, 2020 21:46
Show Gist options
  • Save vicapow/00240218b1816fb9d78f11cc9f9d8504 to your computer and use it in GitHub Desktop.
Save vicapow/00240218b1816fb9d78f11cc9f9d8504 to your computer and use it in GitHub Desktop.
convert lodash-es back to lodash
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