-
Notifications
You must be signed in to change notification settings - Fork 13
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: try to use index.* when package.json not exists #32
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThe pull request introduces modifications to the module resolution process in Changes
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
commit: |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #32 +/- ##
==========================================
+ Coverage 89.44% 89.46% +0.01%
==========================================
Files 6 6
Lines 616 617 +1
Branches 104 104
==========================================
+ Hits 551 552 +1
Misses 65 65 ☔ View full report in Codecov by Sentry. |
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/import.ts
(1 hunks)test/fixtures/cjs/extend/index.js
(1 hunks)test/fixtures/ts-module/extend/index.ts
(1 hunks)test/import.test.ts
(3 hunks)
✅ Files skipped from review due to trivial changes (2)
- test/fixtures/ts-module/extend/index.ts
- test/fixtures/cjs/extend/index.js
🔇 Additional comments (4)
test/import.test.ts (4)
12-12
: Good addition to verify the resolution for cjs/extend
.
This new assertion adds clarity to confirm that importResolve
correctly resolves the cjs/extend
path.
33-33
: Ensures TypeScript extension resolution is tested.
Similarly, this line checks the TypeScript extend
path, strengthening the test coverage.
56-59
: Tests correctness of imported extend
object in CJS.
Confirming the extend
property is set to true
ensures the import works as expected.
61-64
: Verifies TS import under importDefaultOnly
option.
This assertion effectively ensures that TypeScript modules also expose the extend
property when only the default is imported.
let pkg: any = {}; | ||
const pkgFile = path.join(dirname, 'package.json'); | ||
if (fs.existsSync(pkgFile)) { | ||
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8')); | ||
return tryToResolveByDirnameFromPackage(dirname, pkg); | ||
pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8')); | ||
} | ||
return tryToResolveByDirnameFromPackage(dirname, pkg); |
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.
🛠️ Refactor suggestion
Use safer JSON parsing or add error handling.
Currently, the code directly reads and parses package.json
, which can throw an error if the file is malformed or has unexpected content. To make it more robust, consider wrapping the parse operation in a try/catch block and handling errors gracefully.
function tryToResolveByDirname(dirname: string): string | undefined {
let pkg: any = {};
const pkgFile = path.join(dirname, 'package.json');
if (fs.existsSync(pkgFile)) {
+ try {
pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8'));
+ } catch (err) {
+ debug('[tryToResolveByDirname] failed to parse package.json at %o: %o', pkgFile, err);
+ }
}
return tryToResolveByDirnameFromPackage(dirname, pkg);
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
let pkg: any = {}; | |
const pkgFile = path.join(dirname, 'package.json'); | |
if (fs.existsSync(pkgFile)) { | |
const pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8')); | |
return tryToResolveByDirnameFromPackage(dirname, pkg); | |
pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8')); | |
} | |
return tryToResolveByDirnameFromPackage(dirname, pkg); | |
let pkg: any = {}; | |
const pkgFile = path.join(dirname, 'package.json'); | |
if (fs.existsSync(pkgFile)) { | |
try { | |
pkg = JSON.parse(fs.readFileSync(pkgFile, 'utf-8')); | |
} catch (err) { | |
debug('[tryToResolveByDirname] failed to parse package.json at %o: %o', pkgFile, err); | |
} | |
} | |
return tryToResolveByDirnameFromPackage(dirname, pkg); |
[skip ci] ## [4.1.6](v4.1.5...v4.1.6) (2024-12-28) ### Bug Fixes * try to use index.* when package.json not exists ([#32](#32)) ([b4b2e0c](b4b2e0c))
Summary by CodeRabbit
New Features
extend
in both CommonJS and TypeScript modules, allowing access to a boolean value oftrue
.Bug Fixes
pkg
variable in the module resolution process.Tests
extend
modules in CommonJS and TypeScript contexts.