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

feat(paths): Allow usage of TSConfig Paths #76

Merged
merged 8 commits into from
May 26, 2020
Merged

Conversation

EntraptaJ
Copy link
Member

This PR introduces the ability to use the TSConfig Paths feature/options.

@EntraptaJ EntraptaJ linked an issue May 25, 2020 that may be closed by this pull request
@EntraptaJ
Copy link
Member Author

@yoursunny It definitely isn't pretty, but it does work! If you don't mind taking a look at the code review for me.

src/index.ts Outdated Show resolved Hide resolved
@@ -5,6 +5,7 @@
"moduleResolution": "Node",

"allowSyntheticDefaultImports": true,
"declaration": true,
Copy link
Member Author

Choose a reason for hiding this comment

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

This will make it easier for people to extend TS-ESNode in their own scripts. Recently realized that most of my modules don't have declaration enabled, oops.

src/index.ts Show resolved Hide resolved
src/index.ts Outdated
Comment on lines 39 to 59
let forceRelative = false;
if (TSConfig && TSConfig.paths) {
for (const tsPath of Object.keys(TSConfig.paths)) {
const tsPathKey = tsPath.replace('/*', '');
if (specifier.startsWith(tsPathKey)) {
const pathSpecifier = TSConfig.paths[tsPath][0].replace(
'/*',
specifier.split(tsPathKey)[1],
);

forceRelative = true;

parentURL = `${
pathToFileURL(resolvePath(baseURL, TSConfig.baseUrl!)).href
}/`;
console.log(parentURL, pathSpecifier);

specifier = pathSpecifier;
}
}
}
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's a bit of a hack, but I need to swap out the "/*" in the key and the path, and then force relative so that the fileFinder function runs since most people aren't using extension ESModule imports in Node.JS. And I need to resolve the path from the TSConfig path because of how the baseUrl and stuff is used in TypeScript. If I was the person writing the TypeScript side of the Paths feature I would have done it diferently.

src/index.ts Outdated Show resolved Hide resolved
@EntraptaJ
Copy link
Member Author

@yoursunny To properly support Node.JS 13.9 I've set the target to ES2019 as you suggested. If you don't see anything horrific within the PR, I'll merge. Realized an old project I was porting used module-alias is what forced me to finally attempt to make this. Overall this feature was easier than expected, which is why I'm nervous I've made a major logic mistake here.

@@ -1,10 +1,11 @@
{
"compilerOptions": {
"target": "ESNext",
"target": "ES2019",
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 as it turns out Node.JS 13.9 only has nullish coalescing and optional chaining working with an optional harmony V8 flags.

@EntraptaJ
Copy link
Member Author

Let's hope this works, kinda expecting a bunch of issues introduced by this.

@EntraptaJ EntraptaJ merged commit 6e2f6af into master May 26, 2020
github-actions bot pushed a commit that referenced this pull request May 26, 2020
# [1.5.0](v1.4.1...v1.5.0) (2020-05-26)

### Features

* **paths:** Allow usage of TSConfig Paths ([#76](#76)) ([6e2f6af](6e2f6af))
@KJDev-Bot
Copy link
Contributor

🎉 This PR is included in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@EntraptaJ EntraptaJ deleted the feat-tsconfig-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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tsconfig paths are ignored
3 participants