{"version":3,"sources":["../../../../src/bin/generateImportMap/utilities/addPayloadComponentToImportMap.ts"],"sourcesContent":["import crypto from 'crypto'\nimport path from 'path'\n\nimport type { PayloadComponent } from '../../../config/types.js'\nimport type { Imports, InternalImportMap } from '../index.js'\n\nimport { parsePayloadComponent } from './parsePayloadComponent.js'\n\n/**\n * Normalizes the component path based on the import map's base directory path.\n */\nfunction getAdjustedComponentPath(importMapToBaseDirPath: string, componentPath: string): string {\n // Normalize input paths to use forward slashes\n const normalizedBasePath = importMapToBaseDirPath.replace(/\\\\/g, '/')\n const normalizedComponentPath = componentPath.replace(/\\\\/g, '/')\n\n // Base path starts with './' - preserve the './' prefix\n // => import map is in a subdirectory of the base directory, or in the same directory as the base directory\n if (normalizedBasePath.startsWith('./')) {\n // Remove './' from component path if it exists\n const cleanComponentPath = normalizedComponentPath.startsWith('./')\n ? normalizedComponentPath.substring(2)\n : normalizedComponentPath\n\n // Join the paths to preserve the './' prefix\n return `${normalizedBasePath}${cleanComponentPath}`\n }\n\n return path.posix.join(normalizedBasePath, normalizedComponentPath)\n}\n\n/**\n * Adds a payload component to the import map.\n */\nexport function addPayloadComponentToImportMap({\n importMap,\n importMapToBaseDirPath,\n imports,\n payloadComponent,\n}: {\n importMap: InternalImportMap\n importMapToBaseDirPath: string\n imports: Imports\n payloadComponent: PayloadComponent\n}): {\n path: string\n specifier: string\n} | null {\n if (!payloadComponent) {\n return null\n }\n const { exportName, path: componentPath } = parsePayloadComponent(payloadComponent)\n\n if (importMap[componentPath + '#' + exportName]) {\n return null\n }\n\n const importIdentifier =\n exportName + '_' + crypto.createHash('md5').update(componentPath).digest('hex')\n\n importMap[componentPath + '#' + exportName] = importIdentifier\n\n const isRelativePath = componentPath.startsWith('.') || componentPath.startsWith('/')\n\n if (isRelativePath) {\n const adjustedComponentPath = getAdjustedComponentPath(importMapToBaseDirPath, componentPath)\n\n imports[importIdentifier] = {\n path: adjustedComponentPath,\n specifier: exportName,\n }\n return {\n path: adjustedComponentPath,\n specifier: exportName,\n }\n } else {\n // Tsconfig alias or package import, e.g. '@payloadcms/ui' or '@/components/MyComponent'\n imports[importIdentifier] = {\n path: componentPath,\n specifier: exportName,\n }\n return {\n path: componentPath,\n specifier: exportName,\n }\n }\n}\n"],"names":["crypto","path","parsePayloadComponent","getAdjustedComponentPath","importMapToBaseDirPath","componentPath","normalizedBasePath","replace","normalizedComponentPath","startsWith","cleanComponentPath","substring","posix","join","addPayloadComponentToImportMap","importMap","imports","payloadComponent","exportName","importIdentifier","createHash","update","digest","isRelativePath","adjustedComponentPath","specifier"],"mappings":"AAAA,OAAOA,YAAY,SAAQ;AAC3B,OAAOC,UAAU,OAAM;AAKvB,SAASC,qBAAqB,QAAQ,6BAA4B;AAElE;;CAEC,GACD,SAASC,yBAAyBC,sBAA8B,EAAEC,aAAqB;IACrF,+CAA+C;IAC/C,MAAMC,qBAAqBF,uBAAuBG,OAAO,CAAC,OAAO;IACjE,MAAMC,0BAA0BH,cAAcE,OAAO,CAAC,OAAO;IAE7D,wDAAwD;IACxD,2GAA2G;IAC3G,IAAID,mBAAmBG,UAAU,CAAC,OAAO;QACvC,+CAA+C;QAC/C,MAAMC,qBAAqBF,wBAAwBC,UAAU,CAAC,QAC1DD,wBAAwBG,SAAS,CAAC,KAClCH;QAEJ,6CAA6C;QAC7C,OAAO,GAAGF,qBAAqBI,oBAAoB;IACrD;IAEA,OAAOT,KAAKW,KAAK,CAACC,IAAI,CAACP,oBAAoBE;AAC7C;AAEA;;CAEC,GACD,OAAO,SAASM,+BAA+B,EAC7CC,SAAS,EACTX,sBAAsB,EACtBY,OAAO,EACPC,gBAAgB,EAMjB;IAIC,IAAI,CAACA,kBAAkB;QACrB,OAAO;IACT;IACA,MAAM,EAAEC,UAAU,EAAEjB,MAAMI,aAAa,EAAE,GAAGH,sBAAsBe;IAElE,IAAIF,SAAS,CAACV,gBAAgB,MAAMa,WAAW,EAAE;QAC/C,OAAO;IACT;IAEA,MAAMC,mBACJD,aAAa,MAAMlB,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