import path from 'path';
import { encodeRoutePath } from './handlers/pathNormalizer';
import { buildDocApiPath } from '../utils/docUtils';
export const PREVIEW_HOST_MODULE_PREFIX = 'virtual:axhub-preview-host.js?';
export interface PreviewHostModuleOptions {
type: 'components' | 'prototypes' | 'themes';
name: string;
entryImportPath: string;
resourcePath: string;
resourceUrlPath: string;
editableHackCssHref: string | null;
initialHackCssEnabled: boolean;
versionId?: string;
}
export interface HackCssUpdatePayload {
resourcePath: string;
href: string;
removed: boolean;
timestamp: number;
}
export interface DocUpdatePayload {
docUrl: string;
filePath: string;
removed: boolean;
timestamp: number;
}
export function createPreviewHostModuleId(options: PreviewHostModuleOptions): string {
const params = new URLSearchParams({
resourceType: options.type,
name: options.name,
entry: options.entryImportPath,
resourcePath: options.resourcePath,
resourceUrlPath: options.resourceUrlPath,
initialHackCssEnabled: options.initialHackCssEnabled ? '1' : '0',
});
if (options.editableHackCssHref) {
params.set('editableHackCssHref', options.editableHackCssHref);
}
if (options.versionId) {
params.set('versionId', options.versionId);
}
return `${PREVIEW_HOST_MODULE_PREFIX}${params.toString()}`;
}
export function parsePreviewHostModuleId(id: string): PreviewHostModuleOptions | null {
if (!id.startsWith(PREVIEW_HOST_MODULE_PREFIX)) {
return null;
}
const params = new URLSearchParams(id.slice(PREVIEW_HOST_MODULE_PREFIX.length));
const type = params.get('resourceType');
const name = params.get('name');
const entryImportPath = params.get('entry');
const resourcePath = params.get('resourcePath');
const resourceUrlPath = params.get('resourceUrlPath');
if (
(type !== 'components' && type !== 'prototypes' && type !== 'themes')
|| !name
|| !entryImportPath
|| !resourcePath
|| !resourceUrlPath
) {
return null;
}
return {
type,
name,
entryImportPath,
resourcePath,
resourceUrlPath,
editableHackCssHref: params.get('editableHackCssHref'),
initialHackCssEnabled: params.get('initialHackCssEnabled') === '1',
versionId: params.get('versionId') || undefined,
};
}
export function replacePreviewLoaderScript(html: string, previewHostModuleCode: string): string {
const indentedCode = previewHostModuleCode
.split('\n')
.map((line) => ` ${line}`)
.join('\n');
const loaderScript = ` `;
const legacyLoaderPattern = /