Skip to content

Commit

Permalink
fix(findDepencies): track dependencies properly
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Nov 15, 2024
1 parent 494e682 commit dc94f3c
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions src/findDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const findDependencies = (args: {
const visited = args.visited ?? []
const dependencies = args.imports ?? []
const packages = args.packages ?? new Set<string>()
let importsSubpathPatterns = args.importsSubpathPatterns ?? {}
const importsSubpathPatterns = args.importsSubpathPatterns ?? {}
if (visited.includes(sourceFilePath))
return {
dependencies,
Expand Down Expand Up @@ -64,17 +64,13 @@ export const findDependencies = (args: {
const moduleSpecifier = (
(node as ImportDeclaration).moduleSpecifier as StringLiteral
).text
const {
resolvedPath: file,
importsSubpathPatterns: updatedImportsSubpathPatterns,
} = resolve({
const { resolvedPath: file } = resolve({
moduleSpecifier,
sourceFilePath,
tsConfigFilePath,
tsConfig,

Check warning on line 71 in src/findDependencies.ts

View workflow job for this annotation

GitHub Actions / tests

Unsafe assignment of an `any` value
importsSubpathPatterns,
})
importsSubpathPatterns = updatedImportsSubpathPatterns
try {
const s = statSync(file)
if (!s.isDirectory()) dependencies.push(file)
Expand Down Expand Up @@ -144,7 +140,6 @@ const resolve = ({
}
)): {
resolvedPath: string
importsSubpathPatterns: Record<string, string>
} => {
if (moduleSpecifier.startsWith('.'))
return {
Expand All @@ -156,7 +151,6 @@ const resolve = ({
// Example: import { Network, notifyClients } from './notifyClients.js'
// The source file for that is actually in './notifyClients.ts'
.replace(/\.js$/, '.ts'),
importsSubpathPatterns,
}
if (
tsConfigFilePath !== undefined &&
Expand All @@ -172,23 +166,25 @@ const resolve = ({
tsConfig.compilerOptions.baseUrl,
resolvedPath,
)
importsSubpathPatterns[key] = [
tsConfig.compilerOptions.baseUrl,
path.sep,
resolvedPath.replace(/\.ts$/, '.js'),
].join('')
return {
resolvedPath: fullResolvedPath,
importsSubpathPatterns: {
...importsSubpathPatterns,
[key]: [
tsConfig.compilerOptions.baseUrl,
path.sep,
resolvedPath.replace(/\.ts$/, '.js'),
].join(''),
},
}
}
// Wildcard match
if (!key.includes('*')) continue
const rx = new RegExp(`^${key.replace('*', '(?<wildcard>.*)')}`)
const maybeMatch = rx.exec(moduleSpecifier)
if (maybeMatch?.groups?.wildcard === undefined) continue
importsSubpathPatterns[key] = [
tsConfig.compilerOptions.baseUrl,
path.sep,
resolvedPath.replace(/\.ts$/, '.js'),
].join('')
return {
resolvedPath: path
.resolve(
Expand All @@ -198,19 +194,10 @@ const resolve = ({
)
// Same as above, replace `.js` with `.ts`
.replace(/\.js$/, '.ts'),
importsSubpathPatterns: {
...importsSubpathPatterns,
[key]: [
tsConfig.compilerOptions.baseUrl,
path.sep,
resolvedPath.replace(/\.ts$/, '.js'),
].join(''),
},
}
}
}
return {
resolvedPath: moduleSpecifier,
importsSubpathPatterns,
}
}

0 comments on commit dc94f3c

Please sign in to comment.