@@ -74,6 +74,39 @@ export const mapImportsToPathAliases = (
74
74
importPath = normalizePath (
75
75
relative ( dirname ( destinationFilePath ) , resolvePathInDestination ) . replace ( extensionRegex , '' )
76
76
) ;
77
+ // if the importee is a sibling file of the importer then `relative` will
78
+ // produce a somewhat confusing result. We use `dirname` to get the
79
+ // directory of the importer, so for example, assume we have two files
80
+ // `foo/bar.ts` and `foo/baz.ts` like so:
81
+ //
82
+ // ```
83
+ // foo
84
+ // ├── bar.ts
85
+ // └── baz.ts
86
+ // ```
87
+ //
88
+ // then if `baz.ts` imports a symbol from `bar.ts` we'll call
89
+ // `relative(fromdir, to)` like so:
90
+ //
91
+ // ```ts
92
+ // relative(dirname("foo/baz.ts"), "foo/bar.ts")
93
+ // // equivalently
94
+ // relative("foo", "foo/bar.ts")
95
+ // ```
96
+ //
97
+ // you'd think that in this case `relative` would return `'./bar.ts'` as
98
+ // a correct relative path to `bar.ts` from the `foo` directory, but
99
+ // actually in this case it returns just `bar.ts`. So since when updating
100
+ // import paths we're only concerned with `paths` aliases that should be
101
+ // transformed to relative imports anyway, we check to see if the new
102
+ // `importPath` starts with `'.'`, and add `'./'` if it doesn't, since
103
+ // otherwise Node will interpret `bar` as a module name, not a relative
104
+ // path.
105
+ //
106
+ // Note also that any relative paths as module specifiers which _don't_
107
+ // need to be transformed (e.g. `'./foo'`) have already been handled
108
+ // above.
109
+ importPath = importPath . startsWith ( '.' ) ? importPath : './' + importPath ;
77
110
}
78
111
}
79
112
0 commit comments