Skip to content

Instantly share code, notes, and snippets.

@toddmantell
Last active October 30, 2017 14:41
Show Gist options
  • Select an option

  • Save toddmantell/1c2f8fd3824a6e8dd92ace348d4d7606 to your computer and use it in GitHub Desktop.

Select an option

Save toddmantell/1c2f8fd3824a6e8dd92ace348d4d7606 to your computer and use it in GitHub Desktop.
Import declaration transform to commonJS
//copy this into AST Explorer and run it against a standard import statement, like import React from 'react';
export default function ({types: t}) {
return {
visitor: {
ImportDeclaration(path) {
const {node} = path;
path.replaceWith(
t.variableDeclaration('var', [
t.variableDeclarator(t.identifier(`_${node.specifiers[0].local.name}`),
t.callExpression(t.identifier('require'), [t.identifier(`'${node.source.value}'`)]))
])
)
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment