Skip to content

Commit

Permalink
fix: import resolve find in paths's node_modules (#35)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced module resolution to search for modules within the
`node_modules` directory
- Added support for resolving both CommonJS and ECMAScript (ESM)
packages

- **Tests**
- Expanded test coverage for package resolution in different module
systems

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Dec 30, 2024
1 parent 72b4e9e commit bd772af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ export function importResolve(filepath: string, options?: ImportResolveOptions)
}
}

// find from node_modules
for (const p of paths) {
const resolvedPath = path.join(p, 'node_modules', filepath);
moduleFilePath = tryToResolveFromAbsoluteFile(resolvedPath);
if (moduleFilePath) {
debug('[importResolve:node_modules] %o => %o => %o',
filepath, resolvedPath, moduleFilePath);
return moduleFilePath;
}
}

const extname = path.extname(filepath);
if ((!isAbsolute && extname === '.json') || !isESM) {
moduleFilePath = getRequire().resolve(filepath, {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cjs/node_modules/inject/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/cjs/node_modules/inject/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/esm/node_modules/inject/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/esm/node_modules/inject/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('test/import.test.ts', () => {
}), getFilepath('cjs/index.js'));
});

it('should inject commonjs package from {paths}/node_modules', () => {
assert.equal(importResolve('inject', {
paths: [ getFilepath('cjs') ],
}), getFilepath('cjs/node_modules/inject/index.js'));
});

it('should work on commonjs and require exists', () => {
return coffee.fork(getFilepath('cjs/run.js'))
// .debug()
Expand All @@ -49,6 +55,12 @@ describe('test/import.test.ts', () => {
}, /Cannot find module/);
});

it('should inject esm package from {paths}/node_modules', () => {
assert.equal(importResolve('inject', {
paths: [ getFilepath('esm') ],
}), getFilepath('esm/node_modules/inject/index.js'));
});

it('should work on ts-module', () => {
assert.equal(importResolve(getFilepath('ts-module')), getFilepath('ts-module/index.ts'));
assert.equal(importResolve(getFilepath('ts-module/extend')), getFilepath('ts-module/extend/index.ts'));
Expand Down

0 comments on commit bd772af

Please sign in to comment.