Skip to content

Commit

Permalink
Merge pull request #121 from FirebaseExtended/apphosting_dir_fixes
Browse files Browse the repository at this point in the history
Apphosting dir fixes
  • Loading branch information
sjjj986 authored Nov 29, 2023
2 parents d404622 + 6e485df commit 68c8e99
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions packages/@apphosting/adapter-nextjs/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
import { spawnSync } from "child_process";
import { loadConfig, readRoutesManifest } from "../utils.js";

import { join } from "path";
import { join, relative, normalize } from "path";
import fsExtra from "fs-extra";
import { stringify as yamlStringify } from "yaml";

// unable to use shorthand imports on fsExtra since fsExtra is CJS
const { move, exists, writeFile, mkdirp } = fsExtra;
const { move, exists, writeFile } = fsExtra;
const cwd = process.cwd();

// Set standalone mode
process.env.NEXT_PRIVATE_STANDALONE = "true";
process.env.NEXT_PRIVATE_STANDALONE = "true";
// Opt-out sending telemetry to Vercel
process.env.NEXT_TELEMETRY_DISABLED = "1";
process.env.NEXT_TELEMETRY_DISABLED = "1";

build(cwd);

const {distDir} = await loadConfig(cwd);
const { distDir } = await loadConfig(cwd);
const manifest = await readRoutesManifest(join(cwd, distDir));

const appHostingOutputDirectory = join(cwd, ".apphosting");
const appHostingStaticDirectory = join(appHostingOutputDirectory,"_next", "static");
const appHostingStaticDirectory = join(appHostingOutputDirectory, ".next", "static");
const appHostingPublicDirectory = join(appHostingOutputDirectory, "public");
const outputBundlePath = join(appHostingOutputDirectory, "bundle.yaml");
const serverFilePath = join(appHostingOutputDirectory, "server.js");
Expand All @@ -30,41 +30,39 @@ const standaloneDirectory = join(cwd, distDir, "standalone");
const staticDirectory = join(cwd, distDir, "static");
const publicDirectory = join(cwd, "public");

await mkdirp(appHostingStaticDirectory);

// Run build command
function build(cwd: string) {
spawnSync("npm", ["run", "build"], {cwd, shell: true, stdio: "inherit"});
}
spawnSync("npm", ["run", "build"], {cwd, shell: true, stdio: "inherit"});
}

// move public directory to apphosting output public directory
const movePublicDirectory = async () => {
const publicDirectoryExists = await exists(appHostingPublicDirectory);
const publicDirectoryExists = await exists(publicDirectory);
if (!publicDirectoryExists) return;
await move(publicDirectory, appHostingPublicDirectory, { overwrite: true });
};

// generate bundle.yaml
const generateBundleYaml = async () => {
const headers = manifest.headers.map(it => ({...it, regex: undefined}));
const redirects = manifest.redirects.filter(it => !it.internal).map(it => ({...it, regex: undefined}));
const beforeFileRewrites = Array.isArray(manifest.rewrites) ? manifest.rewrites : manifest.rewrites?.beforeFiles || [];
const rewrites = beforeFileRewrites.map(it => ({...it, regex: undefined}));
await writeFile(outputBundlePath, yamlStringify({
headers,
redirects,
headers,
redirects,
rewrites,
runCommand: `node ${serverFilePath}`,
neededDirs: [appHostingOutputDirectory],
staticAssets: [appHostingStaticDirectory, appHostingPublicDirectory],
runCommand: `node ${normalize(relative(cwd, serverFilePath))}`,
neededDirs: [normalize(relative(cwd, appHostingOutputDirectory))],
staticAssets: [normalize(relative(cwd, appHostingPublicDirectory))],
}));
}

// move the standalone directory, the static directory and the public directory to apphosting output directory
// as well as generating bundle.yaml
await move(standaloneDirectory, appHostingOutputDirectory, { overwrite: true });
await Promise.all([
move(standaloneDirectory, appHostingOutputDirectory, { overwrite: true }),
move(staticDirectory, appHostingStaticDirectory, { overwrite: true }),
move(staticDirectory, appHostingStaticDirectory, { overwrite: true }),
movePublicDirectory(),
generateBundleYaml(),
]);
]);

0 comments on commit 68c8e99

Please sign in to comment.