Skip to content

Commit

Permalink
Parse JS dependencies correctly
Browse files Browse the repository at this point in the history
To correctly parse JS dependencies, we need to:
1. Pass a long enough directory path so that
   enhanced-resolve can find the root node_modules
2. Pass the webpack config
  • Loading branch information
noahtallen committed May 25, 2021
1 parent 9c47bc7 commit a3b61dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/dependency-finder/src/lib/entrypoints/additional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* External dependencies
*/
import globby from 'globby';
import { resolve } from 'path';

export const findAdditionalEntryPoints = async (
additionalEntryPoints: string[]
): Promise< string[] > => {
return (
await Promise.all( additionalEntryPoints.map( ( pattern ) => globby( pattern ) ) )
).flat();
return ( await Promise.all( additionalEntryPoints.map( ( pattern ) => globby( pattern ) ) ) )
.flat()
.map( ( entry ) => resolve( entry ) );
};
14 changes: 14 additions & 0 deletions packages/dependency-finder/src/lib/find-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ export const findDependencies = async ( {
}
} )();

const webpackConfig = ( () => {
try {
const webpackPath = path.join( pkg, 'webpack.config.js' );
fs.accessSync( webpackPath );
modules.add( webpackPath );
return webpackPath;
} catch {
return null;
}
} )();

const absoluteAdditionalEntryPoints = await findAdditionalEntryPoints( additionalEntryPoints );

// Handles monorepo and npm results from parsed files.
Expand All @@ -77,10 +88,13 @@ export const findDependencies = async ( {
return true;
};

// Entrypoints need to be absolute paths at this point so that the webpack resolver
// knows to parse up the filesystem further. (Otherwise, it stops short before reaching the root node_modules.)
for ( const entrypoint of [ ...entrypoints, ...jestTests, ...absoluteAdditionalEntryPoints ] ) {
const tree = dependencyTree.toList( {
filename: entrypoint,
directory: path.dirname( entrypoint ),
webpackConfig: webpackConfig ?? undefined,
tsConfig: tsConfig ?? undefined,
visited,
filter: fileFilter,
Expand Down

0 comments on commit a3b61dc

Please sign in to comment.