Use .mjs extension when building for netlify edge and vercel serverless (#6317)

This commit is contained in:
Bjorn Lu 2023-02-21 22:14:47 +08:00 committed by GitHub
parent 2a6d09e5d2
commit 2eb73cb9d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 119 additions and 3 deletions

View file

@ -0,0 +1,6 @@
---
'@astrojs/netlify': patch
'@astrojs/vercel': patch
---
Use .mjs extension when building to support CJS environments

View file

@ -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": {

View file

@ -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',
},
});
},

View file

@ -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',
})

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-netlify-prerender",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/netlify": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,12 @@
---
export const prerender = true
---
<html>
<head>
<title>testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>

View file

@ -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();
},
});

View file

@ -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),
},

View file

@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';
export default defineConfig({
adapter: vercel(),
output: 'server'
});

View file

@ -0,0 +1,9 @@
{
"name": "@test/astro-vercel-serverless-prerender",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/vercel": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,12 @@
---
export const prerender = true
---
<html>
<head>
<title>testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>

View file

@ -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;
});
});

View file

@ -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