-
-
Notifications
You must be signed in to change notification settings - Fork 537
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
Allow specifying "require" option via tsconfig #925
Allow specifying "require" option via tsconfig #925
Conversation
This reverts commit 700a90f.
48b57cf
to
e7690b9
Compare
I came across another use-case for this: using ts-node with tsconfig-paths via shebang.
The alternative is to import |
Hi @cspotcode, I've found myself wanting this behaviour in one of my projects - for a similar reason to the use case you outlined above. I'd like to use Given the age, are you likely to finish this PR? If not I might attempt something similar when I get some free time! |
@ArkadyDR I've merged the latest master so this PR is ready for another review. blakeembrey and I didn't agree whether or not this should be a feature, so I shelved it in order to get #921 released. I still think it's a good feature for the reasons that you cite, but we'll need to come to agreement. @blakeembrey, want to revisit this? |
Codecov Report
@@ Coverage Diff @@
## master #925 +/- ##
==========================================
+ Coverage 74.91% 75.20% +0.28%
==========================================
Files 6 6
Lines 606 617 +11
Branches 141 146 +5
==========================================
+ Hits 454 464 +10
Misses 100 100
- Partials 52 53 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
CI seems to be stuck but as far as I can tell all tests are fixed. It properly resolves |
Absolutely, sounds like a good idea now that we shipped the basic feature. |
src/bin.ts
Outdated
@@ -199,6 +199,8 @@ export function main (argv: string[]) { | |||
: undefined | |||
}) | |||
|
|||
const requires = argsRequire.length !== 0 ? argsRequire : service.options.require || [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we merge these lists together? Seems odd to have the CLI entirely overwrite the options when specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense. I can't think of a good use-case where you'd want the --require
flag to suppress whatever was specified in the tsconfig.
@@ -991,6 +1005,14 @@ function readConfig ( | |||
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames | |||
}, basePath, undefined, configFileName)) | |||
|
|||
if (tsconfigOptions.require) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on doing the require for these options within index.ts
so configuration flows in a single direction? It might make it easier instead of needing to resolve and pass it back to the CLI which requires. Also, this would ensure we cover the programmatic usage of register()
and via ts-node/register
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I like that.
The CLI uses Module._preloadModules
; do you know if that function is doing anything special that will break if we switch to a normal require()
call? There's nothing wrong with _preloadModules
but I don't really understand what it does other than require()
stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I recall I tried to change it one time. Feel free to keep the CLI logic as it is and build the internal one separately for now so there's no regression. Here's the implementation in node.js: https://github.com/nodejs/node/blob/ec2ffd6b9d255e19818b6949d2f7dc7ac70faee9/lib/internal/modules/cjs/loader.js#L1400-L1417.
Here's where I had changed it at one point, but I can't seem to find the issue. I should have linked things better in the past 🤕 2e99c50#diff-ee45801c137c3b9f9d8ec7ece340a514R197.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main thing it's trying to ensure is that the require()
is occurring from the place where you run the script, not where ts-node
lives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I don't think there's anything wrong with calling _preloadModules
from within register()
, so I'll keep doing that.
Should it happen in register()
or in create()
? I feel like register()
makes more sense because that's where we "install" the environment. But source map support is installed in create()
.
Thanks @cspotcode and @blakeembrey for jumping on this so quickly!
If it helps, my expectation would be in line with where you're going - I would think that any CLI requires would be in addition to what is in the config, although you may want to de-duplicate them if there would be an overhead in processing something twice? If someone had a case where one of the configured requires was innapropriate for a script, the solution would probably be to spin off one or more extra tsconfig files (using the extension behaviour). |
…`register()` instead of bin.ts
Ok, here's how the new behavior works:
When Items pulled from Programmatically passed items are not resolved and are passed verbatim to |
@ArkadyDR this is released in v9.0.0: https://github.com/TypeStrong/ts-node/releases/tag/v9.0.0 |
A follow-up to #921, moved into a separate PR per review comments. The diff for this will look much better after #921 is merged.
Allows specifying
--require
options via tsconfig.Questions
create()
andregister()
will parse the option from tsconfig. Should they also do therequire()
s? Right now that's only done bybin