import process from "node:process"; import type { ResolvedConfig } from "vite"; import { getLegacyName, prepareRollupInputs, resolveUserExternal } from "./utils"; import { EntryPoints, GeneratedFiles, FileInfos, FilesMetadatas, BuildEntryPoint } from "../types"; import { getOutputPath } from "./pathMapping"; export const getDevEntryPoints = (config: ResolvedConfig, viteDevServerUrl: string): EntryPoints => { const entryPoints: EntryPoints = {}; for (const [entryName, { inputRelPath, inputType }] of Object.entries(prepareRollupInputs(config))) { entryPoints[entryName] = { [inputType]: [`${viteDevServerUrl}${config.base}${inputRelPath}`], }; } return entryPoints; }; export const getFilesMetadatas = (base: string, generatedFiles: GeneratedFiles): FilesMetadatas => { return Object.fromEntries( Object.values(generatedFiles) .filter((fileInfos: FileInfos) => fileInfos.hash) .map((fileInfos: FileInfos) => [ `${base}${fileInfos.outputRelPath}`, { hash: fileInfos.hash, }, ]), ); }; export const getBuildEntryPoints = (generatedFiles: GeneratedFiles, viteConfig: ResolvedConfig): EntryPoints => { const entryPoints: EntryPoints = {}; let hasLegacyEntryPoint = false; /** get an Array of entryPoints from build.rollupOptions.input inside vite config file */ const entryFiles = prepareRollupInputs(viteConfig); for (const [entryName, entry] of Object.entries(entryFiles)) { const outputRelPath = getOutputPath(entry.inputRelPath); if (!outputRelPath) { console.error("unable to get outputPath", entry.inputRelPath); process.exit(1); } const fileInfos = generatedFiles[outputRelPath]; if (!fileInfos) { console.error("unable to map generatedFile", entry, outputRelPath, fileInfos); process.exit(1); } const legacyInputRelPath = getLegacyName(entry.inputRelPath); const legacyFileInfos = generatedFiles[getOutputPath(legacyInputRelPath)!] ?? null; if (legacyFileInfos) { hasLegacyEntryPoint = true; entryPoints[`${entryName}-legacy`] = resolveBuildEntrypoint(legacyFileInfos, generatedFiles, viteConfig, false); } entryPoints[entryName] = resolveBuildEntrypoint( fileInfos, generatedFiles, viteConfig, hasLegacyEntryPoint ? `${entryName}-legacy` : false, ); } /** * legacy polyfills target browsers that don't support ESM at all. * added as