mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
[ci] format
This commit is contained in:
parent
05adaaa2d2
commit
3a233c9ea2
3 changed files with 42 additions and 37 deletions
|
@ -179,7 +179,7 @@ export default function vercelServerless({
|
|||
if (command === 'build' && speedInsights?.enabled) {
|
||||
injectScript('page', 'import "@astrojs/vercel/speed-insights"');
|
||||
}
|
||||
|
||||
|
||||
updateConfig({
|
||||
outDir: new URL('./.vercel/output/', config.root),
|
||||
build: {
|
||||
|
@ -211,9 +211,9 @@ export default function vercelServerless({
|
|||
`\tYou can set functionPerRoute: false to prevent surpassing the limit.\n`
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
setAdapter(getAdapter({ functionPerRoute, edgeMiddleware }));
|
||||
|
||||
|
||||
_config = config;
|
||||
_buildTempFolder = config.build.server;
|
||||
_serverEntry = config.build.serverEntry;
|
||||
|
@ -246,12 +246,14 @@ export default function vercelServerless({
|
|||
}
|
||||
|
||||
const routeDefinitions: Array<{
|
||||
src: string
|
||||
dest: string
|
||||
middlewarePath?: string
|
||||
src: string;
|
||||
dest: string;
|
||||
middlewarePath?: string;
|
||||
}> = [];
|
||||
|
||||
const includeFiles = _includeFiles.map((file) => new URL(file, _config.root)).concat(extraFilesToInclude);
|
||||
const includeFiles = _includeFiles
|
||||
.map((file) => new URL(file, _config.root))
|
||||
.concat(extraFilesToInclude);
|
||||
const excludeFiles = _excludeFiles.map((file) => new URL(file, _config.root));
|
||||
|
||||
const runtime = getRuntime(process, logger);
|
||||
|
@ -328,9 +330,11 @@ export default function vercelServerless({
|
|||
? [
|
||||
{
|
||||
src: '/.*',
|
||||
dest: fourOhFourRoute.prerender ? '/404.html'
|
||||
: _middlewareEntryPoint ? MIDDLEWARE_PATH
|
||||
: NODE_PATH,
|
||||
dest: fourOhFourRoute.prerender
|
||||
? '/404.html'
|
||||
: _middlewareEntryPoint
|
||||
? MIDDLEWARE_PATH
|
||||
: NODE_PATH,
|
||||
status: 404,
|
||||
},
|
||||
]
|
||||
|
@ -362,23 +366,19 @@ export default function vercelServerless({
|
|||
type Runtime = `nodejs${string}.x`;
|
||||
|
||||
interface CreateMiddlewareFolderArgs {
|
||||
config: AstroConfig
|
||||
entry: URL
|
||||
functionName: string
|
||||
config: AstroConfig;
|
||||
entry: URL;
|
||||
functionName: string;
|
||||
}
|
||||
|
||||
async function createMiddlewareFolder({
|
||||
functionName,
|
||||
entry,
|
||||
config,
|
||||
}: CreateMiddlewareFolderArgs) {
|
||||
async function createMiddlewareFolder({ functionName, entry, config }: CreateMiddlewareFolderArgs) {
|
||||
const functionFolder = new URL(`./functions/${functionName}.func/`, config.outDir);
|
||||
|
||||
await generateEdgeMiddleware(
|
||||
entry,
|
||||
new URL(VERCEL_EDGE_MIDDLEWARE_FILE, config.srcDir),
|
||||
new URL('./middleware.mjs', functionFolder),
|
||||
)
|
||||
new URL('./middleware.mjs', functionFolder)
|
||||
);
|
||||
|
||||
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
|
||||
runtime: 'edge',
|
||||
|
@ -434,7 +434,7 @@ async function createFunctionFolder({
|
|||
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration
|
||||
await writeJson(vcConfig, {
|
||||
runtime,
|
||||
handler: handler.replaceAll("\\","/"),
|
||||
handler: handler.replaceAll('\\', '/'),
|
||||
launcherType: 'Nodejs',
|
||||
maxDuration,
|
||||
supportsResponseStreaming: true,
|
||||
|
|
|
@ -17,9 +17,12 @@ import { ASTRO_LOCALS_HEADER, ASTRO_PATH_HEADER, NODE_PATH } from './adapter.js'
|
|||
export async function generateEdgeMiddleware(
|
||||
astroMiddlewareEntryPointPath: URL,
|
||||
vercelEdgeMiddlewareHandlerPath: URL,
|
||||
outPath: URL,
|
||||
outPath: URL
|
||||
): Promise<URL> {
|
||||
const code = edgeMiddlewareTemplate(astroMiddlewareEntryPointPath, vercelEdgeMiddlewareHandlerPath);
|
||||
const code = edgeMiddlewareTemplate(
|
||||
astroMiddlewareEntryPointPath,
|
||||
vercelEdgeMiddlewareHandlerPath
|
||||
);
|
||||
// https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware
|
||||
const bundledFilePath = fileURLToPath(outPath);
|
||||
const esbuild = await import('esbuild');
|
||||
|
@ -38,18 +41,23 @@ export async function generateEdgeMiddleware(
|
|||
bundle: true,
|
||||
minify: false,
|
||||
// ensure node built-in modules are namespaced with `node:`
|
||||
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 }));
|
||||
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 }));
|
||||
},
|
||||
},
|
||||
}]
|
||||
],
|
||||
});
|
||||
return pathToFileURL(bundledFilePath);
|
||||
}
|
||||
|
||||
function edgeMiddlewareTemplate(astroMiddlewareEntryPointPath: URL, vercelEdgeMiddlewareHandlerPath: URL) {
|
||||
function edgeMiddlewareTemplate(
|
||||
astroMiddlewareEntryPointPath: URL,
|
||||
vercelEdgeMiddlewareHandlerPath: URL
|
||||
) {
|
||||
const middlewarePath = JSON.stringify(
|
||||
fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/')
|
||||
);
|
||||
|
|
|
@ -17,18 +17,15 @@ describe('Vercel edge middleware', () => {
|
|||
'../.vercel/output/functions/_middleware.func/.vc-config.json'
|
||||
);
|
||||
expect(JSON.parse(contents)).to.deep.include({
|
||||
"runtime": "edge",
|
||||
"entrypoint": "middleware.mjs"
|
||||
runtime: 'edge',
|
||||
entrypoint: 'middleware.mjs',
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('deployment config points to the middleware edge function', async () => {
|
||||
const contents = await build.readFile(
|
||||
'../.vercel/output/config.json'
|
||||
);
|
||||
const contents = await build.readFile('../.vercel/output/config.json');
|
||||
const { routes } = JSON.parse(contents);
|
||||
expect(routes.some(route => route.dest === '_middleware')).to.be.true;
|
||||
expect(routes.some((route) => route.dest === '_middleware')).to.be.true;
|
||||
});
|
||||
|
||||
// TODO: The path here seems to be inconsistent?
|
||||
|
|
Loading…
Reference in a new issue