fix: esm json import#416
fix: esm json import#416bcoe merged 10 commits intoyargs:mainfrom jly36963:fix-broken-esm-json-import
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. |
bcoe
left a comment
There was a problem hiding this comment.
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
| return require(path) | ||
| } else if (path.match(/\.json$/)) { | ||
| return readFileSync(path, 'utf8') | ||
| return esmRequire(path) |
There was a problem hiding this comment.
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. |
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
createRequiresolution, which required me to change the modules from es2015 to es2020 in tsconfig. I believe it would also require dropping support for Node 10, ascreateRequirewas implemented in node 12. If that's a deal-breaker, I'm cool with switching to theJSON.parsesolution 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.