From 008f7647c4788207aab55ab12c734bd80e6df9c5 Mon Sep 17 00:00:00 2001 From: Simon Knott Date: Mon, 25 Sep 2023 15:45:13 +0200 Subject: [PATCH] fix(netlify): bundle netlify functions as ESM to support top-level await (#8661) --- .changeset/witty-carrots-applaud.md | 5 +++++ .../netlify/src/integration-functions.ts | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/witty-carrots-applaud.md diff --git a/.changeset/witty-carrots-applaud.md b/.changeset/witty-carrots-applaud.md new file mode 100644 index 000000000..16f516e40 --- /dev/null +++ b/.changeset/witty-carrots-applaud.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +fix build failures because of CJS builds and top-level await diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index b8d56ad65..81aa8ff2f 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -1,5 +1,6 @@ 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 { generateEdgeMiddleware } from './middleware.js'; import type { Args } from './netlify-functions.js'; @@ -85,6 +86,15 @@ function netlifyFunctions({ } }, '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 kind = type ?? 'functions';