diff --git a/packages/find-dependencies/src/find-dependencies.js b/packages/find-dependencies/src/find-dependencies.js index 46083845..9402ca46 100644 --- a/packages/find-dependencies/src/find-dependencies.js +++ b/packages/find-dependencies/src/find-dependencies.js @@ -5,18 +5,13 @@ import { init, parse } from 'es-module-lexer'; import { isBareModuleSpecifier, - splitPath, - traverseUp } from './utils.js'; -const require = createRequire(import.meta.url); - /** * * @param {string[]} paths * @param {{ * nodeModulesDepth?: number, - * basePath?: string, * }} options * @returns {Promise} */ @@ -25,25 +20,21 @@ export async function findDependencies(paths, options = {}) { const dependencies = new Set(); const nodeModulesDepth = options?.nodeModulesDepth ?? 3; - const basePath = options?.basePath ?? process.cwd(); /** Init es-module-lexer wasm */ await init; - paths.forEach(path => { - const source = fs.readFileSync(path).toString(); + paths.forEach(filePath => { + const source = fs.readFileSync(filePath).toString(); const [imports] = parse(source); + const pathRequire = createRequire(path.resolve(filePath)); + imports?.forEach(i => { /** Skip built-in modules like fs, path, etc */ if(builtinModules.includes(i.n)) return; try { - const pathToDependency = require.resolve(i.n, {paths: [ - /** Current project's node_modules */ - basePath, - /** Monorepo, look upwards in filetree n times */ - ...traverseUp(nodeModulesDepth) - ]}); + const pathToDependency = pathRequire.resolve(i.n); importsToScan.add(pathToDependency); dependencies.add(pathToDependency); @@ -60,24 +51,18 @@ export async function findDependencies(paths, options = {}) { const source = fs.readFileSync(dep).toString(); const [imports] = parse(source); + const depRequire = createRequire(dep); + imports?.forEach(i => { /** Skip built-in modules like fs, path, etc */ if(builtinModules.includes(i.n)) return; - const { packageRoot } = splitPath(dep); const fileToFind = isBareModuleSpecifier(i.n) ? i.n : path.join(path.dirname(dep), i.n); try { /** * First check in the dependencies' node_modules, then in the project's node_modules, * then up, and up, and up */ - const pathToDependency = require.resolve(fileToFind, {paths: [ - /** Nested node_modules */ - packageRoot, - /** Current project's node_modules */ - basePath, - /** Monorepo, look upwards in filetree n times */ - ...traverseUp(nodeModulesDepth) - ]}); + const pathToDependency = depRequire.resolve(fileToFind); /** * Don't add dependencies we've already scanned, also avoids circular dependencies * and multiple modules importing from the same module @@ -94,4 +79,4 @@ export async function findDependencies(paths, options = {}) { } return [...dependencies]; -} \ No newline at end of file +} diff --git a/packages/find-dependencies/src/utils.js b/packages/find-dependencies/src/utils.js index 62497c95..ea850838 100644 --- a/packages/find-dependencies/src/utils.js +++ b/packages/find-dependencies/src/utils.js @@ -6,14 +6,6 @@ import path from 'path'; */ const toUnix = p => p.replace(/\\/g, '/'); -/** - * @param {number} depth - * @returns {string[]} - */ -export function traverseUp(depth) { - return Array(depth).fill().map((_, i) => path.join(process.cwd(), ...Array(i).fill('..'))); -} - /** * @param {string} specifier * @returns {boolean} @@ -117,4 +109,4 @@ export function getUniquePackages(paths) { unique.add(packageName); }); return [...unique]; -} \ No newline at end of file +} diff --git a/packages/find-dependencies/test/find-dependencies.test.js b/packages/find-dependencies/test/find-dependencies.test.js index 19699002..ddd5c6ab 100644 --- a/packages/find-dependencies/test/find-dependencies.test.js +++ b/packages/find-dependencies/test/find-dependencies.test.js @@ -7,7 +7,7 @@ import { findDependencies } from '../src/find-dependencies.js'; describe('find-dependencies', ({it}) => { it('finds dependencies for monorepo setup', async () => { const globs = await globby(['fixtures/monorepo/packages/my-package/*.js']); - let dependencies = await findDependencies(globs, { basePath: 'fixtures/monorepo/packages/my-package' }); + let dependencies = await findDependencies(globs); dependencies = dependencies.map(d => d.split('fixtures')[1]); assert.deepEqual(dependencies, @@ -31,7 +31,7 @@ describe('find-dependencies', ({it}) => { it('finds dependencies for regular setup', async () => { const globs = await globby(['fixtures/regular/index.js']); - let dependencies = await findDependencies(globs, { basePath: 'fixtures/regular' }); + let dependencies = await findDependencies(globs); dependencies = dependencies.map(d => d.split('fixtures')[1]); assert.deepEqual(dependencies, @@ -42,4 +42,4 @@ describe('find-dependencies', ({it}) => { ] ) }); -}); \ No newline at end of file +}); diff --git a/packages/find-dependencies/test/utils.test.js b/packages/find-dependencies/test/utils.test.js index 47ec6aa1..4ca017c7 100644 --- a/packages/find-dependencies/test/utils.test.js +++ b/packages/find-dependencies/test/utils.test.js @@ -95,4 +95,4 @@ describe('utils', () => { }); }); }); -}); \ No newline at end of file +});