all files / src/ plugin.js

100% Statements 14/14
62.5% Branches 5/8
100% Functions 2/2
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                   
import {
  normalizeSourceRoot, normalizeFilename,
  checkAndRemovePrefix, transformPath
} from '../src/helper'
 
export default function() {
  return {
    visitor: {
      ImportDeclaration(path, state) {
        const projectPathSuffix = typeof state.opts.projectPathSuffix == 'string' ?
          state.opts.projectPathSuffix :
          ''
 
        const importPathPrefix = typeof state.opts.importPathPrefix == 'string' ?
          state.opts.importPathPrefix + '/':
          '~/'
 
        const sourceRoot = normalizeSourceRoot(
          state.file.opts.sourceRoot, projectPathSuffix
        )
 
        // Tried to use filenameRelative but it doesn't seem to be set correctly
        // by any major wrapper for babel.
        const filename = normalizeFilename(
          state.file.opts.filename,
          sourceRoot
        )
 
        if (!filename) return
 
        const importPath = path.node.source.value
        const importPathWithoutPrefix = checkAndRemovePrefix(
          importPath, importPathPrefix
        )
 
        Eif (importPathWithoutPrefix) {
          path.node.source.value = transformPath(
            importPathWithoutPrefix, filename, sourceRoot
          )
        }
      }
    }
  }
}