vite-plugin-react-server
Version:
Vite plugin for React Server Components (RSC)
104 lines (102 loc) • 2.88 kB
JavaScript
/**
* vite-plugin-react-server
* Copyright (c) Nico Brinkkemper
* MIT License
*/
function isFunction(value) {
return typeof value === "function";
}
async function resolveProps({
propsModule,
path,
exportName,
url
}) {
if (!propsModule) {
return {
type: "error",
error: new Error(`propsModule is ${typeof propsModule}`)
};
}
if (typeof propsModule !== "object") {
return {
type: "error",
error: new Error(
`propsModule must be an object, got ${typeof propsModule}`
)
};
}
const keys = Object.keys(propsModule);
let found = keys.find((v) => v === exportName || v === url || v === path);
if (exportName in propsModule) {
found = exportName;
}
if (found) {
let value = propsModule[found];
try {
if (isFunction(value)) {
const props = await value(url);
return {
type: "success",
key: found,
props
};
}
if (value && typeof value.then === "function") {
const props = await value;
return {
type: "success",
key: found,
props
};
}
if (typeof value === "object" && value !== null) {
return {
type: "success",
key: found,
props: value
};
}
return {
type: "error",
error: new Error(
`Expected props export "${exportName}" in "${path}" to be a function, promise, or object that resolves to props, instead got typeof ${typeof value}.`
)
};
} catch (error) {
console.trace(error);
console.warn(found, "error in resolveProps", propsModule, url, path);
return {
type: "error",
error
};
}
}
const commonjs = keys.find((v) => v === "exports");
if (!!commonjs) {
const exportKeys = commonjs["exports"] ? Object.keys(commonjs["exports"]) : [];
const foundCommonJS = exportKeys.find(
(v) => v === exportName || v === url || v === path
);
return {
type: "error",
error: new Error(
`Expected props export "${exportName}" in "${path}", but instead got "exports" with ${!!foundCommonJS ? foundCommonJS.toString() : exportKeys.length ? exportKeys.join(", ") : "no keys"}, this will not work. Make sure to set esModule: true in rollupOptions.output. ${JSON.stringify(propsModule)}`
)
};
}
if (Object.keys(propsModule).includes("error")) {
return {
type: "error",
error: propsModule["error"]
};
}
return {
type: "error",
error: new Error(
`Could not find props export "${exportName}" in "${path}". ${keys.length ? "Available exports: " + keys.join(", ") : 'The object was defined but has no properties. "' + JSON.stringify(propsModule) + '"'}`
)
};
}
export { resolveProps };
//# sourceMappingURL=resolveProps.js.map