@@ -8,7 +8,6 @@ var exists = require('./lib/helpers/exists')();
88var read = require ( './lib/helpers/read' ) ( ) ;
99var resolveRc = require ( './lib/resolve-rc.js' ) ;
1010var pkg = require ( './package.json' ) ;
11- var path = require ( 'path' ) ;
1211
1312var transpile = function ( source , options ) {
1413 var result = babel . transform ( source , options ) ;
@@ -25,6 +24,46 @@ var transpile = function(source, options) {
2524 } ;
2625} ;
2726
27+ /**
28+ * Make a path relative to a URL or another path.
29+ * Borrowed from https://github.com/mozilla/source-map/blob/master/lib/util.js
30+ *
31+ * @param aRoot The root path or URL.
32+ * @param aPath The path or URL to be made relative to aRoot.
33+ */
34+ function relative ( aRoot , aPath ) {
35+ if ( aRoot === '' ) {
36+ aRoot = '.' ;
37+ }
38+
39+ aRoot = aRoot . replace ( / \/ $ / , '' ) ;
40+
41+ // It is possible for the path to be above the root. In this case, simply
42+ // checking whether the root is a prefix of the path won't work. Instead, we
43+ // need to remove components from the root one by one, until either we find
44+ // a prefix that fits, or we run out of components to remove.
45+ var level = 0 ;
46+ while ( aPath . indexOf ( aRoot + '/' ) !== 0 ) {
47+ var index = aRoot . lastIndexOf ( '/' ) ;
48+ if ( index < 0 ) {
49+ return aPath ;
50+ }
51+
52+ // If the only part of the root that is left is the scheme (i.e. http://,
53+ // file:///, etc.), one or more slashes (/), or simply nothing at all, we
54+ // have exhausted all components, so the path is not relative to the root.
55+ aRoot = aRoot . slice ( 0 , index ) ;
56+ if ( aRoot . match ( / ^ ( [ ^ \/ ] + : \/ ) ? \/ * $ / ) ) {
57+ return aPath ;
58+ }
59+
60+ ++ level ;
61+ }
62+
63+ // Make sure we add a '../' for each component we removed from the root.
64+ return Array ( level + 1 ) . join ( '../' ) + aPath . substr ( aRoot . length + 1 ) ;
65+ }
66+
2867module . exports = function ( source , inputSourceMap ) {
2968 var result = { } ;
3069
@@ -57,7 +96,7 @@ module.exports = function(source, inputSourceMap) {
5796 }
5897
5998 if ( options . sourceFileName === undefined ) {
60- options . sourceFileName = path . relative (
99+ options . sourceFileName = relative (
61100 options . sourceRoot ,
62101 options . filename
63102 ) ;
0 commit comments