import type * as d from '../../../declarations' ; import { dirname } from 'path' ; import { isString , parsePackageJson } from '@utils' ; import { parseCollection } from './parse-collection-module' ; import { tsResolveModuleNamePackageJsonPath } from '../../sys/typescript/typescript-resolve-module' ; export const addExternalImport = ( config , compilerCtx , buildCtx , moduleFile , containingFile , moduleId , resolveCollections ) => { if ( ! moduleFile . externalImports . includes ( moduleId ) ) { moduleFile . externalImports . push ( moduleId ) ; moduleFile . externalImports . sort ( ) ; } if ( ! resolveCollections || compilerCtx . resolvedCollections . has ( moduleId ) ) { return ; } let pkgJsonFilePath = tsResolveModuleNamePackageJsonPath ( config , compilerCtx , moduleId , containingFile ) ; compilerCtx . resolvedCollections . add ( moduleId ) ; if ( pkgJsonFilePath == null ) { return ; } const realPkgJsonFilePath = config . sys . realpathSync ( pkgJsonFilePath ) ; if ( realPkgJsonFilePath . path ) { pkgJsonFilePath = realPkgJsonFilePath . path ; } if ( pkgJsonFilePath === config . packageJsonFilePath ) { return ; } const pkgJsonStr = compilerCtx . fs . readFileSync ( pkgJsonFilePath ) ; if ( pkgJsonStr == null ) { return ; } const parsedPkgJson = parsePackageJson ( pkgJsonStr , pkgJsonFilePath ) ; if ( parsedPkgJson . diagnostic ) { buildCtx . diagnostics . push ( parsedPkgJson . diagnostic ) ; return ; } if ( ! isString ( parsedPkgJson . data . collection ) || ! parsedPkgJson . data . collection . endsWith ( '.json' ) ) { return ; } if ( ! isString ( parsedPkgJson . data . types ) || ! parsedPkgJson . data . types . endsWith ( '.d.ts' ) ) { return ; } const collection = parseCollection ( config , compilerCtx , buildCtx , moduleId , parsedPkgJson . filePath , parsedPkgJson . data ) ; if ( ! collection ) { return ; } const alreadyHasCollection = buildCtx . collections . some ( ( c ) => { return c . collectionName === collection . collectionName ; } ) ; if ( alreadyHasCollection ) { return ; } buildCtx . collections . push ( collection ) ; if ( Array . isArray ( collection . dependencies ) ) { collection . dependencies . forEach ( ( dependencyModuleId ) => { const resolveFromDir = dirname ( pkgJsonFilePath ) ; addExternalImport ( config , compilerCtx , buildCtx , moduleFile , resolveFromDir , dependencyModuleId , resolveCollections ) ; } ) ; } } ;