Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): ensures transformed paths are relative paths for dist-collection #4552

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/compiler/transformers/map-imports-to-path-aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a scenario where we could expect importPath to start with '..' here (e.g. '../foo/bar.ts')?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can, but the conditional here will only prepend the ./ if the path is not already relative. Some scenarios:

  • ../my/import/path -> ../my/import/path (no change)
  • ./my/import/path -> ./my/import/path (no change)
  • my/import/path -> ./my/import/oath (was not relative, so we make it relative)

It's safe to do this because we check that the path TS resolves is not external (i.e. a part of node_modules) before any of this transformation stuff happens. So, by this point, we know the path needs to be relative

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down