From 91173fe9380017310d9f32e7c4ffbf2b26287c30 Mon Sep 17 00:00:00 2001 From: Jonathan Neal Date: Wed, 15 Dec 2021 14:11:14 -0500 Subject: [PATCH] Replace `fetch` walk-n-replace with polyfill (#2206) * Replace fetch replacer with genuine polyfill * chore(lint): Prettier fix * changeset Co-authored-by: github-actions[bot] --- .changeset/clever-years-appear.md | 5 +++++ packages/astro/package.json | 1 + packages/astro/src/core/build/index.ts | 4 ++++ packages/astro/src/core/create-vite.ts | 2 -- packages/astro/src/core/dev/index.ts | 4 ++++ yarn.lock | 5 +++++ 6 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 .changeset/clever-years-appear.md diff --git a/.changeset/clever-years-appear.md b/.changeset/clever-years-appear.md new file mode 100644 index 0000000000..275faecf8e --- /dev/null +++ b/.changeset/clever-years-appear.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Replaced `fetch` walk-n-replace with globalThis polyfill diff --git a/packages/astro/package.json b/packages/astro/package.json index 4617c0a6bc..2e8698df5c 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -64,6 +64,7 @@ "@astrojs/renderer-react": "0.3.1", "@astrojs/renderer-svelte": "0.2.2", "@astrojs/renderer-vue": "0.2.1", + "@astropub/webapi": "^0.4.0", "@babel/core": "^7.15.8", "@babel/traverse": "^7.15.4", "@proload/core": "^0.2.1", diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 1842afc22c..a47d1aaba2 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -7,6 +7,7 @@ import { rollupPluginAstroBuildHTML } from '../../vite-plugin-build-html/index.j import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js'; import fs from 'fs'; import * as colors from 'kleur/colors'; +import { polyfill } from '@astropub/webapi'; import { performance } from 'perf_hooks'; import vite, { ViteDevServer } from '../vite.js'; import { fileURLToPath } from 'url'; @@ -25,6 +26,9 @@ export interface BuildOptions { /** `astro build` */ export default async function build(config: AstroConfig, options: BuildOptions = { logging: defaultLogOptions }): Promise { + // polyfill WebAPIs to globalThis for Node v12, Node v14, and Node v16 + polyfill(globalThis); + const builder = new AstroBuilder(config, options); await builder.build(); } diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 485300f7a9..2001afc540 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -10,7 +10,6 @@ import astroPostprocessVitePlugin from '../vite-plugin-astro-postprocess/index.j import configAliasVitePlugin from '../vite-plugin-config-alias/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import jsxVitePlugin from '../vite-plugin-jsx/index.js'; -import fetchVitePlugin from '../vite-plugin-fetch/index.js'; import { resolveDependency } from './util.js'; // Some packages are just external, and that’s the way it goes. @@ -55,7 +54,6 @@ export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig, markdownVitePlugin({ config: astroConfig, devServer }), jsxVitePlugin({ config: astroConfig, logging }), astroPostprocessVitePlugin({ config: astroConfig, devServer }), - fetchVitePlugin(), ], publicDir: fileURLToPath(astroConfig.public), root: fileURLToPath(astroConfig.projectRoot), diff --git a/packages/astro/src/core/dev/index.ts b/packages/astro/src/core/dev/index.ts index c2f2bca94a..3fcda01144 100644 --- a/packages/astro/src/core/dev/index.ts +++ b/packages/astro/src/core/dev/index.ts @@ -9,6 +9,7 @@ import { fileURLToPath } from 'url'; import { promisify } from 'util'; import connect from 'connect'; import mime from 'mime'; +import { polyfill } from '@astropub/webapi'; import { performance } from 'perf_hooks'; import stripAnsi from 'strip-ansi'; import vite from '../vite.js'; @@ -35,6 +36,9 @@ export interface DevServer { /** `astro dev` */ export default async function dev(config: AstroConfig, options: DevOptions = { logging: defaultLogOptions }): Promise { + // polyfill WebAPIs to globalThis for Node v12, Node v14, and Node v16 + polyfill(globalThis); + // start dev server const server = new AstroDevServer(config, options); await server.start(); diff --git a/yarn.lock b/yarn.lock index de0e860749..d92281fd31 100644 --- a/yarn.lock +++ b/yarn.lock @@ -140,6 +140,11 @@ vscode-languageserver-types "^3.16.0" vscode-uri "^3.0.2" +"@astropub/webapi@^0.4.0": + version "0.4.0" + resolved "https://registry.yarnpkg.com/@astropub/webapi/-/webapi-0.4.0.tgz#ce03357e562758fe8ac9b461fec4206a888c8e56" + integrity sha512-KpHOH9WsIJk2E3Z/suAZri3a6I9GrkZnmm+BHAZp4OBAXUUyNTIid9mIC8tiiHNbY2A6OLbBvfteuD+BYQfcrw== + "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4", "@babel/code-frame@^7.16.0": version "7.16.0" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.16.0.tgz#0dfc80309beec8411e65e706461c408b0bb9b431"