From 4cfdea89397ecc4c3cd5ab4895645b5e70ad6c1a Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 10:10:06 +0100 Subject: [PATCH 1/9] fix(netlify): Try to make polyfill external --- packages/integrations/netlify/package.json | 2 ++ packages/integrations/netlify/src/integration-functions.ts | 5 +++++ packages/integrations/netlify/src/netlify-functions.ts | 7 ++----- packages/integrations/netlify/src/polyfill.ts | 5 +++++ 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 packages/integrations/netlify/src/polyfill.ts diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index bba5f7709616..78cf4dc1fd10 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -23,6 +23,8 @@ "./netlify-functions.js": "./dist/netlify-functions.js", "./edge-functions": "./dist/integration-edge-functions.js", "./netlify-edge-functions.js": "./dist/netlify-edge-functions.js", + "./polyfill.js": "./dist/polyfill.js", + "./polyfill": "./dist/polyfill.js", "./package.json": "./package.json" }, "scripts": { diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index 609dc2500541..76511ad72d72 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -35,6 +35,11 @@ function netlifyFunctions({ client: outDir, server: new URL('./.netlify/functions-internal/', config.root), }, + vite: { + ssr: { + external: ['@astrojs/netlify/polyfill'], + }, + }, }); }, 'astro:config:done': ({ config, setAdapter }) => { diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts index 0f9b581587f2..2332f0a8774b 100644 --- a/packages/integrations/netlify/src/netlify-functions.ts +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -1,12 +1,9 @@ -import { polyfill } from '@astrojs/webapi'; +import './polyfill'; + import { builder, Handler } from '@netlify/functions'; import { SSRManifest } from 'astro'; import { App } from 'astro/app'; -polyfill(globalThis, { - exclude: 'window document', -}); - export interface Args { builders?: boolean; binaryMediaTypes?: string[]; diff --git a/packages/integrations/netlify/src/polyfill.ts b/packages/integrations/netlify/src/polyfill.ts new file mode 100644 index 000000000000..01f7b39bffc1 --- /dev/null +++ b/packages/integrations/netlify/src/polyfill.ts @@ -0,0 +1,5 @@ +import { polyfill } from '@astrojs/webapi'; + +polyfill(globalThis, { + exclude: 'window document', +}); From 52a0d550825318196400eec415532bdcb318176c Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 14:42:46 +0100 Subject: [PATCH 2/9] feat(webapi): Add polyfill applier for SSR --- packages/webapi/package.json | 6 +++++- packages/webapi/polyfill-ssr.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 packages/webapi/polyfill-ssr.js diff --git a/packages/webapi/package.json b/packages/webapi/package.json index 8ccea4aa1fc5..54e225b96c4f 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -10,6 +10,9 @@ }, "./apply": { "import": "./apply.js" + }, + "./polyfill-ssr.js": { + "import": "./polyfill-ssr.js" } }, "main": "mod.js", @@ -18,7 +21,8 @@ "apply.js", "mod.d.ts", "mod.js", - "mod.js.map" + "mod.js.map", + "polyfill-ssr.js" ], "keywords": [ "astro", diff --git a/packages/webapi/polyfill-ssr.js b/packages/webapi/polyfill-ssr.js new file mode 100644 index 000000000000..eddb41ba0e47 --- /dev/null +++ b/packages/webapi/polyfill-ssr.js @@ -0,0 +1,7 @@ +import { polyfill } from './mod.js' + +export * from './mod.js' + +polyfill(globalThis, { + exclude: 'window document', +}) From efd6119d6e476791af8654b1c2444990fcb4e86a Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 14:43:08 +0100 Subject: [PATCH 3/9] fix(netlify): Externalize polyfills so they're always applied before user code --- packages/integrations/netlify/src/integration-functions.ts | 2 +- packages/integrations/netlify/src/netlify-functions.ts | 2 +- packages/integrations/netlify/src/polyfill.ts | 5 ----- 3 files changed, 2 insertions(+), 7 deletions(-) delete mode 100644 packages/integrations/netlify/src/polyfill.ts diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index 76511ad72d72..dc44c2f791bc 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -37,7 +37,7 @@ function netlifyFunctions({ }, vite: { ssr: { - external: ['@astrojs/netlify/polyfill'], + external: ['@astrojs/webapi/polyfill-ssr.js'], }, }, }); diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts index 2332f0a8774b..f4a81b691362 100644 --- a/packages/integrations/netlify/src/netlify-functions.ts +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -1,4 +1,4 @@ -import './polyfill'; +import '@astrojs/webapi/polyfill-ssr.js'; import { builder, Handler } from '@netlify/functions'; import { SSRManifest } from 'astro'; diff --git a/packages/integrations/netlify/src/polyfill.ts b/packages/integrations/netlify/src/polyfill.ts deleted file mode 100644 index 01f7b39bffc1..000000000000 --- a/packages/integrations/netlify/src/polyfill.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { polyfill } from '@astrojs/webapi'; - -polyfill(globalThis, { - exclude: 'window document', -}); From c8d47b3771331770bae777397a8742f7be9d701d Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 14:44:23 +0100 Subject: [PATCH 4/9] chore: changeset --- .changeset/gold-socks-applaud.md | 5 +++++ .changeset/silent-dragons-sell.md | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 .changeset/gold-socks-applaud.md create mode 100644 .changeset/silent-dragons-sell.md diff --git a/.changeset/gold-socks-applaud.md b/.changeset/gold-socks-applaud.md new file mode 100644 index 000000000000..f8d21c56edd1 --- /dev/null +++ b/.changeset/gold-socks-applaud.md @@ -0,0 +1,5 @@ +--- +'@astrojs/webapi': patch +--- + +Add `polyfill-ssr.js` file to apply polyfills needed for certain SSR contexts as a side effect diff --git a/.changeset/silent-dragons-sell.md b/.changeset/silent-dragons-sell.md new file mode 100644 index 000000000000..f2571dc1a722 --- /dev/null +++ b/.changeset/silent-dragons-sell.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +Fix polyfills not being available in certain cases From 2762d4e8d4042d0edc6029b5b37711785b696181 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 15:00:14 +0100 Subject: [PATCH 5/9] test(netlify): Add test for polyfill being in the proper place --- .../fixtures/polyfills/src/pages/index.astro | 4 +++ .../netlify/test/functions/polyfills.test.js | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro create mode 100644 packages/integrations/netlify/test/functions/polyfills.test.js diff --git a/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro b/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro new file mode 100644 index 000000000000..1d8c19257851 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro @@ -0,0 +1,4 @@ +--- +--- + +
{typeof fetch}
diff --git a/packages/integrations/netlify/test/functions/polyfills.test.js b/packages/integrations/netlify/test/functions/polyfills.test.js new file mode 100644 index 000000000000..96406e2b6390 --- /dev/null +++ b/packages/integrations/netlify/test/functions/polyfills.test.js @@ -0,0 +1,28 @@ +import { expect } from 'chai'; +import netlifyAdapter from '../../dist/index.js'; +import { loadFixture, testIntegration } from './test-utils.js'; + +describe('Support polyfills', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/polyfills/', import.meta.url).toString(), + output: 'server', + adapter: netlifyAdapter({ + dist: new URL('./fixtures/polyfills/dist/', import.meta.url), + }), + site: `http://example.com`, + integrations: [testIntegration()], + }); + await fixture.build(); + }); + + it('starts with the polyfill import', async () => { + const entry = await fixture.readFile('../.netlify/functions-internal/entry.mjs'); + expect(entry).to.satisfy((content) => + content.startsWith("import '@astrojs/webapi/polyfill-ssr.js';") + ); + }); +}); From 328f93aae215372a78c51ca2248f57a319ad3c91 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Fri, 3 Feb 2023 15:30:31 +0100 Subject: [PATCH 6/9] config(netlify): Remove unnecessary change to package.json --- packages/integrations/netlify/package.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index 53f25ae6aaa9..25695ea79181 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -23,8 +23,6 @@ "./netlify-functions.js": "./dist/netlify-functions.js", "./edge-functions": "./dist/integration-edge-functions.js", "./netlify-edge-functions.js": "./dist/netlify-edge-functions.js", - "./polyfill.js": "./dist/polyfill.js", - "./polyfill": "./dist/polyfill.js", "./package.json": "./package.json" }, "scripts": { From 29197594534302b3a4110c15eb9d0849bd73c027 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Mon, 6 Feb 2023 17:21:26 +0100 Subject: [PATCH 7/9] revert(netlify): Revert changes --- .changeset/gold-socks-applaud.md | 5 ----- packages/integrations/netlify/src/integration-functions.ts | 5 ----- packages/integrations/netlify/src/netlify-functions.ts | 7 +++++-- packages/webapi/package.json | 6 +----- packages/webapi/polyfill-ssr.js | 7 ------- 5 files changed, 6 insertions(+), 24 deletions(-) delete mode 100644 .changeset/gold-socks-applaud.md delete mode 100644 packages/webapi/polyfill-ssr.js diff --git a/.changeset/gold-socks-applaud.md b/.changeset/gold-socks-applaud.md deleted file mode 100644 index f8d21c56edd1..000000000000 --- a/.changeset/gold-socks-applaud.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@astrojs/webapi': patch ---- - -Add `polyfill-ssr.js` file to apply polyfills needed for certain SSR contexts as a side effect diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index dc44c2f791bc..609dc2500541 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -35,11 +35,6 @@ function netlifyFunctions({ client: outDir, server: new URL('./.netlify/functions-internal/', config.root), }, - vite: { - ssr: { - external: ['@astrojs/webapi/polyfill-ssr.js'], - }, - }, }); }, 'astro:config:done': ({ config, setAdapter }) => { diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts index f4a81b691362..0f9b581587f2 100644 --- a/packages/integrations/netlify/src/netlify-functions.ts +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -1,9 +1,12 @@ -import '@astrojs/webapi/polyfill-ssr.js'; - +import { polyfill } from '@astrojs/webapi'; import { builder, Handler } from '@netlify/functions'; import { SSRManifest } from 'astro'; import { App } from 'astro/app'; +polyfill(globalThis, { + exclude: 'window document', +}); + export interface Args { builders?: boolean; binaryMediaTypes?: string[]; diff --git a/packages/webapi/package.json b/packages/webapi/package.json index 54e225b96c4f..8ccea4aa1fc5 100644 --- a/packages/webapi/package.json +++ b/packages/webapi/package.json @@ -10,9 +10,6 @@ }, "./apply": { "import": "./apply.js" - }, - "./polyfill-ssr.js": { - "import": "./polyfill-ssr.js" } }, "main": "mod.js", @@ -21,8 +18,7 @@ "apply.js", "mod.d.ts", "mod.js", - "mod.js.map", - "polyfill-ssr.js" + "mod.js.map" ], "keywords": [ "astro", diff --git a/packages/webapi/polyfill-ssr.js b/packages/webapi/polyfill-ssr.js deleted file mode 100644 index eddb41ba0e47..000000000000 --- a/packages/webapi/polyfill-ssr.js +++ /dev/null @@ -1,7 +0,0 @@ -import { polyfill } from './mod.js' - -export * from './mod.js' - -polyfill(globalThis, { - exclude: 'window document', -}) From b94e619a4e00126a0b046859b1a39549e3cef748 Mon Sep 17 00:00:00 2001 From: Princesseuh Date: Mon, 6 Feb 2023 18:59:14 +0100 Subject: [PATCH 8/9] feat(astro): Add a way for packages to explicitely mark themselves as needing to be external --- packages/astro/src/core/create-vite.ts | 6 ++++ packages/integrations/netlify/package.json | 3 ++ .../fixtures/polyfills/src/pages/index.astro | 4 --- .../netlify/test/functions/polyfills.test.js | 28 ------------------- 4 files changed, 9 insertions(+), 32 deletions(-) delete mode 100644 packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro delete mode 100644 packages/integrations/netlify/test/functions/polyfills.test.js diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 9708466c9215..03b0ee5d1f70 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -55,6 +55,12 @@ export async function createVite( isBuild: mode === 'build', viteUserConfig: settings.config.vite, isFrameworkPkgByJson(pkgJson) { + // Certain packages will trigger the checks below, but need to be external. A common example are SSR adapters + // for node-based platforms, as we need to control the order of the import paths to make sure polyfills are applied in time. + if (pkgJson?.astro?.external === true) { + return false; + } + return ( // Attempt: package relies on `astro`. ✅ Definitely an Astro package pkgJson.peerDependencies?.astro || diff --git a/packages/integrations/netlify/package.json b/packages/integrations/netlify/package.json index 25695ea79181..33caf0f62004 100644 --- a/packages/integrations/netlify/package.json +++ b/packages/integrations/netlify/package.json @@ -50,5 +50,8 @@ "cheerio": "^1.0.0-rc.11", "mocha": "^9.2.2", "vite": "^4.0.3" + }, + "astro": { + "external": true } } diff --git a/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro b/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro deleted file mode 100644 index 1d8c19257851..000000000000 --- a/packages/integrations/netlify/test/functions/fixtures/polyfills/src/pages/index.astro +++ /dev/null @@ -1,4 +0,0 @@ ---- ---- - -
{typeof fetch}
diff --git a/packages/integrations/netlify/test/functions/polyfills.test.js b/packages/integrations/netlify/test/functions/polyfills.test.js deleted file mode 100644 index 96406e2b6390..000000000000 --- a/packages/integrations/netlify/test/functions/polyfills.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import { expect } from 'chai'; -import netlifyAdapter from '../../dist/index.js'; -import { loadFixture, testIntegration } from './test-utils.js'; - -describe('Support polyfills', () => { - /** @type {import('./test-utils').Fixture} */ - let fixture; - - before(async () => { - fixture = await loadFixture({ - root: new URL('./fixtures/polyfills/', import.meta.url).toString(), - output: 'server', - adapter: netlifyAdapter({ - dist: new URL('./fixtures/polyfills/dist/', import.meta.url), - }), - site: `http://example.com`, - integrations: [testIntegration()], - }); - await fixture.build(); - }); - - it('starts with the polyfill import', async () => { - const entry = await fixture.readFile('../.netlify/functions-internal/entry.mjs'); - expect(entry).to.satisfy((content) => - content.startsWith("import '@astrojs/webapi/polyfill-ssr.js';") - ); - }); -}); From be2b777c4b8ae00ca68d26b38fee18b70755e81f Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Mon, 6 Feb 2023 13:25:13 -0600 Subject: [PATCH 9/9] Update .changeset/silent-dragons-sell.md --- .changeset/silent-dragons-sell.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/silent-dragons-sell.md b/.changeset/silent-dragons-sell.md index f2571dc1a722..233d99a9696a 100644 --- a/.changeset/silent-dragons-sell.md +++ b/.changeset/silent-dragons-sell.md @@ -1,5 +1,6 @@ --- '@astrojs/netlify': patch +'astro': patch --- Fix polyfills not being available in certain cases