import {fileURLToPath} from "url";
import vite from "./vite.js";
import astroVitePlugin from "../vite-plugin-astro/index.js";
import astroPostprocessVitePlugin from "../vite-plugin-astro-postprocess/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";
const ALWAYS_EXTERNAL = new Set([
  "@sveltejs/vite-plugin-svelte",
  "estree-util-value-to-estree",
  "micromark-util-events-to-acorn",
  "node-fetch",
  "prismjs",
  "shiki",
  "shorthash",
  "unified",
  "whatwg-url"
]);
const ALWAYS_NOEXTERNAL = new Set([
  "astro"
]);
async function createVite(inlineConfig, {astroConfig, logging, devServer}) {
  let viteConfig = {
    cacheDir: fileURLToPath(new URL("./node_modules/.vite/", astroConfig.projectRoot)),
    clearScreen: false,
    logLevel: "error",
    optimizeDeps: {
      entries: ["src/**/*"]
    },
    plugins: [
      astroVitePlugin({config: astroConfig, devServer}),
      markdownVitePlugin({config: astroConfig, devServer}),
      jsxVitePlugin({config: astroConfig, logging}),
      astroPostprocessVitePlugin({config: astroConfig, devServer}),
      fetchVitePlugin()
    ],
    publicDir: fileURLToPath(astroConfig.public),
    root: fileURLToPath(astroConfig.projectRoot),
    server: {
      force: true,
      hmr: process.env.NODE_ENV === "test" || process.env.NODE_ENV === "production" ? false : void 0,
      proxy: {}
    },
    ssr: {
      external: [...ALWAYS_EXTERNAL],
      noExternal: [...ALWAYS_NOEXTERNAL]
    }
  };
  for (const name of astroConfig.renderers) {
    try {
      const {default: renderer} = await import(resolveDependency(name, astroConfig));
      if (!renderer)
        continue;
      if (renderer.viteConfig) {
        if (typeof renderer.viteConfig !== "function") {
          throw new Error(`${name}: viteConfig(options) must be a function! Got ${typeof renderer.viteConfig}.`);
        }
        const rendererConfig = await renderer.viteConfig({mode: inlineConfig.mode, command: inlineConfig.mode === "production" ? "build" : "serve"});
        viteConfig = vite.mergeConfig(viteConfig, rendererConfig);
      }
    } catch (err) {
      throw new Error(`${name}: ${err}`);
    }
  }
  viteConfig = vite.mergeConfig(viteConfig, astroConfig.vite || {});
  viteConfig = vite.mergeConfig(viteConfig, inlineConfig);
  return viteConfig;
}
export {
  createVite
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vc3JjL2NvcmUvY3JlYXRlLXZpdGUudHMiXSwKICAibWFwcGluZ3MiOiAiQUFJQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBR0EsTUFBTSxrQkFBa0IsSUFBSSxJQUFJO0FBQUEsRUFDOUI7QUFBQSxFQUNBO0FBQUEsRUFDQTtBQUFBLEVBQ0E7QUFBQSxFQUNBO0FBQUEsRUFDQTtBQUFBLEVBQ0E7QUFBQSxFQUNBO0FBQUEsRUFDQTtBQUFBO0FBRUYsTUFBTSxvQkFBb0IsSUFBSSxJQUFJO0FBQUEsRUFDaEM7QUFBQTtBQWFGLDBCQUFpQyxjQUFpQyxDQUFFLGFBQWEsU0FBUyxZQUE0RDtBQUVwSixNQUFJLGFBQWdDO0FBQUEsSUFDbEMsVUFBVSxjQUFjLElBQUksSUFBSSx5QkFBeUIsWUFBWTtBQUFBLElBQ3JFLGFBQWE7QUFBQSxJQUNiLFVBQVU7QUFBQSxJQUNWLGNBQWM7QUFBQSxNQUNaLFNBQVMsQ0FBQztBQUFBO0FBQUEsSUFFWixTQUFTO0FBQUEsTUFDUCxnQkFBZ0IsQ0FBRSxRQUFRLGFBQWE7QUFBQSxNQUN2QyxtQkFBbUIsQ0FBRSxRQUFRLGFBQWE7QUFBQSxNQUMxQyxjQUFjLENBQUUsUUFBUSxhQUFhO0FBQUEsTUFDckMsMkJBQTJCLENBQUUsUUFBUSxhQUFhO0FBQUEsTUFDbEQ7QUFBQTtBQUFBLElBRUYsV0FBVyxjQUFjLFlBQVk7QUFBQSxJQUNyQyxNQUFNLGNBQWMsWUFB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