Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. var fs = require('fs')
  2. var pathUtils = require('path')
  3.  
  4.  
  5. var fileExist = function( path ){
  6. try{
  7. fs.accessSync( path )
  8. return true
  9. } catch( err ){
  10. return false
  11. }
  12. }
  13.  
  14. var moduleExist = function( path ){
  15. return path.slice(-3) != '.js'
  16. ? fileExist( path+'.js' ) || fileExist( path+'/index.js' )
  17. : fileExist( path )
  18. }
  19.  
  20. module.exports = function () {
  21.  
  22. var cwd = process.cwd()
  23.  
  24. return {
  25. visitor:{
  26.  
  27. ImportDeclaration: function(path, parent) {
  28.  
  29. var src = path.node.source.value.trim()
  30.  
  31. if ( src.slice(0,2) == './' )
  32. return
  33.  
  34. // the directory of the current file ( from where to write the relative path )
  35. var fromDirectory = pathUtils.join( cwd, pathUtils.dirname( parent.file.opts.filename ) )
  36.  
  37. // directories that may contain the file
  38. // ( read from the options, default is 'web_modules' )
  39. var rootDirectories = parent.opts && parent.opts.rootDirectories || [ 'web_modules' ]
  40.  
  41. // test the existence of the file in each rootDirectory
  42. // if it exist in two directories, the first in the list have the priority
  43. for( var i=rootDirectories.length; i--; ) {
  44.  
  45. var absolute = pathUtils.join( cwd, rootDirectories[i], src )
  46.  
  47. if ( moduleExist( absolute ) )
  48. path.node.source.value = pathUtils.relative( fromDirectory, absolute )
  49.  
  50. }
  51. }
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement