Skip to content

Instantly share code, notes, and snippets.

@nickbrowne
Created October 4, 2018 02:18
Show Gist options
  • Save nickbrowne/1647dbb9943960bc1ca8ec7cf5e05788 to your computer and use it in GitHub Desktop.
Save nickbrowne/1647dbb9943960bc1ca8ec7cf5e05788 to your computer and use it in GitHub Desktop.
Hoist es6 imports to top of file
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
let imports = [];
let paths = root.find(j.ImportDeclaration, {
type: "ImportDeclaration"
}).forEach((x) => imports.push(x));
imports.reverse().forEach(path => {
let thing = j(path);
root.get().node.program.body.unshift(thing.toSource());
thing.remove();
});
return root.toSource();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment