UNPKG

vite-plugin-react-server

Version:
132 lines (129 loc) 3.91 kB
/** * vite-plugin-react-server * Copyright (c) Nico Brinkkemper * MIT License */ import { createInputNormalizer } from './helpers/inputNormalizer.js'; import { DEFAULT_CONFIG } from './config/defaults.js'; async function collectModuleGraphCss({ moduleGraph, pagePath, onCss, parentUrl }) { if (!pagePath) return /* @__PURE__ */ new Map(); const cssFiles = /* @__PURE__ */ new Map(); const pageModule = await moduleGraph.getModuleByUrl(pagePath, true); if (!pageModule) { return /* @__PURE__ */ new Map(); } const seen = /* @__PURE__ */ new Set(); const walkModule = (mod) => { if (!mod?.id || seen.has(mod.id)) return; seen.add(mod.id); if (mod?.id?.endsWith(".css")) { cssFiles.set(mod?.url, mod?.id); } mod?.importedModules?.forEach((imp) => walkModule(imp)); }; walkModule(pageModule); return cssFiles; } function collectManifestClientFiles({ manifest, root, pagePath, preserveModulesRoot, moduleBase, onCss, onClientModule, testClient = DEFAULT_CONFIG.AUTO_DISCOVER.clientComponents, testJson = DEFAULT_CONFIG.AUTO_DISCOVER.jsonPattern }) { const normalizer = createInputNormalizer({ root, removeExtension: true, preserveModulesRoot: preserveModulesRoot ? moduleBase : undefined }); const [_, value] = normalizer(pagePath); const cssFiles = /* @__PURE__ */ new Map(); const clientFiles = /* @__PURE__ */ new Map(); const seen = /* @__PURE__ */ new Set(); const manifestValues = Object.values(manifest); const possibleKeys = [ value // Relative path ]; const walkManifestEntry = (id, parentUrl) => { if (seen.has(id)) return; seen.add(id); const entry = manifest[id] ?? manifestValues.find((e) => id === e.file); if (!entry) { const hasKey = Object.keys(manifest).find((key) => id === key); if (hasKey) { console.log( `Manifest entry found for ${id}, but it is ${typeof entry}` ); return; } else { console.log( `No manifest entry found for ${id}, possible keys: ${Object.keys( manifest ).join(", ")}` ); } return; } if (typeof testClient === "function" && typeof onClientModule === "function" && testClient(entry.file) || typeof testJson === "function" && testJson(entry.file)) { onClientModule?.(entry.file ?? "", parentUrl); clientFiles.set(id, entry.name ?? ""); } if (entry.css) { entry.css.forEach((css) => { cssFiles.set(css, css); onCss?.(css, id); onClientModule?.(css, id); }); } if (entry.imports) { entry.imports.forEach( (imp) => walkManifestEntry(imp, entry.file) ); } if (entry.dynamicImports) { entry.dynamicImports.forEach( (imp) => walkManifestEntry(imp, entry.file) ); } }; for (const possibleKey of possibleKeys) { if (manifest[possibleKey]) { walkManifestEntry(possibleKey, pagePath); break; } } if (cssFiles.size === 0) { const entry = manifestValues.find( (e) => possibleKeys.includes(e.file) || e.src && possibleKeys.includes(e.src) || e.name && possibleKeys.includes(e.name) ); if (entry) { walkManifestEntry(value, pagePath); } else { const hasKey = Object.keys(manifest).find((key) => value === key); if (hasKey) { console.warn( `Manifest entry found for ${value}, but it is ${typeof manifest[hasKey]}` ); } else { console.warn( `No manifest entry found for ${value} (tried all possible keys: ${possibleKeys.join( ", " )} for manifest keys: ${Object.keys(manifest).join(", ")})` ); } } } return { cssFiles, clientFiles }; } export { collectManifestClientFiles, collectModuleGraphCss }; //# sourceMappingURL=collect-manifest-client-files.js.map