Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy authored and astrobot-houston committed Jan 22, 2024
1 parent 05adaaa commit 3a233c9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
42 changes: 21 additions & 21 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
},
]
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 17 additions & 9 deletions packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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, '/')
);
Expand Down
11 changes: 4 additions & 7 deletions packages/integrations/vercel/test/edge-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down

0 comments on commit 3a233c9

Please sign in to comment.