telefunc
Version:
Remote functions. Instead of API.
79 lines (78 loc) • 2.98 kB
JavaScript
export { determineOutDir };
export { getOutDirAbsolute };
import { assert, assertPosixPath, pathJoin, toPosixPath } from '../utils.js';
/** Appends `client/` or `server/` to `config.build.outDir` */
function determineOutDir(config) {
// The mechansism to detect a framework down below doesn't work for Astro
// - https://github.com/withastro/astro/issues/5211#issuecomment-1326084151
if (config.plugins.some((p) => { var _a; return (_a = p.name) === null || _a === void 0 ? void 0 : _a.startsWith('astro:'); }))
return null;
const outDirRoot = toPosixPath(config.build.outDir);
assertPosixPath(outDirRoot);
// Mechanism to detect whether Telefunc is used with framework.
// When used with a framework then Telefunc should let the framework determine `outDir`.
// E.g. Vike and SvelteKit already set `config.build.outDir`.
if (!isOutDirRoot(outDirRoot)) {
assertConfig(config);
return null;
}
const { outDirClient, outDirServer } = declineOutDirs(outDirRoot);
if (config.build.ssr) {
return outDirServer;
}
else {
return outDirClient;
}
}
function declineOutDirs(outDirRoot) {
assertIsOutDirRoot(outDirRoot);
assertPosixPath(outDirRoot);
const outDirClient = pathJoin(outDirRoot, 'client');
const outDirServer = pathJoin(outDirRoot, 'server');
assertIsNotOutDirRoot(outDirClient);
assertIsNotOutDirRoot(outDirServer);
return { outDirClient, outDirServer };
}
function assertIsOutDirRoot(outDir) {
assert(isOutDirRoot(outDir));
}
function isOutDirRoot(outDir) {
const p = outDir.split('/').filter(Boolean);
const lastDir = p[p.length - 1];
return lastDir !== 'client' && lastDir !== 'server';
}
function assertIsNotOutDirRoot(outDir) {
assert(outDir.endsWith('/client') || outDir.endsWith('/server'));
}
function assertConfig(config) {
var _a;
const outDir = (_a = config.build) === null || _a === void 0 ? void 0 : _a.outDir;
assert(outDir);
assertIsNotOutDirRoot(outDir);
if (config.build.ssr) {
assert(outDir.endsWith('/server'));
}
else {
assert(outDir.endsWith('/client'));
}
}
function getOutDirAbsolute(config) {
let { outDir } = config.build;
// SvelteKit sets config.build.outDir to `D:\a\telefunc\packages\telefunc\examples\svelte-kit\.svelte-kit/output/server.`
outDir = toPosixPath(outDir);
if (!outDirIsAbsolutePath(outDir)) {
const { root } = config;
assertPosixPath(root);
outDir = pathJoin(root, outDir);
}
return outDir;
}
function outDirIsAbsolutePath(outDir) {
// There doesn't seem to be a better alternative to determine whether `outDir` is an absolute path
// - Very unlikely that `outDir`'s first dir matches the filesystem's first dir
return getFirstDir(outDir) === getFirstDir(process.cwd());
}
function getFirstDir(p) {
const firstDir = p.split(/\/|\\/).filter(Boolean)[0];
return firstDir;
}