From 39df7952a5a53e8540b834419e8fa553e45ea7d3 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 3 Aug 2021 13:13:00 -0400 Subject: [PATCH] Make fetch available in all component types (#949) * Make fetch available in all component types This makes `globalThis.fetch` available in all components. * Adds a changeset --- .changeset/wet-garlics-beam.md | 5 +++++ packages/astro/src/compiler/index.ts | 4 ++++ packages/astro/test/fetch.test.js | 18 ++++++++++++++++++ .../test/fixtures/fetch/snowpack.config.json | 3 +++ .../fixtures/fetch/src/components/Child.jsx | 7 +++++++ .../test/fixtures/fetch/src/pages/index.astro | 12 ++++++++++++ 6 files changed, 49 insertions(+) create mode 100644 .changeset/wet-garlics-beam.md create mode 100644 packages/astro/test/fetch.test.js create mode 100644 packages/astro/test/fixtures/fetch/snowpack.config.json create mode 100644 packages/astro/test/fixtures/fetch/src/components/Child.jsx create mode 100644 packages/astro/test/fixtures/fetch/src/pages/index.astro diff --git a/.changeset/wet-garlics-beam.md b/.changeset/wet-garlics-beam.md new file mode 100644 index 000000000..30773e3bc --- /dev/null +++ b/.changeset/wet-garlics-beam.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Makes `fetch` available in all framework components diff --git a/packages/astro/src/compiler/index.ts b/packages/astro/src/compiler/index.ts index c7bd98655..c9cce245f 100644 --- a/packages/astro/src/compiler/index.ts +++ b/packages/astro/src/compiler/index.ts @@ -115,6 +115,10 @@ export async function compileComponent(source: string, { compileOptions, filenam import fetch from 'node-fetch'; ${result.imports.join('\n')} +if(!('fetch' in globalThis)) { + globalThis.fetch = fetch; +} + ${/* Global Astro Namespace (shadowed & extended by the scoped namespace inside of __render()) */ ''} const __TopLevelAstro = { site: new URL(${JSON.stringify(site)}), diff --git a/packages/astro/test/fetch.test.js b/packages/astro/test/fetch.test.js new file mode 100644 index 000000000..5f00a61ad --- /dev/null +++ b/packages/astro/test/fetch.test.js @@ -0,0 +1,18 @@ +import { suite } from 'uvu'; +import * as assert from 'uvu/assert'; +import { doc } from './test-utils.js'; +import { setup } from './helpers.js'; + +const Fetch = suite('Global Fetch'); + +setup(Fetch, './fixtures/fetch'); + +Fetch('Is available in non-Astro components.', async ({ runtime }) => { + const result = await runtime.load('/'); + assert.ok(!result.error, `build error: ${result.error}`); + + const $ = doc(result.contents); + assert.equal($('#jsx').text(), 'function'); +}); + +Fetch.run(); \ No newline at end of file diff --git a/packages/astro/test/fixtures/fetch/snowpack.config.json b/packages/astro/test/fixtures/fetch/snowpack.config.json new file mode 100644 index 000000000..8f034781d --- /dev/null +++ b/packages/astro/test/fixtures/fetch/snowpack.config.json @@ -0,0 +1,3 @@ +{ + "workspaceRoot": "../../../../../" +} diff --git a/packages/astro/test/fixtures/fetch/src/components/Child.jsx b/packages/astro/test/fixtures/fetch/src/components/Child.jsx new file mode 100644 index 000000000..071473ea9 --- /dev/null +++ b/packages/astro/test/fixtures/fetch/src/components/Child.jsx @@ -0,0 +1,7 @@ +import { h } from 'preact'; + +export default function() { + return ( + { typeof fetch } + ); +} \ No newline at end of file diff --git a/packages/astro/test/fixtures/fetch/src/pages/index.astro b/packages/astro/test/fixtures/fetch/src/pages/index.astro new file mode 100644 index 000000000..66979efa3 --- /dev/null +++ b/packages/astro/test/fixtures/fetch/src/pages/index.astro @@ -0,0 +1,12 @@ +--- +import Child from '../components/Child.jsx'; +--- + + + + Global fetch + + + + + \ No newline at end of file