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

fix(vercel): edge middleware #9585

Merged
merged 12 commits into from
Jan 22, 2024
Prev Previous commit
Next Next commit
handle node built-in modules
  • Loading branch information
lilnasy committed Jan 17, 2024
commit b014bb194037b31b4fb66d1d91d10c62d12d4d02
13 changes: 5 additions & 8 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,18 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
`\tYour project will use Node.js 18 as the runtime instead.\n` +
`\tConsider switching your local version to 18.\n`
);
return 'nodejs18.x';
}
if (support.status === 'current') {
return `nodejs${major}.x`;
} else if (support?.status === 'beta') {
}
if (support.status === 'beta') {
logger.warn(
`Your project is being built for Node.js ${major} as the runtime, which is currently in beta for Vercel Serverless Functions.`
);
return `nodejs${major}.x`;
} else if (support.status === 'deprecated') {
}
if (support.status === 'deprecated') {
const removeDate = new Intl.DateTimeFormat(undefined, { dateStyle: 'long' }).format(
support.removal
);
Expand All @@ -458,11 +461,5 @@ function getRuntime(process: NodeJS.Process, logger: AstroIntegrationLogger): Ru
);
return `nodejs${major}.x`;
}
logger.warn(
`\n` +
`\tThe local Node.js version (${major}) is not supported by Vercel Serverless Functions.\n` +
`\tYour project will use Node.js 18 as the runtime instead.\n` +
`\tConsider switching your local version to 18.\n`
);
return 'nodejs18.x';
}
9 changes: 8 additions & 1 deletion packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { ASTRO_LOCALS_HEADER } from './adapter.js';
import { builtinModules } from 'node:module';

/**
* It generates the Vercel Edge Middleware file.
Expand Down Expand Up @@ -36,6 +36,13 @@ export async function generateEdgeMiddleware(
format: 'esm',
bundle: true,
minify: false,
plugins: [{
name: 'esbuild-namespace-node-built-in-modules',
setup(build) {
const filter = new RegExp(builtinModules.map((mod) => `(^${mod}$)`).join('|'));
build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true }));
},
}]
ematipico marked this conversation as resolved.
Show resolved Hide resolved
});
return pathToFileURL(bundledFilePath);
}
Expand Down