Skip to content

Commit

Permalink
fix nested package type acquisition
Browse files Browse the repository at this point in the history
  • Loading branch information
souporserious committed Oct 6, 2023
1 parent 19a9873 commit e489d07
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
69 changes: 37 additions & 32 deletions mdxts/utils/get-type-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,17 @@ function getAllDependencies(packageJson) {
)
}

async function findTypesPathFromTypeVersions(
packageJson,
packageName,
packageExport
) {
async function findTypesPathFromTypeVersions(packageJson, packageName) {
const rootPackageName = getRootPackageName(packageName)
const submoduleName = packageName.split('/').slice(1).join('/')

if (!packageJson.typesVersions) return null

const typeVersionsField = packageJson.typesVersions['*']

if (!typeVersionsField) return null

const typesPathsFromTypeVersions = typeVersionsField[packageExport]
const typesPathsFromTypeVersions = typeVersionsField[submoduleName]

if (!typesPathsFromTypeVersions) return null

Expand All @@ -78,7 +77,7 @@ async function findTypesPathFromTypeVersions(
const typesPath = path.resolve(
process.cwd(),
'node_modules',
packageName,
rootPackageName,
candidatePath
)

Expand All @@ -100,62 +99,68 @@ async function findParentNodeModulesPath(currentPath, packageName) {
return nodeModulesPath
} catch {
const parentPath = path.dirname(currentPath)
if (parentPath === currentPath) return null // We have reached the root directory
// We have reached the root directory
if (parentPath === currentPath) {
return null
}
return findParentNodeModulesPath(parentPath, packageName)
}
}

async function findTypesPath(packageJson, parentPackage, submodule) {
async function findTypesPath(packageJson, packageName) {
const typesField = packageJson.types || packageJson.typings
const isSubmodule = packageName.includes('/')

if (!submodule && typesField) {
return path.resolve(
process.cwd(),
'node_modules',
parentPackage,
typesField
)
if (!isSubmodule && typesField) {
return path.resolve(process.cwd(), 'node_modules', packageName, typesField)
}

if (submodule) {
if (isSubmodule) {
const typesPath = await findTypesPathFromTypeVersions(
packageJson,
parentPackage,
submodule
packageName
)
if (typesPath) return typesPath

if (typesPath) {
return typesPath
}
}

const parentNodeModulesPath = await findParentNodeModulesPath(
process.cwd(),
`@types/${parentPackage}`
`@types/${packageName}`
)

if (!parentNodeModulesPath) return null

return path.resolve(parentNodeModulesPath, 'index.d.ts')
}

/** Parses the root package name from a nested package name. */
function getRootPackageName(packageName: string) {
const isOrg = packageName.startsWith('@')

if (isOrg) {
return packageName.split('/').slice(0, 2).join('/')
}

return packageName.split('/').shift()
}

/** Fetches the types for a locally installed NPM package. */
export async function getTypeDeclarations(packageName) {
const [orgOrParent, parentPackageOrSubmodule, submoduleCandidate] =
packageName.split('/')
const isOrgPackage = orgOrParent.startsWith('@')
const parentPackage = isOrgPackage
? `${orgOrParent}/${parentPackageOrSubmodule}`
: orgOrParent
const submodule = isOrgPackage ? submoduleCandidate : parentPackageOrSubmodule
const parentPackagePath = path.resolve(
const rootPackageName = getRootPackageName(packageName)
const packageJsonPath = path.resolve(
process.cwd(),
'node_modules',
parentPackage,
rootPackageName,
'package.json'
)

try {
const packageJson = await getPackageJson(parentPackagePath)
const packageJson = await getPackageJson(packageJsonPath)
const allDependencies = getAllDependencies(packageJson)
const typesPath = await findTypesPath(packageJson, parentPackage, submodule)
const typesPath = await findTypesPath(packageJson, packageName)

// use ATA when dealing with @types since rollup is not reliable
if (typesPath.includes('@types/')) {
Expand Down
2 changes: 2 additions & 0 deletions site/docs/06.test.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
```tsx live
import { useState } from 'react'
import { Navigation } from 'mdxts/components'
import { Code } from 'mdxts/components/server'

/**
* Say hello.
Expand Down
10 changes: 9 additions & 1 deletion site/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { createMDXTSPlugin } from 'mdxts/next'
const withMDXTS = createMDXTSPlugin({
theme: 'theme.json',
gitSource: 'https://github.com/souporserious/mdxts/tree/main',
types: ['react', 'mdxts/components'],
types: [
'react',
'mdxts/components',
'mdxts/components/server',
'mdxts/components/client',
'mdxts/next',
'mdxts/rehype',
'mdxts/remark',
],
})

export default withMDXTS({
Expand Down

1 comment on commit e489d07

@vercel
Copy link

@vercel vercel bot commented on e489d07 Oct 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.