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: Don't replace .d.ts paths #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/utils/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

/** */
import { isAbsolute, normalize, relative, resolve } from 'path';
import { isAbsolute, normalize, relative, resolve, parse, sep } from 'path';
import { findBasePathOfAlias, relativeOutPathToConfigDir } from '../helpers';
import { Alias, IProjectConfig, PathLike } from '../interfaces';

Expand Down Expand Up @@ -79,9 +79,21 @@ export class TrieNode<T> {
prefix: alias.replace(/\*$/, ''),
// Normalize paths.
paths: paths[alias].map((path) => {
path = path
.replace(/\*$/, '')
.replace(/\.([mc])?ts(x)?$/, '.$1js$2');
path = path.replace(/\*$/, '');

const { dir, base } = parse(path);

const dotIndex = base.indexOf('.');
const name = base.slice(0, dotIndex);
const extension = base.slice(dotIndex);
Comment on lines +86 to +88
Copy link
Author

Choose a reason for hiding this comment

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

You might wonder why I did this instead of just using the extension already given to me by parse. This is because they consider xyz.d.ts to have a name of xyz.d and an extension of .ts


const normalizedExtension = extension.replace(
/^\.([mc])?ts(x)?$/,
Copy link
Author

Choose a reason for hiding this comment

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

I added a ^ which means the whole extension must be matched. This may need to have smarter logic.

Files to definitely exclude:

  • foo.d.css.ts - The correct way of defining a declaration file for foo.css file. Cannot point to a js file.
  • foo.css.d.ts - A legacy way of defining a foo.css file. Cannot point to a js file.
  • foo.d.ts - Declaration files for a JS file. This JS file could technically exist in repo but if it does the .d.ts file should probably exist alongside. Also if you're doing this you probably should have a plain .ts file instead.
  • foo.xyz.ts - I'm unsure why you'd write this but it would be broken by this regex change. Maybe some libraries make this common or some repos do this?

If you'd like me to look into a better way, I can.

Copy link
Author

Choose a reason for hiding this comment

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

I decided I may as well push a fix for foo.xyz.ts. See "Add explicit isDTS detection"

'.$1js$2'
);

path = dir + sep + name + normalizedExtension;

if (isAbsolute(path)) {
path = relative(
resolve(config.configDir, config.baseUrl),
Expand Down