Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
56 lines
2.4 KiB
Plaintext
56 lines
2.4 KiB
Plaintext
import crypto from 'crypto';
|
|
import path from 'path';
|
|
import { parsePayloadComponent } from './parsePayloadComponent.js';
|
|
/**
|
|
* Normalizes the component path based on the import map's base directory path.
|
|
*/ function getAdjustedComponentPath(importMapToBaseDirPath, componentPath) {
|
|
// Normalize input paths to use forward slashes
|
|
const normalizedBasePath = importMapToBaseDirPath.replace(/\\/g, '/');
|
|
const normalizedComponentPath = componentPath.replace(/\\/g, '/');
|
|
// Base path starts with './' - preserve the './' prefix
|
|
// => import map is in a subdirectory of the base directory, or in the same directory as the base directory
|
|
if (normalizedBasePath.startsWith('./')) {
|
|
// Remove './' from component path if it exists
|
|
const cleanComponentPath = normalizedComponentPath.startsWith('./') ? normalizedComponentPath.substring(2) : normalizedComponentPath;
|
|
// Join the paths to preserve the './' prefix
|
|
return `${normalizedBasePath}${cleanComponentPath}`;
|
|
}
|
|
return path.posix.join(normalizedBasePath, normalizedComponentPath);
|
|
}
|
|
/**
|
|
* Adds a payload component to the import map.
|
|
*/ export function addPayloadComponentToImportMap({ importMap, importMapToBaseDirPath, imports, payloadComponent }) {
|
|
if (!payloadComponent) {
|
|
return null;
|
|
}
|
|
const { exportName, path: componentPath } = parsePayloadComponent(payloadComponent);
|
|
if (importMap[componentPath + '#' + exportName]) {
|
|
return null;
|
|
}
|
|
const importIdentifier = exportName + '_' + crypto.createHash('md5').update(componentPath).digest('hex');
|
|
importMap[componentPath + '#' + exportName] = importIdentifier;
|
|
const isRelativePath = componentPath.startsWith('.') || componentPath.startsWith('/');
|
|
if (isRelativePath) {
|
|
const adjustedComponentPath = getAdjustedComponentPath(importMapToBaseDirPath, componentPath);
|
|
imports[importIdentifier] = {
|
|
path: adjustedComponentPath,
|
|
specifier: exportName
|
|
};
|
|
return {
|
|
path: adjustedComponentPath,
|
|
specifier: exportName
|
|
};
|
|
} else {
|
|
// Tsconfig alias or package import, e.g. '@payloadcms/ui' or '@/components/MyComponent'
|
|
imports[importIdentifier] = {
|
|
path: componentPath,
|
|
specifier: exportName
|
|
};
|
|
return {
|
|
path: componentPath,
|
|
specifier: exportName
|
|
};
|
|
}
|
|
}
|
|
|
|
//# sourceMappingURL=addPayloadComponentToImportMap.js.map |