UNPKG

telefunc

Version:

Remote functions. Instead of API.

72 lines (71 loc) 3.83 kB
export { isViteServerSide }; export { isViteClientSide }; export { isViteServerSide_withoutEnv }; export { isViteServerSide_onlySsrEnv }; export { isViteServerSide_extraSafe }; import { assert } from '../../../utils/assert.js'; function isViteServerSide_withoutEnv(configGlobal, viteEnv) { var _a, _b, _c, _d, _e, _f, _g, _h; assert(!('consumer' in configGlobal)); // make sure configGlobal isn't viteEnv.config const isServerSide1 = !(viteEnv === null || viteEnv === void 0 ? void 0 : viteEnv.config.consumer) ? null : viteEnv.config.consumer !== 'client'; const isServerSide2 = !(viteEnv === null || viteEnv === void 0 ? void 0 : viteEnv.name) ? null : viteEnv.name !== 'client'; // I can't think of a use case for creating another client-side environment const isServerSide3 = !viteEnv ? null : !!((_a = viteEnv.config.build) === null || _a === void 0 ? void 0 : _a.ssr); const isServerSide4 = !!((_b = configGlobal.build) === null || _b === void 0 ? void 0 : _b.ssr); const debug = { envIsUndefined: !viteEnv, envName: (_c = viteEnv === null || viteEnv === void 0 ? void 0 : viteEnv.name) !== null && _c !== void 0 ? _c : null, envConsumer: (_d = viteEnv === null || viteEnv === void 0 ? void 0 : viteEnv.config.consumer) !== null && _d !== void 0 ? _d : null, configEnvBuildSsr: (_f = (_e = viteEnv === null || viteEnv === void 0 ? void 0 : viteEnv.config.build) === null || _e === void 0 ? void 0 : _e.ssr) !== null && _f !== void 0 ? _f : null, configGlobalBuildSsr: (_h = (_g = configGlobal.build) === null || _g === void 0 ? void 0 : _g.ssr) !== null && _h !== void 0 ? _h : null, isServerSide1, isServerSide2, isServerSide3, isServerSide4, }; if (isServerSide1 !== null) { assert(isServerSide1 === isServerSide2 || isServerSide2 === null, debug); /* This assertion can fail, seems to be a Vite bug? assert(isServerSide1 === isServerSide3, debug) */ return isServerSide1; } if (isServerSide2 !== null) { /* This assertion can fail, seems to be a Vite bug? assert(isServerSide2 === isServerSide3, debug) */ return isServerSide2; } if (isServerSide3 !== null) { return isServerSide3; } return isServerSide4; } function isViteServerSide(configGlobal, viteEnv) { return isViteServerSide_withoutEnv(configGlobal, viteEnv); } function isViteClientSide(configGlobal, viteEnv) { return !isViteServerSide(configGlobal, viteEnv); } // Only `ssr` env: for example don't include `vercel_edge` nor `vercel_node`. function isViteServerSide_onlySsrEnv(configGlobal, viteEnv) { return viteEnv.name ? viteEnv.name === 'ssr' : isViteServerSide(configGlobal, viteEnv); } // Vite is quite messy about setting config.build.ssr — for security purposes, we use an extra safe implementation with lots of assertions, which is needed for the .client.js and .server.js guarantee. function isViteServerSide_extraSafe(config, options, viteEnv) { if (config.command === 'build') { const res = config.build.ssr; assert(typeof res === 'boolean'); assert(res === (options === null || options === void 0 ? void 0 : options.ssr) || (options === null || options === void 0 ? void 0 : options.ssr) === undefined); assert(res === isViteServerSide(config, viteEnv)); return res; } else { const res = options === null || options === void 0 ? void 0 : options.ssr; assert(typeof res === 'boolean'); /* This assertion can fail, seems to be a Vite bug? It's very unexpected. if (typeof config.build.ssr === 'boolean') assert(res === config.build.ssr) */ assert(res === isViteServerSide(config, viteEnv)); return res; } }