fix(netlify): bundle netlify functions as ESM to support top-level await (#8661)

This commit is contained in:
Simon Knott 2023-09-25 15:45:13 +02:00 committed by GitHub
parent 954cadc1e5
commit 008f7647c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---
fix build failures because of CJS builds and top-level await

View file

@ -1,5 +1,6 @@
import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro'; import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro';
import { extname } from 'node:path'; import { extname, join } from 'node:path';
import { writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { generateEdgeMiddleware } from './middleware.js'; import { generateEdgeMiddleware } from './middleware.js';
import type { Args } from './netlify-functions.js'; import type { Args } from './netlify-functions.js';
@ -85,6 +86,15 @@ function netlifyFunctions({
} }
}, },
'astro:build:done': async ({ routes, dir }) => { 'astro:build:done': async ({ routes, dir }) => {
const functionsConfig = {
version: 1,
config: {
nodeModuleFormat: "esm"
}
}
const functionsConfigPath = join(fileURLToPath(_config.build.server), "entry.json")
await writeFile(functionsConfigPath, JSON.stringify(functionsConfig))
const type = builders ? 'builders' : 'functions'; const type = builders ? 'builders' : 'functions';
const kind = type ?? 'functions'; const kind = type ?? 'functions';