| import { t as getErrorMessage$1 } from "./utils.js"; |
| import { extname } from "node:path"; |
| import { fileURLToPath, pathToFileURL } from "node:url"; |
| |
| const ArrayIsArray$1 = Array.isArray, isObject$1 = (v) => typeof v == "object" && !!v && !ArrayIsArray$1(v), TS_MODULE_EXTENSIONS = new Set([ |
| ".ts", |
| ".mts", |
| ".cts" |
| ]); |
| function normalizeModuleSpecifierPath(specifier) { |
| if (!specifier.startsWith("file:")) return specifier; |
| try { |
| return fileURLToPath(specifier); |
| } catch { |
| return specifier; |
| } |
| } |
| function isTypeScriptModuleSpecifier(specifier) { |
| let ext = extname(normalizeModuleSpecifierPath(specifier)).toLowerCase(); |
| return TS_MODULE_EXTENSIONS.has(ext); |
| } |
| function isUnknownFileExtensionError(err) { |
| if (err?.code === "ERR_UNKNOWN_FILE_EXTENSION") return !0; |
| let message = err?.message; |
| return typeof message == "string" && /unknown(?: or unsupported)? file extension/i.test(message); |
| } |
| function getErrorMessage(err) { |
| return err instanceof Error ? err.message : String(err); |
| } |
| |
| |
| |
| |
| |
| function getUnsupportedTypeScriptModuleLoadHintForError(err, specifier, nodeVersion = process.version) { |
| return !isTypeScriptModuleSpecifier(specifier) || !isUnknownFileExtensionError(err) ? null : `${getErrorMessage(err)}\n\nTypeScript config files require Node.js ^20.19.0 || >=22.18.0.\nDetected Node.js ${nodeVersion}.\nPlease upgrade Node.js or use a JSON config file instead.`; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function importJsConfig(path, cacheKey) { |
| let fileUrl = pathToFileURL(path); |
| fileUrl.searchParams.set("cache", cacheKey.toString()); |
| let module; |
| try { |
| module = await import(fileUrl.href); |
| } catch (err) { |
| let hint = getUnsupportedTypeScriptModuleLoadHintForError(err, path); |
| throw hint ? Error(hint, { cause: err }) : err; |
| } |
| if (module.default === void 0) throw Error("Configuration file has no default export."); |
| if (!isObject$1(module.default)) throw Error("Configuration file must have a default export that is an object."); |
| return module.default; |
| } |
| |
| |
| let vitePlusCache = null; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async function loadViteConfigField(path, fieldName) { |
| vitePlusCache ??= import("vite-plus"); |
| let { resolveConfig } = await vitePlusCache, config = await resolveConfig({ configFile: path }, "build"); |
| if (!(fieldName in config)) return null; |
| let fieldValue = config[fieldName]; |
| if (!isObject$1(fieldValue)) throw Error(`The \`${fieldName}\` field in the default export must be an object.`); |
| return fieldValue; |
| } |
| |
| |
| const ArrayIsArray = Array.isArray, JSONStringify = JSON.stringify, DateNow = Date.now, isObject = (v) => typeof v == "object" && !!v && !ArrayIsArray(v); |
| function validateConfigExtends(root) { |
| let visited = new WeakSet(), inStack = new WeakSet(), stackObjects = [], stackPaths = [], formatCycleError = (refPath, cycleStart, idx) => `\`extends\` contains a circular reference. |
| |
| ${refPath} points back to ${cycleStart}\nCycle: ${idx === -1 ? `${cycleStart} -> ${cycleStart}` : [...stackPaths.slice(idx), cycleStart].join(" -> ")}`, visit = (config, path) => { |
| if (visited.has(config)) return; |
| if (inStack.has(config)) { |
| let idx = stackObjects.indexOf(config), cycleStart = idx === -1 ? "<unknown>" : stackPaths[idx]; |
| throw Error(formatCycleError(path, cycleStart, idx)); |
| } |
| inStack.add(config), stackObjects.push(config), stackPaths.push(path); |
| let maybeExtends = config.extends; |
| if (maybeExtends !== void 0) { |
| if (!ArrayIsArray(maybeExtends)) throw Error("`extends` must be an array of config objects (strings/paths are not supported)."); |
| for (let i = 0; i < maybeExtends.length; i++) { |
| let item = maybeExtends[i]; |
| if (!isObject(item)) throw Error(`\`extends[${i}]\` must be a config object (strings/paths are not supported).`); |
| let itemPath = `${path}.extends[${i}]`; |
| if (inStack.has(item)) { |
| let idx = stackObjects.indexOf(item), cycleStart = idx === -1 ? "<unknown>" : stackPaths[idx]; |
| throw Error(formatCycleError(itemPath, cycleStart, idx)); |
| } |
| visit(item, itemPath); |
| } |
| } |
| inStack.delete(config), stackObjects.pop(), stackPaths.pop(), visited.add(config); |
| }; |
| visit(root, "<root>"); |
| } |
| |
| |
| |
| |
| async function resolveJsConfig(path, cacheKey) { |
| let config = await importJsConfig(path, cacheKey); |
| return validateConfigExtends(config), { |
| path, |
| config |
| }; |
| } |
| |
| |
| |
| |
| async function resolveVitePlusConfig(path) { |
| let lintConfig = await loadViteConfigField(path, "lint"); |
| return lintConfig === null ? { |
| path, |
| config: null |
| } : (validateConfigExtends(lintConfig), { |
| path, |
| config: lintConfig |
| }); |
| } |
| |
| |
| |
| async function loadConfigs(paths, resolver) { |
| try { |
| let results = await Promise.allSettled(paths.map(resolver)), successes = [], errors = []; |
| for (let i = 0; i < results.length; i++) { |
| let result = results[i]; |
| result.status === "fulfilled" ? successes.push(result.value) : errors.push({ |
| path: paths[i], |
| error: getErrorMessage$1(result.reason) |
| }); |
| } |
| return errors.length > 0 ? JSONStringify({ Failures: errors }) : JSONStringify({ Success: successes }); |
| } catch (err) { |
| return JSONStringify({ Error: getErrorMessage$1(err) }); |
| } |
| } |
| |
| |
| |
| const loadJsConfigs = (paths) => { |
| let cacheKey = DateNow(); |
| return loadConfigs(paths, (path) => resolveJsConfig(path, cacheKey)); |
| }, loadVitePlusConfigs = (paths) => loadConfigs(paths, resolveVitePlusConfig); |
| |
| export { loadJsConfigs, loadVitePlusConfigs }; |
|
|