Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update adaptor build script #119

Merged
merged 6 commits into from
Nov 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions packages/@apphosting/adapter-nextjs/src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,58 @@
import { spawnSync } from "child_process";
import { loadConfig, readRoutesManifest } from "../utils.js";

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

const { move, exists, writeFile, mkdirp } = fsExtra;
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const cwd = process.cwd();

spawnSync("next", ["build"], { cwd, stdio: "inherit", env: { ...process.env, NEXT_PRIVATE_STANDALONE: 'true' } });
// Set standalone mode
process.env.NEXT_PRIVATE_STANDALONE = "true";
// Opt-out sending telemetry to Vercel
process.env.NEXT_TELEMETRY_DISABLED = "1";

build(cwd);

const {distDir, basePath} = await loadConfig(cwd);
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const manifest = await readRoutesManifest(join(cwd, distDir));

const destination = join(cwd, ".apphosting");
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const staticDestination = join(destination,"_next", "static");
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const publicDestination = join(destination, "public");
const outputBundleDestination = join(destination, "bundle.yaml");

const standaloneDir = join (cwd, distDir, "standalone");
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const staticDir = join (cwd, distDir, "static");
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const publicDir = join(cwd, "public");

await mkdirp(staticDestination);

// Run build command
function build(cwd: string) {
spawnSync("npm run", ["build"], {cwd, stdio: "inherit"});
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
}

const config = await loadConfig(cwd);
const routeManifest = await readRoutesManifest(config.distDir);
// move public directory to both public and CDN destination
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
const movePublicDir = async () => {
const publicDirExists = await exists(publicDir);
if (!publicDirExists) return;
await move(publicDir, publicDestination);
};

// 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, regec: undefined}));
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
await writeFile(outputBundleDestination, yamlStringify({headers, redirects, rewrites}))
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO do all the things
console.log({ config, routeManifest });
await Promise.all([
move(standaloneDir, destination),
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
move(staticDir, staticDestination),
sjjj986 marked this conversation as resolved.
Show resolved Hide resolved
movePublicDir(),
generateBundleYaml(),
]);