Guest User

Untitled

a guest
May 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. export default function transformer (file, api) {
  2. const j = api.jscodeshift
  3.  
  4. return j(file.source)
  5. .find(j.ImportDeclaration)
  6. .forEach(path => {
  7. const pathStr = path.value.source.value
  8.  
  9. if (!pathStr.startsWith('material-ui/') && pathStr !== 'material-ui') return
  10. path.value.specifiers.forEach(spec => {
  11. let newPath = pathStr.replace(/^material-ui(.*(?=\/))?/, '@material-ui/core')
  12. if (spec.imported) {
  13. // handles non-default imports
  14. // import { CardContent } from 'material-ui/Card' -> import CardContent from '@material-ui/core/CardContent'
  15. // import { CardContent as asdf } from 'material-ui/Card' -> import asdf from '@material-ui/core/CardContent'
  16. // import { withStyles } from 'material-ui/styles' -> import withStyles from '@material-ui/core/styles/withStyles'
  17. newPath = '@material-ui/core/' + spec.imported.name
  18. if (spec.imported.name === 'withStyles') {
  19. newPath = '@material-ui/core/styles/withStyles'
  20. }
  21. path.insertBefore(
  22. j.importDeclaration(
  23. [j.importDefaultSpecifier(spec.local)],
  24. j.literal(newPath)
  25. )
  26. )
  27. } else {
  28. // handles default imports
  29. // import Card from 'material-ui/Card' -> import Card from '@material-ui/core/Card'
  30. if (pathStr.includes('/colors/')) newPath = newPath.replace('/core/', '/core/colors/')
  31. if (pathStr.includes('/styles/')) newPath = newPath.replace('/core/', '/core/styles/')
  32. path.insertBefore(
  33. j.importDeclaration([
  34. spec
  35. ], j.literal(newPath))
  36. )
  37. }
  38. })
  39.  
  40. path.replace()
  41. }).toSource()
  42. }
Add Comment
Please, Sign In to add comment