resolve-path.js 830 B

12345678910111213141516171819202122232425262728293031323334
  1. const path = require('path')
  2. exports.resolveModuleRoot = function (filePath, id = null) {
  3. {
  4. const index = filePath.lastIndexOf(path.sep + 'index.js')
  5. if (index !== -1) {
  6. filePath = filePath.substr(0, index)
  7. }
  8. }
  9. if (id) {
  10. id = id.replace(/\//g, path.sep)
  11. // With node_modules folder
  12. let search = `node_modules/${id}`
  13. let index = filePath.lastIndexOf(search)
  14. if (index === -1) {
  15. // Id only
  16. search = id
  17. index = filePath.lastIndexOf(search)
  18. }
  19. if (index === -1) {
  20. // Scoped (in dev env)
  21. index = id.lastIndexOf('/')
  22. if (index !== -1) {
  23. search = id.substr(index + 1)
  24. index = filePath.lastIndexOf(search)
  25. }
  26. }
  27. if (index !== -1) {
  28. filePath = filePath.substr(0, index + search.length)
  29. }
  30. }
  31. return filePath
  32. }