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

feat: use createRequire instead of custom paths array #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 9 additions & 24 deletions packages/find-dependencies/src/find-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>}
*/
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -94,4 +79,4 @@ export async function findDependencies(paths, options = {}) {
}

return [...dependencies];
}
}
10 changes: 1 addition & 9 deletions packages/find-dependencies/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -117,4 +109,4 @@ export function getUniquePackages(paths) {
unique.add(packageName);
});
return [...unique];
}
}
6 changes: 3 additions & 3 deletions packages/find-dependencies/test/find-dependencies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -42,4 +42,4 @@ describe('find-dependencies', ({it}) => {
]
)
});
});
});
2 changes: 1 addition & 1 deletion packages/find-dependencies/test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ describe('utils', () => {
});
});
});
});
});