diff --git a/packages/astro/src/vite-plugin-fetch/index.ts b/packages/astro/src/vite-plugin-fetch/index.ts index 5191c1b80..8363248a7 100644 --- a/packages/astro/src/vite-plugin-fetch/index.ts +++ b/packages/astro/src/vite-plugin-fetch/index.ts @@ -3,20 +3,6 @@ import type { BaseNode, Identifier } from 'estree'; import MagicString from 'magic-string'; import { walk } from 'estree-walker'; -// https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726 -function isSSR(options: undefined | boolean | { ssr?: boolean }): boolean { - if (options === undefined) { - return false; - } - if (typeof options === 'boolean') { - return options; - } - if (typeof options == 'object') { - return !!options.ssr; - } - return false; -} - // This matches any JS-like file (that we know of) // See https://regex101.com/r/Cgofir/1 const SUPPORTED_FILES = /\.(astro|svelte|vue|[cm]?js|jsx|[cm]?ts|tsx)$/; @@ -28,23 +14,23 @@ function isIdentifier(node: BaseNode): node is Identifier { } export default function pluginFetch(): Plugin { - return { - name: '@astrojs/vite-plugin-fetch', - enforce: 'post', - async transform(code, id, opts) { - const ssr = isSSR(opts); - // If this isn't an SSR pass, `fetch` will already be available! - if (!ssr) { - return null; - } - // Only transform JS-like files - if (!id.match(SUPPORTED_FILES)) { - return null; - } - // Optimization: only run on probable matches - if (!code.includes('fetch')) { - return null; - } + return { + name: '@astrojs/vite-plugin-fetch', + enforce: 'post', + async transform(code, id, opts) { + const ssr = Boolean(opts?.ssr); + // If this isn't an SSR pass, `fetch` will already be available! + if (!ssr) { + return null; + } + // Only transform JS-like files + if (!id.match(SUPPORTED_FILES)) { + return null; + } + // Optimization: only run on probable matches + if (!code.includes('fetch')) { + return null; + } const ast = this.parse(code); let fetchDeclared = false; diff --git a/packages/astro/src/vite-plugin-jsx/index.ts b/packages/astro/src/vite-plugin-jsx/index.ts index a1efb6ffd..8434aebb9 100644 --- a/packages/astro/src/vite-plugin-jsx/index.ts +++ b/packages/astro/src/vite-plugin-jsx/index.ts @@ -24,21 +24,6 @@ const IMPORT_STATEMENTS: Record = { // be careful about esbuild not treating h, React, Fragment, etc. as unused. const PREVENT_UNUSED_IMPORTS = ';;(React,Fragment,h);'; -// This check on a flexible "options" object is needed because Vite uses this flexible argument for ssr. -// More context: https://github.com/vitejs/vite/discussions/5109#discussioncomment-1450726 -function isSSR(options: undefined | boolean | { ssr: boolean }): boolean { - if (options === undefined) { - return false; - } - if (typeof options === 'boolean') { - return options; - } - if (typeof options == 'object') { - return !!options.ssr; - } - return false; -} - function getEsbuildLoader(fileExt: string): string { return fileExt.substr(1); }