From a8f88cc43db7107d40e154efdea2981f749439c4 Mon Sep 17 00:00:00 2001 From: Tanner Reits Date: Fri, 7 Jul 2023 15:55:35 -0400 Subject: [PATCH 1/2] ensures transformed paths are relative paths --- .../map-imports-to-path-aliases.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/compiler/transformers/map-imports-to-path-aliases.ts b/src/compiler/transformers/map-imports-to-path-aliases.ts index b0194b5fe5a..3e993b45dc7 100644 --- a/src/compiler/transformers/map-imports-to-path-aliases.ts +++ b/src/compiler/transformers/map-imports-to-path-aliases.ts @@ -74,6 +74,39 @@ export const mapImportsToPathAliases = ( importPath = normalizePath( relative(dirname(destinationFilePath), resolvePathInDestination).replace(extensionRegex, '') ); + // if the importee is a sibling file of the importer then `relative` will + // produce a somewhat confusing result. We use `dirname` to get the + // directory of the importer, so for example, assume we have two files + // `foo/bar.ts` and `foo/baz.ts` like so: + // + // ``` + // foo + // ├── bar.ts + // └── baz.ts + // ``` + // + // then if `baz.ts` imports a symbol from `bar.ts` we'll call + // `relative(fromdir, to)` like so: + // + // ```ts + // relative(dirname("foo/baz.ts"), "foo/bar.ts") + // // equivalently + // relative("foo", "foo/bar.ts") + // ``` + // + // you'd think that in this case `relative` would return `'./bar.ts'` as + // a correct relative path to `bar.ts` from the `foo` directory, but + // actually in this case it returns just `bar.ts`. So since when updating + // import paths we're only concerned with `paths` aliases that should be + // transformed to relative imports anyway, we check to see if the new + // `importPath` starts with `'.'`, and add `'./'` if it doesn't, since + // otherwise Node will interpret `bar` as a module name, not a relative + // path. + // + // Note also that any relative paths as module specifiers which _don't_ + // need to be transformed (e.g. `'./foo'`) have already been handled + // above. + importPath = importPath.startsWith('.') ? importPath : './' + importPath; } } From 247b94853cc25efd7c5ebfa82db4d7133eddc638 Mon Sep 17 00:00:00 2001 From: Tanner Reits Date: Fri, 7 Jul 2023 16:16:40 -0400 Subject: [PATCH 2/2] fix test expected path --- .../transformers/test/map-imports-to-path-aliases.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/test/map-imports-to-path-aliases.spec.ts b/src/compiler/transformers/test/map-imports-to-path-aliases.spec.ts index 2dac82973bc..4d656d6c3f8 100644 --- a/src/compiler/transformers/test/map-imports-to-path-aliases.spec.ts +++ b/src/compiler/transformers/test/map-imports-to-path-aliases.spec.ts @@ -122,7 +122,7 @@ describe('mapImportsToPathAliases', () => { module = transpileModule(inputText, config, null, [], [mapImportsToPathAliases(config, '', outputTarget)]); - expect(module.outputText).toContain('import { utils } from "utils";'); + expect(module.outputText).toContain('import { utils } from "./utils";'); }); // The resolved module is not part of the output directory