vite-plugin-react-server
Version:
Vite plugin for React Server Components (RSC)
146 lines (143 loc) • 5.04 kB
JavaScript
/**
* vite-plugin-react-server
* Copyright (c) Nico Brinkkemper
* MIT License
*/
import { normalizePath } from 'vite';
import { join } from 'path';
import { DEFAULT_CONFIG } from '../config/defaults.js';
const resolveExtensionOptions = (removeExtension) => {
if (typeof removeExtension === "boolean") {
if (removeExtension) {
return (path) => {
const extensionIndex = path.lastIndexOf(".");
return extensionIndex !== -1 ? path.slice(0, extensionIndex) : path;
};
}
return (path) => path;
}
if (typeof removeExtension === "string") {
return (path) => path.replace(removeExtension, "");
}
if (removeExtension instanceof RegExp) {
return (path) => removeExtension.test(path) ? path.replace(removeExtension, "") : path;
}
if (typeof removeExtension === "function") {
return (path) => {
const extIndex = path.lastIndexOf(".");
if (extIndex !== -1) {
const extension = path.slice(extIndex);
if (removeExtension(extension)) {
return path.slice(0, extIndex);
}
}
return path;
};
}
return (path) => path;
};
const resolveRootOption = (root, preserveModulesRoot) => {
if (typeof preserveModulesRoot === "string" && typeof root === "string") {
const normalizedPreserveModulesRoot = normalizePath(preserveModulesRoot);
if (normalizedPreserveModulesRoot.startsWith(root)) {
return normalizedPreserveModulesRoot.slice(root.length + 1);
}
return "";
} else if (typeof preserveModulesRoot === "string" && typeof root !== "string") {
return normalizePath(preserveModulesRoot);
}
return "";
};
const createKeyNormalizer = ({
root: normalizedRoot,
preserveModulesRoot,
handleExtension
}) => (key) => {
const virtualPrefix = key.match(/^\0+/)?.[0] || "";
const actualKey = key.slice(virtualPrefix.length);
let moduleId = normalizePath(actualKey);
if (moduleId.startsWith(normalizedRoot)) {
moduleId = moduleId.slice(normalizedRoot.length);
}
moduleId = handleExtension(moduleId);
while (moduleId.startsWith("/") || moduleId.startsWith(".")) {
moduleId = moduleId.slice(1);
}
while (moduleId.endsWith("/")) {
moduleId = moduleId.slice(0, -1);
}
if (typeof preserveModulesRoot === "string" && preserveModulesRoot !== "") {
moduleId = moduleId.startsWith(preserveModulesRoot) ? moduleId.slice(preserveModulesRoot.length) : moduleId;
}
return virtualPrefix + moduleId;
};
const createPathNormalizer = ({
root,
preserveModulesRoot
}) => (path) => {
if (typeof path !== "string") {
throw new Error(`Invalid path: ${JSON.stringify(path)}`);
}
let normalPath = normalizePath(path);
if (normalPath.startsWith(root)) {
normalPath = normalPath.slice(root.length);
}
if (typeof preserveModulesRoot === "string" && preserveModulesRoot !== "") {
normalPath = normalPath.startsWith(preserveModulesRoot) ? normalPath.slice(preserveModulesRoot.length) : normalPath;
}
while (normalPath.endsWith("/")) {
normalPath = normalPath.slice(0, -1);
}
return normalPath;
};
function createInputNormalizer({
root,
preserveModulesRoot = undefined,
removeExtension = DEFAULT_CONFIG.FILE_REGEX
}) {
const relativeRoot = resolveRootOption(root, preserveModulesRoot);
const handleExtension = resolveExtensionOptions(removeExtension);
const normalizeEntryKey = createKeyNormalizer({
root,
preserveModulesRoot: relativeRoot,
handleExtension
});
const normalizeEntryPath = createPathNormalizer({
root,
preserveModulesRoot: relativeRoot
});
function normalizeInput(id) {
if (Array.isArray(id)) {
const [key, path] = id;
if (typeof key === "string" && Array.isArray(path) && path.length === 2) {
const isNumber = !isNaN(Number(key));
if (isNumber) {
return normalizeInput([path[0], path[1]]);
}
return normalizeInput([join(key, path[0]), path[1]]);
}
if (typeof key !== "string" || typeof path !== "string") {
throw new Error(`Invalid input: ${JSON.stringify(id)}`);
}
return [normalizeEntryKey(key), normalizeEntryPath(path)];
} else if (typeof id === "string") {
return [normalizeEntryKey(id), normalizeEntryPath(id)];
} else if (typeof id === "object" && id !== null && "$$typeof" in id && "$$id" in id && typeof id.$$id === "string") {
const normalized = [
normalizeEntryKey(id.$$id),
normalizeEntryPath(id.$$id)
];
return normalized;
}
throw new Error(`Invalid input type: ${typeof id}`);
}
return (input) => {
const [key, path] = normalizeInput(input);
const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
const virtualPrefix = key.match(/^\0+/)?.[0] || "";
const finalPath = virtualPrefix ? normalizedPath.startsWith(virtualPrefix) ? normalizedPath : virtualPrefix + normalizedPath : normalizedPath;
return [key, finalPath];
};
}
export { createInputNormalizer };
//# sourceMappingURL=inputNormalizer.js.map