'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const cli = require('@graphql-codegen/cli'); const vite = require('vite'); async function getDocumentPaths(context) { const config = context.getConfig(); const sourceDocuments = Object.values(config.generates).map( (output) => Array.isArray(output) ? void 0 : output.documents ); if (config.documents) { sourceDocuments.unshift(config.documents); } const normalized = sourceDocuments.filter((item) => !!item).flat(); if (!normalized.length) return []; const documents = await context.loadDocuments(normalized); if (!documents.length) return []; return documents.map(({ location = "" }) => location).filter(Boolean).map(vite.normalizePath); } async function getSchemaPaths(context) { const config = context.getConfig(); const sourceSchemas = Object.values(config.generates).map( (output) => Array.isArray(output) ? void 0 : output.schema ); if (config.schema) { sourceSchemas.unshift(config.schema); } const normalized = sourceSchemas.filter((item) => !!item).flat(); if (!normalized.length) return []; const schemas = await context.loadSchema( // loadSchema supports array of string, but typings are wrong normalized ); return schemas.extensions.sources.map(({ name = "" }) => name).filter(Boolean).map(vite.normalizePath); } const isCodegenConfig = async (filePath, context) => { if (!context.filepath) return false; return vite.normalizePath(filePath) === vite.normalizePath(context.filepath); }; const isGraphQLDocument = async (filePath, context) => { const documentPaths = await getDocumentPaths(context); if (!documentPaths.length) return false; const normalizedFilePath = vite.normalizePath(filePath); return documentPaths.some((path) => normalizedFilePath.includes(path)); }; const isGraphQLSchema = async (filePath, context) => { const schemaPaths = await getSchemaPaths(context); if (!schemaPaths.length) return false; const normalizedFilePath = vite.normalizePath(filePath); return schemaPaths.some((path) => normalizedFilePath.includes(path)); }; const modes = { serve: "serve", build: "build" }; const { serve, build } = modes; function isServeMode(mode) { return mode === serve; } function isBuildMode(mode) { return mode === build; } const RESET = "\x1B[0m"; const BRIGHT = "\x1B[1m"; const DIM = "\x1B[2m"; const FG_CYAN = "\x1B[36m"; function debugLog(...args) { const LOG_PREFIX = `${FG_CYAN}${BRIGHT}VITE PLUGIN GRAPHQL CODEGEN${RESET} `; console.log(LOG_PREFIX, DIM, ...args, RESET); } function GraphQLCodegen(options) { let codegenContext; let viteMode; const { runOnStart = true, runOnBuild = true, enableWatcher = true, throwOnStart = false, throwOnBuild = true, matchOnDocuments = true, matchOnSchemas = false, config = null, configOverride = {}, configOverrideOnStart = {}, configOverrideOnBuild = {}, configOverrideWatcher = {}, configFilePathOverride, debug = false } = options ?? {}; const log = (...args) => { if (!debug) return; debugLog(...args); }; const generateWithOverride = async (overrideConfig) => { const currentConfig = codegenContext.getConfig(); return cli.generate({ ...currentConfig, ...configOverride, ...overrideConfig, // Vite handles file watching watch: false }); }; if (options) log("Plugin initialized with options:", options); return { name: "graphql-codegen", async config(_config, env) { try { if (config) { log("Manual config passed, creating codegen context"); codegenContext = new cli.CodegenContext({ config }); } else { const cwd = process.cwd(); log("Loading codegen context:", configFilePathOverride ?? cwd); codegenContext = await cli.loadContext(configFilePathOverride); } log("Loading codegen context successful"); } catch (error) { log("Loading codegen context failed"); throw error; } viteMode = env.command; }, async buildStart() { if (isServeMode(viteMode)) { if (!runOnStart) return; try { await generateWithOverride(configOverrideOnStart); log("Generation successful on start"); } catch (error) { log("Generation failed on start"); if (throwOnStart) throw error; } } if (isBuildMode(viteMode)) { if (!runOnBuild) return; try { await generateWithOverride(configOverrideOnBuild); log("Generation successful on build"); } catch (error) { log("Generation failed on build"); if (throwOnBuild) throw error; } } }, configureServer(server) { if (!enableWatcher) return; const listener = async (filePath = "") => { log("File changed:", filePath); const isConfig = await isCodegenConfig(filePath, codegenContext); if (isConfig) { log("Codegen config file changed, restarting vite"); server.restart(); return; } const matchers = [ [matchOnDocuments, "document", isGraphQLDocument], [matchOnSchemas, "schema", isGraphQLSchema] ]; const matcherResults = await Promise.all( matchers.map(async ([enabled, name, matcher]) => { if (!enabled) { log(`Check for ${name} file skipped in file watcher by config`); return false; } try { const isMatch2 = await matcher(filePath, codegenContext); log(`Check for ${name} file successful in file watcher`); if (isMatch2) log(`File matched a graphql ${name}`); else log(`File did not match a graphql ${name}`); return isMatch2; } catch (error) { log(`Check for ${name} file failed in file watcher`); return false; } }) ); const isMatch = matcherResults.some((result) => result); if (!isMatch) return; try { await generateWithOverride(configOverrideWatcher); log("Generation successful in file watcher"); } catch (error) { log("Generation failed in file watcher"); } }; server.watcher.on("add", listener); server.watcher.on("change", listener); } }; } exports.GraphQLCodegen = GraphQLCodegen; exports.default = GraphQLCodegen;