telefunc
Version:
Remote functions. Instead of API.
71 lines (70 loc) • 3.46 kB
JavaScript
export { loadTelefuncFilesUsingVite };
import { importServerProductionEntry } from '@brillout/vite-plugin-server-entry/runtime';
import { assert, assertWarning, getNodeEnv, hasProp, isObject, isProduction, isTelefuncFilePath } from '../utils.js';
import { loadTelefuncFilesWithImportBuild } from './plugins/importBuild/loadBuild.js';
import { getViteDevServer } from '../server/globalContext.js';
async function loadTelefuncFilesUsingVite(runContext, failOnFailure) {
const res = await loadGlobEntryFile(failOnFailure);
if (!res)
return null;
const { moduleExports, viteProvider } = res;
assert(isObject(moduleExports), { moduleExports, viteProvider });
assert(hasProp(moduleExports, 'telefuncFilesGlob'), { moduleExports, viteProvider });
const telefuncFilesGlob = moduleExports.telefuncFilesGlob;
const { telefuncFilesLoaded, telefuncFilesAll } = await loadViteGlobbedFiles(telefuncFilesGlob, runContext);
assert(isObjectOfObjects(telefuncFilesLoaded));
return { telefuncFilesLoaded, viteProvider, telefuncFilesAll };
}
async function loadGlobEntryFile(failOnFailure) {
var _a;
const viteDevServer = getViteDevServer();
if (viteDevServer) {
const devPath = (_a = globalThis._telefunc) === null || _a === void 0 ? void 0 : _a.telefuncFilesGlobFilePath;
assert(devPath);
const moduleExports = await viteDevServer.ssrLoadModule(devPath, { fixStacktrace: true });
return { moduleExports, viteProvider: 'Vite' };
}
else {
let moduleExports;
moduleExports = await loadTelefuncFilesWithImportBuild();
if (moduleExports === null) {
const tolerateDoesNotExist = !failOnFailure;
const success = await importServerProductionEntry({ tolerateDoesNotExist });
moduleExports = await loadTelefuncFilesWithImportBuild();
if (success === false) {
assert(tolerateDoesNotExist);
assert(!moduleExports);
return null;
}
assert(moduleExports);
}
else {
assert(moduleExports);
}
assertProd();
return { moduleExports, viteProvider: '@brillout/vite-plugin-server-entry' };
}
}
function assertProd() {
if (!isProduction()) {
const env = getNodeEnv();
assert(env === undefined || env === 'development' || env === '');
assertWarning(false, `This seems to be a production environment yet process.env.NODE_ENV is ${JSON.stringify(env)}. Set it to a different value such as "production" or "staging".`, { onlyOnce: true });
}
}
function isObjectOfObjects(obj) {
return isObject(obj) && Object.values(obj).every(isObject);
}
// Vite's import.meta.glob() returns promises
async function loadViteGlobbedFiles(telefuncFilesGlob, runContext) {
const telefuncFilesAll = Object.keys(telefuncFilesGlob);
const telefuncFilesLoaded = Object.fromEntries(await Promise.all(Object.entries(telefuncFilesGlob)
.filter(([telefuncFilePath]) => {
assert(isTelefuncFilePath(telefuncFilePath));
assert(isTelefuncFilePath(runContext.telefuncFilePath));
return telefuncFilePath === runContext.telefuncFilePath;
})
.map(async ([telefuncFilePath, loadModuleExports]) => [telefuncFilePath, await loadModuleExports()])));
assert(Object.keys(telefuncFilesLoaded).length <= 1);
return { telefuncFilesAll, telefuncFilesLoaded };
}