Last active
June 11, 2018 22:44
-
-
Save toddmantell/24f7ee5252eeb21599545479ff624caf to your computer and use it in GitHub Desktop.
transform destructured import to CommonJS
This file contains hidden or 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
| //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