From 4194082757aa1733368f8d2770da64e80f174079 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 16:41:24 -0400 Subject: [PATCH 1/4] refactor: remove Deno shim to esbuild "banner" --- packages/integrations/deno/src/index.ts | 6 ++++++ packages/integrations/deno/src/server.ts | 5 ----- packages/integrations/deno/src/shim.ts | 5 ----- 3 files changed, 6 insertions(+), 10 deletions(-) delete mode 100644 packages/integrations/deno/src/shim.ts diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 0dad8e8ef9ab5..8ab3f5e6304aa 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -63,6 +63,12 @@ export default function createIntegration(args?: Options): AstroIntegration { format: 'esm', bundle: true, external: ['@astrojs/markdown-remark'], + banner: { + js: `globalThis.process = { + argv: [], + env: Deno.env.toObject(), + };` + } }); // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. diff --git a/packages/integrations/deno/src/server.ts b/packages/integrations/deno/src/server.ts index 9d2fc98a9c89c..f6dbcb62c4b7f 100644 --- a/packages/integrations/deno/src/server.ts +++ b/packages/integrations/deno/src/server.ts @@ -1,8 +1,3 @@ -// NOTE(fks): Side-effect -- shim.js must run first. This isn't guaranteed by -// the language, but it is a Node.js behavior that we rely on here. Keep this -// separate from the other imports so that it doesn't get organized & reordered. -import './shim.js'; - // Normal Imports import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; diff --git a/packages/integrations/deno/src/shim.ts b/packages/integrations/deno/src/shim.ts deleted file mode 100644 index 62e82ba30300c..0000000000000 --- a/packages/integrations/deno/src/shim.ts +++ /dev/null @@ -1,5 +0,0 @@ -(globalThis as any).process = { - argv: [], - // @ts-ignore - env: Deno.env.toObject(), -}; From 62315af49d8bd6696b6bfa09d25219b1d81c1a5e Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 16:43:19 -0400 Subject: [PATCH 2/4] refactor: move shim to const --- packages/integrations/deno/src/index.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 8ab3f5e6304aa..a104ba863c85c 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -9,6 +9,11 @@ interface Options { hostname?: string; } +const SHIM = `globalThis.process = { + argv: [], + env: Deno.env.toObject(), +};` + export function getAdapter(args?: Options): AstroAdapter { return { name: '@astrojs/deno', @@ -64,10 +69,7 @@ export default function createIntegration(args?: Options): AstroIntegration { bundle: true, external: ['@astrojs/markdown-remark'], banner: { - js: `globalThis.process = { - argv: [], - env: Deno.env.toObject(), - };` + js: SHIM, } }); From 4c29146783e8062ae18758dedf9bec807811652a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 16:49:47 -0400 Subject: [PATCH 3/4] refactor: add shim to netlify edge --- packages/integrations/netlify/src/edge-shim.ts | 4 ---- .../netlify/src/integration-edge-functions.ts | 8 ++++++++ .../integrations/netlify/src/netlify-edge-functions.ts | 1 - 3 files changed, 8 insertions(+), 5 deletions(-) delete mode 100644 packages/integrations/netlify/src/edge-shim.ts diff --git a/packages/integrations/netlify/src/edge-shim.ts b/packages/integrations/netlify/src/edge-shim.ts deleted file mode 100644 index 1a4a6ee9be4c5..0000000000000 --- a/packages/integrations/netlify/src/edge-shim.ts +++ /dev/null @@ -1,4 +0,0 @@ -(globalThis as any).process = { - argv: [], - env: {}, -}; diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 72788abaf348c..17eaf8350f825 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -6,6 +6,11 @@ import { fileURLToPath } from 'url'; import type { Plugin as VitePlugin } from 'vite'; import { createRedirects } from './shared.js'; +const SHIM = `globalThis.process = { + argv: [], + env: {}, +};`; + export function getAdapter(): AstroAdapter { return { name: '@astrojs/netlify/edge-functions', @@ -78,6 +83,9 @@ async function bundleServerEntry(buildConfig: BuildConfig, vite: any) { format: 'esm', bundle: true, external: ['@astrojs/markdown-remark'], + banner: { + js: SHIM, + } }); // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. diff --git a/packages/integrations/netlify/src/netlify-edge-functions.ts b/packages/integrations/netlify/src/netlify-edge-functions.ts index 8d3ecf1079f27..0d2974c61d5cd 100644 --- a/packages/integrations/netlify/src/netlify-edge-functions.ts +++ b/packages/integrations/netlify/src/netlify-edge-functions.ts @@ -1,6 +1,5 @@ import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; -import './edge-shim.js'; export function createExports(manifest: SSRManifest) { const app = new App(manifest); From f758e489ec54a7ceecf4d043229113542b73c1cc Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 17:08:08 -0400 Subject: [PATCH 4/4] chore: changeset --- .changeset/spotty-islands-raise.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/spotty-islands-raise.md diff --git a/.changeset/spotty-islands-raise.md b/.changeset/spotty-islands-raise.md new file mode 100644 index 0000000000000..f8246006de127 --- /dev/null +++ b/.changeset/spotty-islands-raise.md @@ -0,0 +1,6 @@ +--- +'@astrojs/deno': patch +'@astrojs/netlify': patch +--- + +Fix: append shim to top of built file to avoid "can't read process of undefined" issues