Skip to content
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: import resolve find in paths's node_modules #35

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading