summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonathan Neal <jonathantneal@hotmail.com> 2022-01-25 09:49:17 -0500
committerGravatar GitHub <noreply@github.com> 2022-01-25 09:49:17 -0500
commit4c4c8013781956df6ff6c8fa3a6d1cf69aa7f521 (patch)
tree9a9d3ce71a343c58d6cbd1eec30175fed42db19a
parent9a0c76a4a55cdc7c3616efdfccb1342f47794736 (diff)
downloadastro-4c4c8013781956df6ff6c8fa3a6d1cf69aa7f521.tar.gz
astro-4c4c8013781956df6ff6c8fa3a6d1cf69aa7f521.tar.zst
astro-4c4c8013781956df6ff6c8fa3a6d1cf69aa7f521.zip
Remove `vite-plugin-fetch` (#2460)
This plugin is not used in the project
-rw-r--r--packages/astro/src/vite-plugin-fetch/README.md3
-rw-r--r--packages/astro/src/vite-plugin-fetch/index.ts84
2 files changed, 0 insertions, 87 deletions
diff --git a/packages/astro/src/vite-plugin-fetch/README.md b/packages/astro/src/vite-plugin-fetch/README.md
deleted file mode 100644
index 28d65a095..000000000
--- a/packages/astro/src/vite-plugin-fetch/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# vite-plugin-fetch
-
-Adds fetch support in SSR contexts.
diff --git a/packages/astro/src/vite-plugin-fetch/index.ts b/packages/astro/src/vite-plugin-fetch/index.ts
deleted file mode 100644
index 19c355708..000000000
--- a/packages/astro/src/vite-plugin-fetch/index.ts
+++ /dev/null
@@ -1,84 +0,0 @@
-import type { Plugin } from '../core/vite';
-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)$/;
-const IGNORED_MODULES = [/astro\/dist\/runtime\/server/, /\/node-fetch\//];
-const DEFINE_FETCH = `import fetch from 'node-fetch';\n`;
-
-function isIdentifier(node: BaseNode): node is Identifier {
- return node.type === '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;
- }
-
- const ast = this.parse(code);
- let fetchDeclared = false;
- walk(ast, {
- enter(node, parent) {
- if (fetchDeclared) return this.skip();
- if (isIdentifier(node)) {
- // Identifier is OK in any type of Expression (CallExpression, UnaryExpression, etc)
- if (node.name === 'fetch' && !parent.type.endsWith('Expression')) {
- fetchDeclared = true;
- }
- }
- },
- });
-
- // Fetch is already declared, do not inject a re-declaration!
- if (fetchDeclared) {
- return null;
- }
-
- // Ignore specific modules
- for (const ignored of IGNORED_MODULES) {
- if (id.match(ignored)) {
- return null;
- }
- }
- const s = new MagicString(code);
- s.prepend(DEFINE_FETCH);
- const result = s.toString();
- const map = s.generateMap({
- source: id,
- includeContent: true,
- });
- return { code: result, map };
- },
- };
-}