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(paths): Refactor Paths handling #80

Merged
merged 3 commits into from
May 27, 2020
Merged

Conversation

EntraptaJ
Copy link
Member

This PR refactors essentially the entire way the resolve function handles TSConfig paths to allow for single file "paths" along with allowing resolution of javascript files.

@EntraptaJ EntraptaJ added the bug Something isn't working label May 27, 2020
@EntraptaJ EntraptaJ self-assigned this May 27, 2020
@@ -89,7 +85,7 @@ export async function resolve(
}

// Let Node.js handle all other specifiers.
return defaultResolve(specifier, context, defaultResolve);
return defaultResolve(specifier, { ...context, parentURL }, defaultResolve);
Copy link
Member Author

Choose a reason for hiding this comment

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

Although there isn't anything else on context on most Node.JS versions, I want to future proof this for the chance that there is/will be extra data in context.

@EntraptaJ EntraptaJ linked an issue May 27, 2020 that may be closed by this pull request
Comment on lines +49 to +52
specifier = `./${relative(
dirname(fileURLToPath(parentURL)),
tsResolvedPath,
)}`;
Copy link
Member Author

Choose a reason for hiding this comment

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

Annoyingly I can't find a way to get relative to not return Utils/Math when I want it to be ./Utils/Math in the example tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

So even though this would/could/does return stuff like ./../../Utils/Math the ./../OTHER_PATH is a proper path.

(relativePathRegex.test(specifier) || forceRelative) &&
!hasExtensionRegex.test(fileName)
) {
if (relativePathRegex.test(specifier) && !hasExtensionRegex.test(fileName)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I can remove my stupid forceRelative hack now that I've made it so it returns relative paths instead of absolute.

}/`;

specifier = pathSpecifier;
break;
Copy link
Member Author

Choose a reason for hiding this comment

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

Can't believe I missed this before, I'll be adding in proper caching soon for the resolve hook.

if (TSConfig?.paths) {
for (const tsPath of Object.keys(TSConfig.paths)) {
const tsPathKey = tsPath.replace('/*', '');
const tsPathKey = tsPath.replace('*', '');
Copy link
Member Author

Choose a reason for hiding this comment

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

I feel like there could be a way to replace the * in tsPath and the path itself along with replacing the specifier, I tried a few ways to do it, but I can't find the flow/logic.

What I want it to do is somehow replace the specifier, tsPath and result in a single regex or statement.

Comment on lines +44 to +46
const tsResolvedPath = resolvePath(
resolvePath(baseURL, TSConfig.baseUrl!),
TSConfig.paths[tsPath][0].replace('*', specifier.split(tsPathKey)[1]),
Copy link
Member Author

Choose a reason for hiding this comment

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

There is likely a better way to do this, I can't find anything better myself.

@EntraptaJ
Copy link
Member Author

EntraptaJ commented May 27, 2020

@yoursunny If you don't mind, can you give this a quick look. Open to any feedback or suggestions, I'm just as uncertain about this method of handling the paths, but it does feel better, although there is room for improvement.

Copy link
Contributor

@yoursunny yoursunny left a comment

Choose a reason for hiding this comment

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

I tested this change along with #64 in NDNts codebase, and they appear to work well without using my custom hacks.

@yoursunny
Copy link
Contributor

@EntraptaJ EntraptaJ merged commit e537269 into master May 27, 2020
github-actions bot pushed a commit that referenced this pull request May 27, 2020
## [1.5.1](v1.5.0...v1.5.1) (2020-05-27)

### Bug Fixes

* **paths:** Refactor Paths handling ([#80](#80)) ([e537269](e537269))
@KJDev-Bot
Copy link
Contributor

🎉 This PR is included in version 1.5.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@EntraptaJ EntraptaJ deleted the fix-context-js-paths branch May 31, 2020 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TSConfig Paths: error with allowJs
3 participants