all files / src/ helper.js

97.14% Statements 34/35
90% Branches 18/20
100% Functions 4/4
100% Lines 22/22
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                                         
import { join, basename, dirname, relative, isAbsolute } from 'path'
 
export function normalizeSourceRoot(sourceRoot, suffix = '') {
  if (typeof sourceRoot != 'string') sourceRoot = undefined
  Iif (typeof suffix != 'string') suffix = ''
 
  if (!sourceRoot) sourceRoot = process.cwd()
 
  return join(sourceRoot, suffix)
}
 
export function normalizeFilename(filename, sourceRoot) {
  if (typeof filename != 'string') return null
  if (typeof sourceRoot != 'string') return null
  if (filename == 'unknown') return null
 
  // babel-loader supplies an absolute path as filename
  // so we need to check for that.
  if (isAbsolute(filename)) {
    return relative(sourceRoot, filename)
  }
 
  return filename
}
 
export function checkAndRemovePrefix(path, prefix) {
  if (path.startsWith(prefix)) {
    return path.substring(prefix.length)
  } else {
    return null
  }
}
 
export function transformPath(importPath, filePath, sourceRoot) {
  const importPathName = basename(importPath)
 
  const absoluteImportPath = dirname(join(sourceRoot, importPath))
  const absoluteFilePath = dirname(join(sourceRoot, filePath))
  const relativeImportPath = relative(absoluteFilePath, absoluteImportPath)
 
  return './' + join(relativeImportPath, importPathName)
}