Skip to content

Instantly share code, notes, and snippets.

@toddmantell
Last active June 11, 2018 22:44
Show Gist options
  • Select an option

  • Save toddmantell/24f7ee5252eeb21599545479ff624caf to your computer and use it in GitHub Desktop.

Select an option

Save toddmantell/24f7ee5252eeb21599545479ff624caf to your computer and use it in GitHub Desktop.
transform destructured import to CommonJS
//Should transform: import {Component} from 'react';
//to: const _Component = require('react').Component;
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.memberExpression(
t.callExpression(
t.identifier('require'), [t.identifier(`'${node.source.value}'`)]),
t.identifier(`${node.specifiers[0].imported.name}`))
)
])
)
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment