diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index e38542745..aa31245d4 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -36,6 +36,7 @@ "devDependencies": { "@netlify/edge-handler-types": "^0.34.1", "@netlify/functions": "^1.0.0", + "@types/node": "^14.18.20", "astro": "workspace:*", "astro-scripts": "workspace:*" } diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 5589e7c6b..f82e6ecc2 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -1,4 +1,5 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig, RouteData } from 'astro'; +import type { Plugin as VitePlugin } from 'vite'; import esbuild from 'esbuild'; import * as fs from 'fs'; import * as npath from 'path'; @@ -97,12 +98,31 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) return { name: '@astrojs/netlify/edge-functions', hooks: { - 'astro:config:setup': ({ config }) => { + 'astro:config:setup': ({ config, updateConfig }) => { if (dist) { config.outDir = dist; } else { config.outDir = new URL('./dist/', config.root); } + + // Add a plugin that shims the global environment. + const injectPlugin: VitePlugin = { + name: '@astrojs/netlify/plugin-inject', + generateBundle(_options, bundle) { + if(_buildConfig.serverEntry in bundle) { + const chunk = bundle[_buildConfig.serverEntry]; + if(chunk && chunk.type === 'chunk') { + chunk.code = `globalThis.process = { argv: [], env: {}, };${chunk.code}`; + } + } + } + }; + + updateConfig({ + vite: { + plugins: [injectPlugin] + } + }); }, 'astro:config:done': ({ config, setAdapter }) => { setAdapter(getAdapter()); @@ -131,18 +151,6 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) } } - // Add a plugin that shims the global environment. - vite.plugins.unshift({ - name: '@astrojs/netlify/plugin-inject', - generateBundle(options, bundle) { - if(_buildConfig.serverEntry in bundle) { - const chunk = bundle[_buildConfig.serverEntry]; - chunk.code = `globalThis.process = { argv: [], env: {}, };` + - chunk.code; - } - } - }); - vite.ssr = { noExternal: true, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3446877ca..09ad60d4f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1732,6 +1732,7 @@ importers: '@astrojs/webapi': ^0.12.0 '@netlify/edge-handler-types': ^0.34.1 '@netlify/functions': ^1.0.0 + '@types/node': ^14.18.20 astro: workspace:* astro-scripts: workspace:* esbuild: ^0.14.42 @@ -1741,6 +1742,7 @@ importers: devDependencies: '@netlify/edge-handler-types': 0.34.1 '@netlify/functions': 1.0.0 + '@types/node': 14.18.21 astro: link:../../astro astro-scripts: link:../../../scripts