From bd772afbf585283a8ecef1ea6cfcba668a98c511 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 30 Dec 2024 17:51:46 +0800 Subject: [PATCH] fix: import resolve find in paths's node_modules (#35) ## 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 --- src/import.ts | 11 +++++++++++ test/fixtures/cjs/node_modules/inject/index.js | 1 + test/fixtures/cjs/node_modules/inject/package.json | 3 +++ test/fixtures/esm/node_modules/inject/index.js | 1 + test/fixtures/esm/node_modules/inject/package.json | 3 +++ test/import.test.ts | 12 ++++++++++++ 6 files changed, 31 insertions(+) create mode 100644 test/fixtures/cjs/node_modules/inject/index.js create mode 100644 test/fixtures/cjs/node_modules/inject/package.json create mode 100644 test/fixtures/esm/node_modules/inject/index.js create mode 100644 test/fixtures/esm/node_modules/inject/package.json diff --git a/src/import.ts b/src/import.ts index 11e7bb3..f65af8d 100644 --- a/src/import.ts +++ b/src/import.ts @@ -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, { diff --git a/test/fixtures/cjs/node_modules/inject/index.js b/test/fixtures/cjs/node_modules/inject/index.js new file mode 100644 index 0000000..0eaba6b --- /dev/null +++ b/test/fixtures/cjs/node_modules/inject/index.js @@ -0,0 +1 @@ +exports.foo = 'bar'; diff --git a/test/fixtures/cjs/node_modules/inject/package.json b/test/fixtures/cjs/node_modules/inject/package.json new file mode 100644 index 0000000..39edc8e --- /dev/null +++ b/test/fixtures/cjs/node_modules/inject/package.json @@ -0,0 +1,3 @@ +{ + "name": "inject" +} diff --git a/test/fixtures/esm/node_modules/inject/index.js b/test/fixtures/esm/node_modules/inject/index.js new file mode 100644 index 0000000..c155820 --- /dev/null +++ b/test/fixtures/esm/node_modules/inject/index.js @@ -0,0 +1 @@ +export const foo = 'bar'; diff --git a/test/fixtures/esm/node_modules/inject/package.json b/test/fixtures/esm/node_modules/inject/package.json new file mode 100644 index 0000000..39edc8e --- /dev/null +++ b/test/fixtures/esm/node_modules/inject/package.json @@ -0,0 +1,3 @@ +{ + "name": "inject" +} diff --git a/test/import.test.ts b/test/import.test.ts index c40e857..d6a676b 100644 --- a/test/import.test.ts +++ b/test/import.test.ts @@ -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() @@ -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'));