Skip to content

Commit

Permalink
fix: eagerl try Vite telefunc file retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Nov 2, 2024
1 parent 9e3d9c2 commit 2994e90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
25 changes: 20 additions & 5 deletions telefunc/node/server/runTelefunc/loadTelefuncFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 14 additions & 5 deletions telefunc/node/vite/loadTelefuncFilesWithVite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null | {
telefuncFilesLoaded: Record<string, Record<string, unknown>>
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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 2994e90

Please sign in to comment.