-
Notifications
You must be signed in to change notification settings - Fork 118
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: esm json import #416
fix: esm json import #416
Conversation
@jly36963 let's make a separate PR that drops Node 10 in |
@bcoe sounds good |
@jly36963 a little buy this week, but have this on my mind. If you don't get to it, I'll work on dropping Node 10 later in the week so we can merge. |
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.
Left a note about adding a couple tests.
I believe the failing tests are due to a regression with @types/node
, upgrading the dependency should help.
lib/index.ts
Outdated
@@ -42,7 +45,7 @@ const parser = new YargsParser({ | |||
if (typeof require !== 'undefined') { | |||
return require(path) | |||
} else if (path.match(/\.json$/)) { | |||
return readFileSync(path, 'utf8') | |||
return esmRequire(path) |
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.
It would be good to add one test that exercises this line.
Perhaps we can add a yargs-parser.mjs
test to exercise this path? I believe it might also be good to add a test in yargs-parser.cjs
, to confirm that we haven't broken JSON importing for CommonJS.
@bcoe I'm not very familiar with tscc / tsickle / closure compiler. optimize is failing because tsickle overrides module flag as commonjs, which breaks |
@jkrems can you help figure this out, I want to make sure we don't break Google's use case. |
This may break in general in environments without native Anyhow, drive-by comment on the PR: I'm not sure if you want the require to be relative to P.S.: If you're using |
@jkrems thank you for the review. |
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.
I like where this work landed 👍
Addresses: yargs/yargs#2040
Problem
The esm require for json files wasn't working properly. I offered a workaround in a comment on the issue above (passing a custom ConfigCallback to
yargs.config()
), but the underlying issue was the way yargs-parser handles the importing of the json file.Solution
readFileSync(path, 'utf8')
reads the json file as a string, but it needs to be a parsed object. To make it behave the same way as the cjsrequire
, there are two easy solutions.I chose the
createRequire
solution, which required me to change the modules from es2015 to es2020 in tsconfig. I believe it would also require dropping support for Node 10, ascreateRequire
was implemented in node 12. If that's a deal-breaker, I'm cool with switching to theJSON.parse
solution instead.Note
It looks like optimize and typescript are at odds now. Because of the TS 4.4 breaking change, I need to add types to the catch block errors, but optimize doesn't like it.