From 5388c753c3a8b81ec41cfd15064275facf70643f Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 17 Jul 2023 16:32:22 +0800 Subject: [PATCH 1/2] Fix vercel build error when passing `includeFiles` --- .changeset/six-baboons-allow.md | 5 +++++ packages/integrations/vercel/src/serverless/adapter.ts | 10 ++++++---- .../fixtures/serverless-prerender/astro.config.mjs | 5 ++++- 3 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 .changeset/six-baboons-allow.md diff --git a/.changeset/six-baboons-allow.md b/.changeset/six-baboons-allow.md new file mode 100644 index 000000000000..e55e044e3dfd --- /dev/null +++ b/.changeset/six-baboons-allow.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Fix build error when passing `includeFiles` diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index e44aa3f0796d..592f3618ff6d 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -47,6 +47,8 @@ export default function vercelServerless({ let buildTempFolder: URL; let serverEntry: string; let _entryPoints: Map; + // Extra files to be merged with `includeFiles` during build + const extraFilesToInclude: URL[] = []; async function createFunctionFolder(funcName: string, entry: URL, inc: URL[]) { const functionFolder = new URL(`./functions/${funcName}.func/`, _config.outDir); @@ -74,8 +76,6 @@ export default function vercelServerless({ }); } - const filesToInclude = includeFiles?.map((file) => new URL(file, _config.root)) || []; - return { name: PACKAGE_NAME, hooks: { @@ -130,7 +130,7 @@ export default function vercelServerless({ vercelEdgeMiddlewareHandlerPath ); // let's tell the adapter that we need to save this file - filesToInclude.push(bundledMiddlewarePath); + extraFilesToInclude.push(bundledMiddlewarePath); } }, @@ -140,7 +140,7 @@ export default function vercelServerless({ const mergeGlobbedIncludes = (globPattern: unknown) => { if (typeof globPattern === 'string') { const entries = glob.sync(globPattern).map((p) => pathToFileURL(p)); - filesToInclude.push(...entries); + extraFilesToInclude.push(...entries); } else if (Array.isArray(globPattern)) { for (const pattern of globPattern) { mergeGlobbedIncludes(pattern); @@ -152,6 +152,8 @@ export default function vercelServerless({ } const routeDefinitions: { src: string; dest: string }[] = []; + const filesToInclude = includeFiles?.map((file) => new URL(file, _config.root)) || []; + filesToInclude.push(...extraFilesToInclude); // Multiple entrypoint support if (_entryPoints.size) { diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs index 03228c5df506..46ba695d6403 100644 --- a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs @@ -2,6 +2,9 @@ import { defineConfig } from 'astro/config'; import vercel from '@astrojs/vercel/serverless'; export default defineConfig({ - adapter: vercel(), + adapter: vercel({ + // Pass some value to make sure it doesn't error out + includeFiles: ['package.json'] + }), output: 'server' }); From c74450521842870b12af49b8d3da708a67a7bcb7 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 17 Jul 2023 20:46:49 +0800 Subject: [PATCH 2/2] Add test for includeFiles --- .../fixtures/serverless-prerender/astro.config.mjs | 2 +- .../test/fixtures/serverless-prerender/included.js | 1 + .../vercel/test/serverless-prerender.test.js | 12 ++++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 packages/integrations/vercel/test/fixtures/serverless-prerender/included.js diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs index 46ba695d6403..84b96c27d409 100644 --- a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs @@ -4,7 +4,7 @@ import vercel from '@astrojs/vercel/serverless'; export default defineConfig({ adapter: vercel({ // Pass some value to make sure it doesn't error out - includeFiles: ['package.json'] + includeFiles: ['included.js'] }), output: 'server' }); diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/included.js b/packages/integrations/vercel/test/fixtures/serverless-prerender/included.js new file mode 100644 index 000000000000..4e64b2d616bf --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/included.js @@ -0,0 +1 @@ +'works' \ No newline at end of file diff --git a/packages/integrations/vercel/test/serverless-prerender.test.js b/packages/integrations/vercel/test/serverless-prerender.test.js index 18b16534047a..292e92b69d30 100644 --- a/packages/integrations/vercel/test/serverless-prerender.test.js +++ b/packages/integrations/vercel/test/serverless-prerender.test.js @@ -10,12 +10,20 @@ describe('Serverless prerender', () => { fixture = await loadFixture({ root: './fixtures/serverless-prerender/', }); + await fixture.build(); }); it('build successful', async () => { - await fixture.build(); expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok; }); + + it('includeFiles work', async () => { + expect( + await fixture.readFile( + '../.vercel/output/functions/render.func/packages/integrations/vercel/test/fixtures/serverless-prerender/included.js' + ) + ).to.be.ok; + }); }); describe('Serverless hybrid rendering', () => { @@ -28,10 +36,10 @@ describe('Serverless hybrid rendering', () => { root: './fixtures/serverless-prerender/', output: 'hybrid', }); + await fixture.build(); }); it('build successful', async () => { - await fixture.build(); expect(await fixture.readFile('../.vercel/output/static/index.html')).to.be.ok; }); });