vite-plugin-react-server
Version:
Vite plugin for React Server Components (RSC)
68 lines (65 loc) • 2.11 kB
JavaScript
/**
* vite-plugin-react-server
* Copyright (c) Nico Brinkkemper
* MIT License
*/
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { createInputNormalizer } from './helpers/inputNormalizer.js';
let stashedFiles = null;
const resolveFileOption = (pageOrProps) => {
if (typeof pageOrProps === "string") {
return () => pageOrProps;
}
return pageOrProps;
};
async function checkFilesExist(pages, options, root) {
if (stashedFiles) {
return stashedFiles;
}
if (!root || root === "") {
throw new Error("Root not found");
}
const errors = [];
const pageSet = /* @__PURE__ */ new Set();
const propsSet = /* @__PURE__ */ new Set();
const pageMap = /* @__PURE__ */ new Map();
const propsMap = /* @__PURE__ */ new Map();
const urlMap = /* @__PURE__ */ new Map();
const normalizer = createInputNormalizer({
root,
preserveModulesRoot: options.build.preserveModulesRoot === true ? options.moduleBase : undefined,
removeExtension: true
});
const pageFn = resolveFileOption(options.Page);
const propsFn = resolveFileOption(options.props);
for (const page of pages) {
const pagePath = pageFn(page);
const propsPath = propsFn(page);
const [pageKey, pageValue] = normalizer(pagePath);
const [propsKey, propsValue] = normalizer(propsPath);
try {
if (!existsSync(join(root, pageValue))) {
errors.push(
`Page file not found: ${pagePath}, ${join(root, pagePath)}`
);
}
if (!existsSync(join(root, propsValue))) {
errors.push(
`Props file not found: ${propsPath}, ${join(root, propsPath)}`
);
}
} catch (error) {
errors.push(`Error checking files: ${error}`);
}
urlMap.set(page, { props: propsPath, page: pagePath });
pageSet.add(pagePath);
propsSet.add(propsPath);
pageMap.set(pageKey, pageValue);
propsMap.set(propsKey, propsValue);
}
stashedFiles = { pageMap, pageSet, propsMap, propsSet, urlMap, errors };
return stashedFiles;
}
export { checkFilesExist };
//# sourceMappingURL=checkFilesExist.js.map