Last active
October 30, 2017 14:41
-
-
Save toddmantell/1c2f8fd3824a6e8dd92ace348d4d7606 to your computer and use it in GitHub Desktop.
Import declaration transform 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
| //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