mirror of
https://github.com/enricoros/big-AGI.git
synced 2026-05-10 21:50:14 -07:00
f767ad81ce
This introduces a pre-build step on Next Build, which hides the files in the app/api directory when the EXPORT_FRONTEND environment variable is true-ish. Hopefully there won't be disruption due to the post-processing step. Also check https://github.com/vercel/next.js/issues/61213 for upstream updates.
48 lines
1.5 KiB
JavaScript
48 lines
1.5 KiB
JavaScript
import { default as fs, renameSync } from 'fs';
|
|
import { dirname as pathDirName, join as pathJoin } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
|
// build-time configuration
|
|
const buildOnlyFrontend = !!process.env.EXPORT_FRONTEND;
|
|
|
|
|
|
function getApiDirName() {
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = pathDirName(__filename);
|
|
return pathJoin(__dirname, '../app/api');
|
|
}
|
|
|
|
function findAllFiles(startDir) {
|
|
return fs.readdirSync(startDir).flatMap((file) => {
|
|
const fullPath = pathJoin(startDir, file);
|
|
if (fs.statSync(fullPath).isDirectory())
|
|
return findAllFiles(fullPath);
|
|
return fullPath;
|
|
},
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Hide/show API routes depending on the build type
|
|
* Due to an upstream bug, NextJS will not ignore the nodejs API routes and choose to abort instead.
|
|
*/
|
|
function prebuildFrontendHotFixes(hideFiles) {
|
|
const apiDirName = getApiDirName();
|
|
const apiRoutesPaths = findAllFiles(apiDirName)
|
|
.filter((path) => path.endsWith('.ts') || path.endsWith('.ts.backup'));
|
|
|
|
apiRoutesPaths.forEach((path) => {
|
|
const isBackup = path.endsWith('.backup');
|
|
if (hideFiles) {
|
|
// If building the frontend, rename (effectively hide) the file
|
|
!isBackup && renameSync(path, `${path}.backup`);
|
|
} else {
|
|
// If it's a normal build and including API routes and a backup exists, restore it
|
|
isBackup && renameSync(path, path.slice(0, -7));
|
|
}
|
|
});
|
|
}
|
|
|
|
prebuildFrontendHotFixes(buildOnlyFrontend);
|