import MagicString from "magic-string";
function isSSR(options) {
  if (options === void 0) {
    return false;
  }
  if (typeof options === "boolean") {
    return options;
  }
  if (typeof options == "object") {
    return !!options.ssr;
  }
  return false;
}
const SUPPORTED_FILES = /\.(astro|svelte|vue|[cm]?js|jsx|[cm]?ts|tsx)$/;
const IGNORED_FILES = new Set(["astro/dist/runtime/server/index.js"]);
const DEFINE_FETCH = `import fetch from 'node-fetch';
`;
function pluginFetch() {
  return {
    name: "@astrojs/vite-plugin-fetch",
    enforce: "post",
    async transform(code, id, opts) {
      for (const ignored of IGNORED_FILES) {
        if (id.endsWith(ignored))
          return null;
      }
      const ssr = isSSR(opts);
      if (!ssr) {
        return null;
      }
      if (!id.match(SUPPORTED_FILES)) {
        return null;
      }
      if (!code.includes("fetch")) {
        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};
    }
  };
}
export {
  pluginFetch as default
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL3ZpdGUtcGx1Z2luLWZldGNoL2luZGV4LnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBQ0E7QUFHQSxlQUFlLFNBQTBEO0FBQ3ZFLE1BQUksWUFBWSxRQUFXO0FBQ3pCLFdBQU87QUFBQTtBQUVULE1BQUksT0FBTyxZQUFZLFdBQVc7QUFDaEMsV0FBTztBQUFBO0FBRVQsTUFBSSxPQUFPLFdBQVcsVUFBVTtBQUM5QixXQUFPLENBQUMsQ0FBQyxRQUFRO0FBQUE7QUFFbkIsU0FBTztBQUFBO0FBS1QsTUFBTSxrQkFBa0I7QUFDeEIsTUFBTSxnQkFBZ0IsSUFBSSxJQUFJLENBQUM7QUFDL0IsTUFBTSxlQUFlO0FBQUE7QUFFTix1QkFBK0I7QUFDNUMsU0FBTztBQUFBLElBQ0wsTUFBTTtBQUFBLElBQ04sU0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