mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-10 04:22:12 +00:00
[remix] Clean up dev symlink upon config error (#9595)
Move the try/catch block to immediately after the symlink call, so that if reading the `remix.config.js` file throws an error or if there's an error parsing the `export const config` in a route, the catch block will clean up after itself properly. I recommend reviewing with [whitespace changes hidden](https://github.com/vercel/vercel/pull/9595/files?w=1).
This commit is contained in:
@@ -30,7 +30,7 @@ import type {
|
||||
BuildResultV2Typical,
|
||||
} from '@vercel/build-utils';
|
||||
import type { BaseFunctionConfig } from '@vercel/static-config';
|
||||
import type { RemixConfig } from '@remix-run/dev/dist/config';
|
||||
import type { AppConfig, RemixConfig } from '@remix-run/dev/dist/config';
|
||||
import type { ConfigRoute } from '@remix-run/dev/dist/config/routes';
|
||||
import {
|
||||
calculateRouteConfigHash,
|
||||
@@ -111,14 +111,6 @@ export const build: BuildV2 = async ({
|
||||
await runNpmInstall(entrypointFsDirname, [], spawnOpts, meta, nodeVersion);
|
||||
}
|
||||
|
||||
const remixDevPackageJsonPath = _require.resolve(
|
||||
'@remix-run/dev/package.json',
|
||||
{ paths: [entrypointFsDirname] }
|
||||
);
|
||||
const remixVersion = JSON.parse(
|
||||
await fs.readFile(remixDevPackageJsonPath, 'utf8')
|
||||
).version;
|
||||
|
||||
// Make our version of `remix` CLI available to the project's build
|
||||
// command by creating a symlink to the copy in our node modules,
|
||||
// so that `serverBundles` works: https://github.com/remix-run/remix/pull/5479
|
||||
@@ -127,15 +119,34 @@ export const build: BuildV2 = async ({
|
||||
repoRootPath,
|
||||
'@remix-run/dev'
|
||||
);
|
||||
|
||||
const remixVersion = JSON.parse(
|
||||
await fs.readFile(join(remixRunDevPath, 'package.json'), 'utf8')
|
||||
).version;
|
||||
|
||||
let remixConfigWrapped = false;
|
||||
const remixConfigPath = findConfig(entrypointFsDirname, 'remix.config');
|
||||
const renamedRemixConfigPath = remixConfigPath
|
||||
? `${remixConfigPath}.original${extname(remixConfigPath)}`
|
||||
: undefined;
|
||||
|
||||
const backupRemixRunDevPath = `${remixRunDevPath}.__vercel_backup`;
|
||||
await fs.rename(remixRunDevPath, backupRemixRunDevPath);
|
||||
await fs.symlink(REMIX_RUN_DEV_PATH, remixRunDevPath);
|
||||
|
||||
// These get populated inside the try/catch below
|
||||
let serverBundles: AppConfig['serverBundles'];
|
||||
let remixConfig: RemixConfig;
|
||||
let remixRoutes: ConfigRoute[];
|
||||
const serverBundlesMap = new Map<string, ConfigRoute[]>();
|
||||
const resolvedConfigsMap = new Map<ConfigRoute, ResolvedRouteConfig>();
|
||||
|
||||
try {
|
||||
// Make `remix build` output production mode
|
||||
spawnOpts.env.NODE_ENV = 'production';
|
||||
|
||||
const remixConfig = await chdirAndReadConfig(entrypointFsDirname);
|
||||
const remixRoutes = Object.values(remixConfig.routes);
|
||||
remixConfig = await chdirAndReadConfig(entrypointFsDirname);
|
||||
remixRoutes = Object.values(remixConfig.routes);
|
||||
|
||||
// Read the `export const config` (if any) for each route
|
||||
const project = new Project();
|
||||
@@ -146,7 +157,6 @@ export const build: BuildV2 = async ({
|
||||
staticConfigsMap.set(route, staticConfig);
|
||||
}
|
||||
|
||||
const resolvedConfigsMap = new Map<ConfigRoute, ResolvedRouteConfig>();
|
||||
for (const route of remixRoutes) {
|
||||
const config = getResolvedRouteConfig(
|
||||
route,
|
||||
@@ -158,7 +168,6 @@ export const build: BuildV2 = async ({
|
||||
|
||||
// Figure out which routes belong to which server bundles
|
||||
// based on having common static config properties
|
||||
const serverBundlesMap = new Map<string, ConfigRoute[]>();
|
||||
for (const route of remixRoutes) {
|
||||
if (isLayoutRoute(route.id, remixRoutes)) continue;
|
||||
|
||||
@@ -177,7 +186,7 @@ export const build: BuildV2 = async ({
|
||||
routesForHash.push(route);
|
||||
}
|
||||
|
||||
const serverBundles = Array.from(serverBundlesMap.entries()).map(
|
||||
serverBundles = Array.from(serverBundlesMap.entries()).map(
|
||||
([hash, routes]) => {
|
||||
const runtime = resolvedConfigsMap.get(routes[0])?.runtime ?? 'nodejs';
|
||||
return {
|
||||
@@ -189,10 +198,6 @@ export const build: BuildV2 = async ({
|
||||
|
||||
// We need to patch the `remix.config.js` file to force some values necessary
|
||||
// for a build that works on either Node.js or the Edge runtime
|
||||
const remixConfigPath = findConfig(entrypointFsDirname, 'remix.config');
|
||||
const renamedRemixConfigPath = remixConfigPath
|
||||
? `${remixConfigPath}.original${extname(remixConfigPath)}`
|
||||
: undefined;
|
||||
if (remixConfigPath && renamedRemixConfigPath) {
|
||||
await fs.rename(remixConfigPath, renamedRemixConfigPath);
|
||||
|
||||
@@ -227,10 +232,10 @@ config.serverBundles = ${JSON.stringify(serverBundles)};
|
||||
module.exports = config;`;
|
||||
}
|
||||
await fs.writeFile(remixConfigPath, patchedConfig);
|
||||
remixConfigWrapped = true;
|
||||
}
|
||||
|
||||
// Run "Build Command"
|
||||
try {
|
||||
if (buildCommand) {
|
||||
debug(`Executing build command "${buildCommand}"`);
|
||||
await execCommand(buildCommand, {
|
||||
@@ -260,7 +265,7 @@ module.exports = config;`;
|
||||
}
|
||||
} finally {
|
||||
// Clean up our patched `remix.config.js` to be polite
|
||||
if (remixConfigPath && renamedRemixConfigPath) {
|
||||
if (remixConfigWrapped && remixConfigPath && renamedRemixConfigPath) {
|
||||
await fs.rename(renamedRemixConfigPath, remixConfigPath);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user