UNPKG

telefunc

Version:

Remote functions. Instead of API.

37 lines (36 loc) 1.76 kB
export { transformTelefuncFileClientSideSync }; import { posix } from 'node:path'; import { assert, assertPosixPath, assertUsage, getTelefunctionKey } from '../utils.js'; import { rollupSourceMapRemove } from '../vite/utils.js'; function transformTelefuncFileClientSideSync(id, appRootDir, exportNames) { assertPosixPath(id); assertPosixPath(appRootDir); let telefuncFilePath = posix.relative(appRootDir, id); assertPosixPath(telefuncFilePath); assertUsage(!telefuncFilePath.startsWith('../'), `The telefunc file ${telefuncFilePath} needs to live inside ${appRootDir} (the client-side root directory, i.e. the root directory of Vite/Vike/Next.js/Nuxt/...)`); assert(!telefuncFilePath.startsWith('/') && !telefuncFilePath.startsWith('.')); telefuncFilePath = `/${telefuncFilePath}`; const code = getCode(exportNames, telefuncFilePath); return rollupSourceMapRemove(code); } export function getCode(exportNames, telefuncFilePath) { const lines = []; lines.push('// @ts-nocheck'); lines.push(`import { __remoteTelefunctionCall } from 'telefunc/client';`); exportNames.forEach((exportName) => { const varName = exportName === 'default' ? 'defaultExport' : exportName; lines.push(`const ${varName} = (...args) => __remoteTelefunctionCall('${telefuncFilePath}', '${exportName}', args);`); { const key = getTelefunctionKey(telefuncFilePath, exportName); lines.push(`${varName}._key = ${JSON.stringify(key)};`); } if (exportName === 'default') { lines.push(`export default ${varName};`); } else { lines.push(`export { ${varName} };`); } }); const code = lines.join('\n'); return code; }