fix(cjs-interop): handle function default exports #396
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
I tried to use knip in Yarn PnP environments with this
vite.config.ts
and this error occured:
This error only occurred in Yarn PnP, and did not appear in Yarn with node_modules or in other package managers that resolve module paths through node_modules.
I analyzed the root cause and found that the failure was due to the following reasons:
knip loads config files using jiti internally:
Multiple scenarios cause CJS version selection:
CJS module structure:
jiti's interop logic fails:
Solution
The problem lies in jiti's
interopDefault
Proxy logic. When{ default: true }
is used, jiti tries to unwrap the default export, but the current logic doesn't handle CJS interop objects properly.Current logic (broken):
Fixed logic:
Result
AS-IS
TO-BE
Reproduce Bug
You can compare the AS-IS and TO-BE examples here: https://github.com/jungwoo3490/repro-jiti-396
1. Clone the repository
2. Move to the
as-is
directory, install dependencies, and run knip.then you should see the following error log:
3. Move to the
jiti
directory, install dependencies, and build the project.The
jiti
code here has already been modified as described above.4. Move to the
to-be
directory, install dependencies, and run knip:The
to-be
setup uses the modified jiti bundle.then you should see the following successful output: