diff --git a/telefunc/node/server/runTelefunc/loadTelefuncFiles.ts b/telefunc/node/server/runTelefunc/loadTelefuncFiles.ts index 2f79fc10..81d2aa8c 100644 --- a/telefunc/node/server/runTelefunc/loadTelefuncFiles.ts +++ b/telefunc/node/server/runTelefunc/loadTelefuncFiles.ts @@ -37,13 +37,28 @@ async function loadTelefuncFiles(runContext: { // Vite: // - In development, `.telefunc.js` files provided with Vite's development server // - In production, `.telefunc.js` files provided with @brillout/vite-plugin-server-entry - if (!isWebpack() || isVikeApp()) { - const { telefuncFilesLoaded, viteProvider, telefuncFilesAll } = await loadTelefuncFilesWithVite(runContext) - assertUsage(Object.keys(telefuncFilesAll).length > 0, getNothingFoundErr(viteProvider)) - return { telefuncFilesLoaded, telefuncFilesAll } + { + const res = await loadTelefuncFilesWithVite(runContext) + if (res) { + const { telefuncFilesLoaded, viteProvider, telefuncFilesAll } = res + assertUsage(Object.keys(telefuncFilesAll).length > 0, getNothingFoundErr(viteProvider)) + return { telefuncFilesLoaded, telefuncFilesAll } + } else if (isVikeApp() || !isWebpack()) { + // Show [manual import error](https://github.com/brillout/vite-plugin-server-entry#manual-import): + // ``` + // [@brillout/vite-plugin-server-entry][Wrong Usage] The server production entry is missing. + // (Re-)build your app and try again. If you still get this error, then you need to manually + // import the server production entry. + // ``` + // + const res2 = loadTelefuncFilesWithVite(runContext, true) + assert(res2 === null) + assert(false) // loadTelefuncFilesWithVite() should have called assertUsage() + } } - assertUsage(false, "You don't seem to be using Telefunc with a supported stack. Reach out on GitHub.") + // No retrieval method found + assertUsage(false, `Couldn't find method for retrieving ${pc.cyan('.telefunc.js')} files. Is your stack supported?`) } function getNothingFoundErr(retrievalMethod: string) { diff --git a/telefunc/node/vite/loadTelefuncFilesWithVite.ts b/telefunc/node/vite/loadTelefuncFilesWithVite.ts index 7a26ad51..8bb5a3e3 100644 --- a/telefunc/node/vite/loadTelefuncFilesWithVite.ts +++ b/telefunc/node/vite/loadTelefuncFilesWithVite.ts @@ -6,12 +6,17 @@ import { telefuncFilesGlobFilePath } from './importGlob/telefuncFilesGlobPath' import { loadTelefuncFilesWithImportBuild } from './plugins/importBuild/loadBuild' import { getViteDevServer } from '../server/globalContext' -async function loadTelefuncFilesWithVite(runContext: { telefuncFilePath: string }): Promise<{ +async function loadTelefuncFilesWithVite( + runContext: { telefuncFilePath: string }, + failOnFailure?: true, +): Promise> telefuncFilesAll: string[] viteProvider: 'Vite' | '@brillout/vite-plugin-server-entry' }> { - const { moduleExports, viteProvider } = await loadGlobImporter() + const res = await loadGlobImporter(failOnFailure) + if (!res) return null + const { moduleExports, viteProvider } = res assert(isObject(moduleExports), { moduleExports, viteProvider }) assert(hasProp(moduleExports, 'telefuncFilesGlob'), { moduleExports, viteProvider }) const telefuncFilesGlob = moduleExports.telefuncFilesGlob as GlobFiles @@ -20,7 +25,7 @@ async function loadTelefuncFilesWithVite(runContext: { telefuncFilePath: string return { telefuncFilesLoaded, viteProvider, telefuncFilesAll } } -async function loadGlobImporter() { +async function loadGlobImporter(failOnFailure?: true) { const viteDevServer = getViteDevServer() if (viteDevServer) { const devPath = telefuncFilesGlobFilePath @@ -36,9 +41,13 @@ async function loadGlobImporter() { let moduleExports: unknown moduleExports = await loadTelefuncFilesWithImportBuild() if (moduleExports === null) { - await importServerProductionEntry() + await importServerProductionEntry({ tolerateNotFound: !failOnFailure }) moduleExports = await loadTelefuncFilesWithImportBuild() - assert(moduleExports) + if (failOnFailure) { + assert(moduleExports) + } else { + return null + } } else { assert(moduleExports) }