From 2eb73cb9d1c982df5f8788ddacd634645643c5c6 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 21 Feb 2023 22:14:47 +0800 Subject: [PATCH] Use .mjs extension when building for netlify edge and vercel serverless (#6317) --- .changeset/eighty-rockets-crash.md | 6 ++++++ packages/integrations/netlify/package.json | 2 +- .../netlify/src/integration-edge-functions.ts | 2 +- .../fixtures/prerender/astro.config.mjs | 9 +++++++++ .../fixtures/prerender/package.json | 9 +++++++++ .../fixtures/prerender/src/pages/index.astro | 12 ++++++++++++ .../test/edge-functions/prerender.test.ts | 18 ++++++++++++++++++ .../vercel/src/serverless/adapter.ts | 2 +- .../serverless-prerender/astro.config.mjs | 7 +++++++ .../fixtures/serverless-prerender/package.json | 9 +++++++++ .../serverless-prerender/src/pages/index.astro | 12 ++++++++++++ .../vercel/test/serverless-prerender.test.js | 18 ++++++++++++++++++ pnpm-lock.yaml | 16 ++++++++++++++++ 13 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 .changeset/eighty-rockets-crash.md create mode 100644 packages/integrations/netlify/test/edge-functions/fixtures/prerender/astro.config.mjs create mode 100644 packages/integrations/netlify/test/edge-functions/fixtures/prerender/package.json create mode 100644 packages/integrations/netlify/test/edge-functions/fixtures/prerender/src/pages/index.astro create mode 100644 packages/integrations/netlify/test/edge-functions/prerender.test.ts create mode 100644 packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs create mode 100644 packages/integrations/vercel/test/fixtures/serverless-prerender/package.json create mode 100644 packages/integrations/vercel/test/fixtures/serverless-prerender/src/pages/index.astro create mode 100644 packages/integrations/vercel/test/serverless-prerender.test.js diff --git a/.changeset/eighty-rockets-crash.md b/.changeset/eighty-rockets-crash.md new file mode 100644 index 000000000..4dda80c77 --- /dev/null +++ b/.changeset/eighty-rockets-crash.md @@ -0,0 +1,6 @@ +--- +'@astrojs/netlify': patch +'@astrojs/vercel': patch +--- + +Use .mjs extension when building to support CJS environments diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index 8e87c40c3..dd780608a 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -30,7 +30,7 @@ "build:ci": "astro-scripts build \"src/**/*.ts\"", "dev": "astro-scripts dev \"src/**/*.ts\"", "test-fn": "mocha --exit --timeout 20000 test/functions/", - "test-edge": "deno test --allow-run --allow-read --allow-net ./test/edge-functions/", + "test-edge": "deno test --allow-run --allow-read --allow-net --allow-env ./test/edge-functions/", "test": "npm run test-fn" }, "dependencies": { diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 9cb2d663d..495501f68 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -122,7 +122,7 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) build: { client: outDir, server: new URL('./.netlify/edge-functions/', config.root), - serverEntry: 'entry.js', + serverEntry: 'entry.mjs', }, }); }, diff --git a/packages/integrations/netlify/test/edge-functions/fixtures/prerender/astro.config.mjs b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/astro.config.mjs new file mode 100644 index 000000000..cd758352b --- /dev/null +++ b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/astro.config.mjs @@ -0,0 +1,9 @@ +import { defineConfig } from 'astro/config'; +import { netlifyEdgeFunctions } from '@astrojs/netlify'; + +export default defineConfig({ + adapter: netlifyEdgeFunctions({ + dist: new URL('./dist/', import.meta.url), + }), + output: 'server', +}) diff --git a/packages/integrations/netlify/test/edge-functions/fixtures/prerender/package.json b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/package.json new file mode 100644 index 000000000..a080cc213 --- /dev/null +++ b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-netlify-prerender", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/netlify": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/netlify/test/edge-functions/fixtures/prerender/src/pages/index.astro b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/src/pages/index.astro new file mode 100644 index 000000000..075253550 --- /dev/null +++ b/packages/integrations/netlify/test/edge-functions/fixtures/prerender/src/pages/index.astro @@ -0,0 +1,12 @@ +--- +export const prerender = true +--- + + + + testing + + +

testing

+ + diff --git a/packages/integrations/netlify/test/edge-functions/prerender.test.ts b/packages/integrations/netlify/test/edge-functions/prerender.test.ts new file mode 100644 index 000000000..4237e321a --- /dev/null +++ b/packages/integrations/netlify/test/edge-functions/prerender.test.ts @@ -0,0 +1,18 @@ +// @ts-ignore +import { runBuild } from './test-utils.ts'; +// @ts-ignore +import { assertEquals } from './deps.ts'; + +// @ts-ignore +Deno.test({ + name: 'Prerender', + async fn() { + let close = await runBuild('./fixtures/prerender/'); + const { default: handler } = await import( + './fixtures/prerender/.netlify/edge-functions/entry.mjs' + ); + const response = await handler(new Request('http://example.com/index.html')); + assertEquals(response, undefined, 'No response because this is an asset'); + await close(); + }, +}); \ No newline at end of file diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 8a9553eca..24b9c735b 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -43,7 +43,7 @@ export default function vercelServerless({ updateConfig({ outDir, build: { - serverEntry: 'entry.js', + serverEntry: 'entry.mjs', client: new URL('./static/', outDir), server: new URL('./dist/', config.root), }, diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs new file mode 100644 index 000000000..03228c5df --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/astro.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from 'astro/config'; +import vercel from '@astrojs/vercel/serverless'; + +export default defineConfig({ + adapter: vercel(), + output: 'server' +}); diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/package.json b/packages/integrations/vercel/test/fixtures/serverless-prerender/package.json new file mode 100644 index 000000000..c51362516 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/astro-vercel-serverless-prerender", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/vercel": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/vercel/test/fixtures/serverless-prerender/src/pages/index.astro b/packages/integrations/vercel/test/fixtures/serverless-prerender/src/pages/index.astro new file mode 100644 index 000000000..075253550 --- /dev/null +++ b/packages/integrations/vercel/test/fixtures/serverless-prerender/src/pages/index.astro @@ -0,0 +1,12 @@ +--- +export const prerender = true +--- + + + + testing + + +

testing

+ + diff --git a/packages/integrations/vercel/test/serverless-prerender.test.js b/packages/integrations/vercel/test/serverless-prerender.test.js new file mode 100644 index 000000000..4cada43a7 --- /dev/null +++ b/packages/integrations/vercel/test/serverless-prerender.test.js @@ -0,0 +1,18 @@ +import { loadFixture } from './test-utils.js'; +import { expect } from 'chai'; + +describe('Serverless prerender', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/serverless-prerender/', + }); + }); + + it('build successful', async () => { + await fixture.build(); + expect(fixture.readFile('/static/index.html')).to.be.ok; + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 85b30aabc..a53c5b009 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3096,6 +3096,14 @@ importers: react: 18.2.0 react-dom: 18.2.0_react@18.2.0 + packages/integrations/netlify/test/edge-functions/fixtures/prerender: + specifiers: + '@astrojs/netlify': workspace:* + astro: workspace:* + dependencies: + '@astrojs/netlify': link:../../../.. + astro: link:../../../../../../astro + packages/integrations/netlify/test/edge-functions/fixtures/root-dynamic: specifiers: '@astrojs/netlify': workspace:* @@ -3399,6 +3407,14 @@ importers: '@astrojs/vercel': link:../../.. astro: link:../../../../../astro + packages/integrations/vercel/test/fixtures/serverless-prerender: + specifiers: + '@astrojs/vercel': workspace:* + astro: workspace:* + dependencies: + '@astrojs/vercel': link:../../.. + astro: link:../../../../../astro + packages/integrations/vue: specifiers: '@types/chai': ^4.3.3