Fix types for the edge function integration

This commit is contained in:
unknown 2022-06-20 08:27:17 -04:00 committed by Matthew Phillips
parent 0c3a85f2e3
commit 4e4ff19fe0
3 changed files with 24 additions and 13 deletions

View file

@ -36,6 +36,7 @@
"devDependencies": { "devDependencies": {
"@netlify/edge-handler-types": "^0.34.1", "@netlify/edge-handler-types": "^0.34.1",
"@netlify/functions": "^1.0.0", "@netlify/functions": "^1.0.0",
"@types/node": "^14.18.20",
"astro": "workspace:*", "astro": "workspace:*",
"astro-scripts": "workspace:*" "astro-scripts": "workspace:*"
} }

View file

@ -1,4 +1,5 @@
import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig, RouteData } from 'astro'; import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig, RouteData } from 'astro';
import type { Plugin as VitePlugin } from 'vite';
import esbuild from 'esbuild'; import esbuild from 'esbuild';
import * as fs from 'fs'; import * as fs from 'fs';
import * as npath from 'path'; import * as npath from 'path';
@ -97,12 +98,31 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {})
return { return {
name: '@astrojs/netlify/edge-functions', name: '@astrojs/netlify/edge-functions',
hooks: { hooks: {
'astro:config:setup': ({ config }) => { 'astro:config:setup': ({ config, updateConfig }) => {
if (dist) { if (dist) {
config.outDir = dist; config.outDir = dist;
} else { } else {
config.outDir = new URL('./dist/', config.root); 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 }) => { 'astro:config:done': ({ config, setAdapter }) => {
setAdapter(getAdapter()); 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 = { vite.ssr = {
noExternal: true, noExternal: true,
}; };

View file

@ -1732,6 +1732,7 @@ importers:
'@astrojs/webapi': ^0.12.0 '@astrojs/webapi': ^0.12.0
'@netlify/edge-handler-types': ^0.34.1 '@netlify/edge-handler-types': ^0.34.1
'@netlify/functions': ^1.0.0 '@netlify/functions': ^1.0.0
'@types/node': ^14.18.20
astro: workspace:* astro: workspace:*
astro-scripts: workspace:* astro-scripts: workspace:*
esbuild: ^0.14.42 esbuild: ^0.14.42
@ -1741,6 +1742,7 @@ importers:
devDependencies: devDependencies:
'@netlify/edge-handler-types': 0.34.1 '@netlify/edge-handler-types': 0.34.1
'@netlify/functions': 1.0.0 '@netlify/functions': 1.0.0
'@types/node': 14.18.21
astro: link:../../astro astro: link:../../astro
astro-scripts: link:../../../scripts astro-scripts: link:../../../scripts