@reasonabletech/config-vitest 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -0
- package/dist/src/global-setup.d.ts +15 -0
- package/dist/src/global-setup.d.ts.map +1 -0
- package/dist/src/index.d.ts +63 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +352 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/node.d.ts +29 -0
- package/dist/src/node.d.ts.map +1 -0
- package/dist/src/node.js +209 -0
- package/dist/src/node.js.map +1 -0
- package/dist/src/react.d.ts +46 -0
- package/dist/src/react.d.ts.map +1 -0
- package/dist/src/react.js +225 -0
- package/dist/src/react.js.map +1 -0
- package/dist/src/workspace.d.ts +29 -0
- package/dist/src/workspace.d.ts.map +1 -0
- package/dist/src/workspace.js +33 -0
- package/dist/src/workspace.js.map +1 -0
- package/docs/README.md +28 -0
- package/docs/api/api-reference.md +469 -0
- package/docs/api/base-config.md +542 -0
- package/docs/guides/migration.md +47 -0
- package/docs/guides/usage-guide.md +81 -0
- package/package.json +100 -0
- package/src/global-setup.ts +58 -0
- package/src/index.ts +280 -0
- package/src/node.ts +74 -0
- package/src/react.ts +241 -0
- package/src/workspace.ts +62 -0
package/dist/src/node.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { existsSync as existsSync2 } from "fs";
|
|
3
|
+
import { join as join2 } from "path";
|
|
4
|
+
import { defaultClientConditions, defaultServerConditions } from "vite";
|
|
5
|
+
import { defineConfig } from "vitest/config";
|
|
6
|
+
|
|
7
|
+
// src/workspace.ts
|
|
8
|
+
import { existsSync, readFileSync } from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
function readPackageName(packageDir) {
|
|
11
|
+
const packageJsonPath = path.join(packageDir, "package.json");
|
|
12
|
+
if (!existsSync(packageJsonPath)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const parsed = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
17
|
+
return typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : null;
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var EMPTY_CONFIG = {};
|
|
25
|
+
var baseConfig = {
|
|
26
|
+
test: {
|
|
27
|
+
testTimeout: 1e4,
|
|
28
|
+
// 10 seconds default timeout
|
|
29
|
+
hookTimeout: 1e4,
|
|
30
|
+
// 10 seconds for setup/teardown hooks
|
|
31
|
+
coverage: {
|
|
32
|
+
provider: "v8",
|
|
33
|
+
reporter: ["text", "html", "lcov", "json"],
|
|
34
|
+
reportsDirectory: "./generated/test-coverage",
|
|
35
|
+
exclude: [
|
|
36
|
+
"**/node_modules/**",
|
|
37
|
+
"**/dist/**",
|
|
38
|
+
"**/tests/**",
|
|
39
|
+
"**/*.d.ts",
|
|
40
|
+
"**/*.config.{js,ts,mjs,mts}",
|
|
41
|
+
"**/coverage/**",
|
|
42
|
+
"**/examples/**",
|
|
43
|
+
"**/src/index.ts",
|
|
44
|
+
"**/src/*/index.ts",
|
|
45
|
+
"**/src/types/**",
|
|
46
|
+
"tsup.config.ts",
|
|
47
|
+
"vitest.config.mts",
|
|
48
|
+
"tailwind.config.mjs",
|
|
49
|
+
"**/vitest.setup.ts"
|
|
50
|
+
],
|
|
51
|
+
thresholds: process.env.VITEST_COVERAGE_THRESHOLDS_DISABLED === "true" ? {
|
|
52
|
+
lines: 0,
|
|
53
|
+
functions: 0,
|
|
54
|
+
branches: 0,
|
|
55
|
+
statements: 0
|
|
56
|
+
} : {
|
|
57
|
+
lines: 100,
|
|
58
|
+
functions: 100,
|
|
59
|
+
branches: 100,
|
|
60
|
+
statements: 100
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
function generateSelfPackageAliases(projectDir) {
|
|
66
|
+
const packageName = readPackageName(projectDir);
|
|
67
|
+
if (packageName === null) {
|
|
68
|
+
return {};
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
[packageName]: `${projectDir}/src`
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function autoDetectSetupFiles(projectDir) {
|
|
75
|
+
if (projectDir === void 0 || projectDir === "") {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const vitestSetupPath = join2(projectDir, "vitest.setup.ts");
|
|
79
|
+
if (existsSync2(vitestSetupPath)) {
|
|
80
|
+
return ["./vitest.setup.ts"];
|
|
81
|
+
}
|
|
82
|
+
const testsSetupPath = join2(projectDir, "tests/setup.ts");
|
|
83
|
+
if (existsSync2(testsSetupPath)) {
|
|
84
|
+
return ["./tests/setup.ts"];
|
|
85
|
+
}
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
function autoDetectIncludePatterns() {
|
|
89
|
+
return ["tests/**/*.test.{ts,tsx,js,jsx}"];
|
|
90
|
+
}
|
|
91
|
+
function createVitestConfig(projectDirOrConfig, customConfig) {
|
|
92
|
+
let projectDir;
|
|
93
|
+
let config;
|
|
94
|
+
if (typeof projectDirOrConfig === "string") {
|
|
95
|
+
projectDir = projectDirOrConfig;
|
|
96
|
+
config = customConfig ?? EMPTY_CONFIG;
|
|
97
|
+
} else {
|
|
98
|
+
projectDir = void 0;
|
|
99
|
+
config = projectDirOrConfig ?? EMPTY_CONFIG;
|
|
100
|
+
}
|
|
101
|
+
const autoSetupFiles = config.test?.setupFiles !== void 0 && config.test.setupFiles.length > 0 ? [] : autoDetectSetupFiles(projectDir);
|
|
102
|
+
const autoIncludePatterns = config.test?.include !== void 0 && config.test.include.length > 0 ? [] : autoDetectIncludePatterns();
|
|
103
|
+
return defineConfig({
|
|
104
|
+
...baseConfig,
|
|
105
|
+
...config,
|
|
106
|
+
test: {
|
|
107
|
+
...baseConfig.test,
|
|
108
|
+
// Auto-detect setupFiles if not explicitly provided
|
|
109
|
+
...autoSetupFiles.length > 0 && { setupFiles: autoSetupFiles },
|
|
110
|
+
// Auto-detect include patterns if not explicitly provided
|
|
111
|
+
...autoIncludePatterns.length > 0 && { include: autoIncludePatterns },
|
|
112
|
+
...config.test,
|
|
113
|
+
coverage: {
|
|
114
|
+
...baseConfig.test.coverage,
|
|
115
|
+
...config.test?.coverage
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
resolve: {
|
|
119
|
+
// Prefer "source" condition in package.json exports, allowing
|
|
120
|
+
// Vitest to resolve workspace dependencies directly to TypeScript source
|
|
121
|
+
// files without requiring a prior build step.
|
|
122
|
+
//
|
|
123
|
+
// Since Vite 6, resolve.conditions REPLACES the defaults instead of
|
|
124
|
+
// extending them. We must include defaultClientConditions to preserve
|
|
125
|
+
// standard conditions like "module", "browser", "development|production".
|
|
126
|
+
conditions: ["source", ...defaultClientConditions],
|
|
127
|
+
alias: {
|
|
128
|
+
// Work around packages whose "module" entry is bundler-oriented but not
|
|
129
|
+
// directly Node.js ESM-resolvable in Vitest (e.g. extensionless internal
|
|
130
|
+
// specifiers). Prefer the Node/CJS build for tests.
|
|
131
|
+
"@opentelemetry/api": "@opentelemetry/api/build/src/index.js",
|
|
132
|
+
// Auto-generate self-package alias (e.g. "@reasonabletech/utils" → "./src")
|
|
133
|
+
...projectDir !== void 0 && projectDir !== "" && generateSelfPackageAliases(projectDir),
|
|
134
|
+
// Standard "@" → src alias
|
|
135
|
+
...projectDir !== void 0 && projectDir !== "" && { "@": `${projectDir}/src` },
|
|
136
|
+
// Consumer-provided aliases override everything above
|
|
137
|
+
...config.resolve?.alias
|
|
138
|
+
},
|
|
139
|
+
...config.resolve
|
|
140
|
+
},
|
|
141
|
+
// Vitest runs in Vite's SSR mode. Since Vite 6, ssr.resolve.conditions is
|
|
142
|
+
// independent from resolve.conditions and defaults to defaultServerConditions.
|
|
143
|
+
// We must explicitly include "source" in ssr.resolve.conditions so
|
|
144
|
+
// workspace dependencies resolve to TypeScript source during test execution.
|
|
145
|
+
// Additionally, ssr.resolve.externalConditions passes "source" to
|
|
146
|
+
// Node.js when it natively resolves externalized packages.
|
|
147
|
+
//
|
|
148
|
+
// Note: We intentionally omit the "module" export condition for SSR tests.
|
|
149
|
+
// Some third-party packages expose a "module" build that is bundler-friendly
|
|
150
|
+
// but not directly Node.js ESM-resolvable (for example: extensionless internal
|
|
151
|
+
// specifiers). For tests, preferring Node-compatible ("node"/"default") entry
|
|
152
|
+
// points avoids runtime import failures.
|
|
153
|
+
ssr: {
|
|
154
|
+
resolve: {
|
|
155
|
+
conditions: [
|
|
156
|
+
"source",
|
|
157
|
+
...defaultServerConditions.filter(
|
|
158
|
+
(condition) => condition !== "module"
|
|
159
|
+
)
|
|
160
|
+
],
|
|
161
|
+
externalConditions: ["source"]
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/node.ts
|
|
168
|
+
var EMPTY_CONFIG2 = {};
|
|
169
|
+
var nodeConfig = {
|
|
170
|
+
test: {
|
|
171
|
+
environment: "node",
|
|
172
|
+
include: ["tests/**/*.test.ts"]
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
function createNodeConfig(projectDirOrConfig, customConfig) {
|
|
176
|
+
let projectDir;
|
|
177
|
+
let config;
|
|
178
|
+
if (typeof projectDirOrConfig === "string") {
|
|
179
|
+
projectDir = projectDirOrConfig;
|
|
180
|
+
config = customConfig ?? EMPTY_CONFIG2;
|
|
181
|
+
} else {
|
|
182
|
+
projectDir = void 0;
|
|
183
|
+
config = projectDirOrConfig ?? EMPTY_CONFIG2;
|
|
184
|
+
}
|
|
185
|
+
const mergedConfig = {
|
|
186
|
+
...nodeConfig,
|
|
187
|
+
...config,
|
|
188
|
+
test: {
|
|
189
|
+
...nodeConfig.test,
|
|
190
|
+
...config.test
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
if (projectDir !== void 0) {
|
|
194
|
+
return createVitestConfig(
|
|
195
|
+
projectDir,
|
|
196
|
+
mergedConfig
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
return createVitestConfig(
|
|
200
|
+
mergedConfig
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
var node_default = createNodeConfig;
|
|
204
|
+
export {
|
|
205
|
+
createNodeConfig,
|
|
206
|
+
node_default as default,
|
|
207
|
+
nodeConfig
|
|
208
|
+
};
|
|
209
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/workspace.ts","../../src/node.ts"],"sourcesContent":["/**\n * Base Vitest configuration for all packages\n * @module @reasonabletech/config-vitest\n */\n\nimport { existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { defaultClientConditions, defaultServerConditions } from \"vite\";\nimport { defineConfig } from \"vitest/config\";\nimport type { InlineConfig } from \"vitest/node\";\nimport { readPackageName } from \"./workspace.js\";\n\n/** Recursively makes all properties of T readonly */\nexport type DeepReadonly<T> = {\n readonly [P in keyof T]: T[P] extends ReadonlyArray<infer U>\n ? ReadonlyArray<DeepReadonly<U>>\n : T[P] extends Array<infer U>\n ? ReadonlyArray<DeepReadonly<U>>\n : T[P] extends object\n ? DeepReadonly<T[P]>\n : T[P];\n};\n\n/**\n * Immutable configuration object accepted by {@link createVitestConfig} and\n * related helpers.\n */\nexport type VitestConfig = DeepReadonly<{\n test?: InlineConfig;\n resolve?: {\n conditions?: string[];\n alias?: Record<string, string>;\n [key: string]: unknown;\n };\n [key: string]: unknown;\n}>;\n\n// Empty readonly config for default parameters\nconst EMPTY_CONFIG = {} as const satisfies VitestConfig;\n\n/**\n * Base configuration options that apply to all test environments\n */\nexport const baseConfig = {\n test: {\n testTimeout: 10000, // 10 seconds default timeout\n hookTimeout: 10000, // 10 seconds for setup/teardown hooks\n coverage: {\n provider: \"v8\" as const,\n reporter: [\"text\", \"html\", \"lcov\", \"json\"],\n reportsDirectory: \"./generated/test-coverage\",\n exclude: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/tests/**\",\n \"**/*.d.ts\",\n \"**/*.config.{js,ts,mjs,mts}\",\n \"**/coverage/**\",\n \"**/examples/**\",\n \"**/src/index.ts\",\n \"**/src/*/index.ts\",\n \"**/src/types/**\",\n \"tsup.config.ts\",\n \"vitest.config.mts\",\n \"tailwind.config.mjs\",\n \"**/vitest.setup.ts\",\n ],\n thresholds:\n process.env.VITEST_COVERAGE_THRESHOLDS_DISABLED === \"true\"\n ? {\n lines: 0,\n functions: 0,\n branches: 0,\n statements: 0,\n }\n : {\n lines: 100,\n functions: 100,\n branches: 100,\n statements: 100,\n },\n },\n },\n};\n\n/**\n * Generates resolve aliases that map a package's own name back to its source\n * directory. This allows test files to `import { foo } from \"@reasonabletech/my-pkg\"`\n * and have Vite resolve it to the local `src/` tree instead of requiring a prior\n * build step.\n *\n * Placed before user-supplied aliases so consumers can override if needed.\n * @param projectDir - Absolute path to the project directory\n * @returns A record mapping the package name to its `src/` directory\n */\nfunction generateSelfPackageAliases(\n projectDir: string,\n): Record<string, string> {\n const packageName = readPackageName(projectDir);\n if (packageName === null) {\n return {};\n }\n return {\n [packageName]: `${projectDir}/src`,\n };\n}\n\n/**\n * Auto-detects setup files in a project directory\n * @param projectDir - The project directory path\n * @returns Array of detected setup file paths\n */\nfunction autoDetectSetupFiles(projectDir?: string): string[] {\n if (projectDir === undefined || projectDir === \"\") {\n return [];\n }\n\n const vitestSetupPath = join(projectDir, \"vitest.setup.ts\");\n if (existsSync(vitestSetupPath)) {\n return [\"./vitest.setup.ts\"];\n }\n\n const testsSetupPath = join(projectDir, \"tests/setup.ts\");\n if (existsSync(testsSetupPath)) {\n return [\"./tests/setup.ts\"];\n }\n\n return [];\n}\n\n/**\n * Auto-detects test include patterns based on project structure\n * @returns Array of include patterns\n */\nfunction autoDetectIncludePatterns(): string[] {\n return [\"tests/**/*.test.{ts,tsx,js,jsx}\"];\n}\n\n/** @overload */\n/**\n * Creates a merged configuration from the base and any custom options.\n * @param projectDirOrConfig - Either the absolute project directory (use `import.meta.dirname`) or a prebuilt config.\n * @param customConfig - Additional configuration to merge when a project directory is provided.\n * @returns A merged Vitest configuration tailored for the caller.\n */\nexport function createVitestConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n // Auto-detect configuration if not explicitly provided\n const autoSetupFiles =\n config.test?.setupFiles !== undefined && config.test.setupFiles.length > 0\n ? []\n : autoDetectSetupFiles(projectDir);\n const autoIncludePatterns =\n config.test?.include !== undefined && config.test.include.length > 0\n ? []\n : autoDetectIncludePatterns();\n\n return defineConfig({\n ...baseConfig,\n ...config,\n test: {\n ...baseConfig.test,\n // Auto-detect setupFiles if not explicitly provided\n ...(autoSetupFiles.length > 0 && { setupFiles: autoSetupFiles }),\n // Auto-detect include patterns if not explicitly provided\n ...(autoIncludePatterns.length > 0 && { include: autoIncludePatterns }),\n ...config.test,\n coverage: {\n ...baseConfig.test.coverage,\n ...config.test?.coverage,\n },\n },\n resolve: {\n // Prefer \"source\" condition in package.json exports, allowing\n // Vitest to resolve workspace dependencies directly to TypeScript source\n // files without requiring a prior build step.\n //\n // Since Vite 6, resolve.conditions REPLACES the defaults instead of\n // extending them. We must include defaultClientConditions to preserve\n // standard conditions like \"module\", \"browser\", \"development|production\".\n conditions: [\"source\", ...defaultClientConditions],\n alias: {\n // Work around packages whose \"module\" entry is bundler-oriented but not\n // directly Node.js ESM-resolvable in Vitest (e.g. extensionless internal\n // specifiers). Prefer the Node/CJS build for tests.\n \"@opentelemetry/api\": \"@opentelemetry/api/build/src/index.js\",\n // Auto-generate self-package alias (e.g. \"@reasonabletech/utils\" → \"./src\")\n ...(projectDir !== undefined &&\n projectDir !== \"\" &&\n generateSelfPackageAliases(projectDir)),\n // Standard \"@\" → src alias\n ...(projectDir !== undefined &&\n projectDir !== \"\" && { \"@\": `${projectDir}/src` }),\n // Consumer-provided aliases override everything above\n ...config.resolve?.alias,\n },\n ...config.resolve,\n },\n // Vitest runs in Vite's SSR mode. Since Vite 6, ssr.resolve.conditions is\n // independent from resolve.conditions and defaults to defaultServerConditions.\n // We must explicitly include \"source\" in ssr.resolve.conditions so\n // workspace dependencies resolve to TypeScript source during test execution.\n // Additionally, ssr.resolve.externalConditions passes \"source\" to\n // Node.js when it natively resolves externalized packages.\n //\n // Note: We intentionally omit the \"module\" export condition for SSR tests.\n // Some third-party packages expose a \"module\" build that is bundler-friendly\n // but not directly Node.js ESM-resolvable (for example: extensionless internal\n // specifiers). For tests, preferring Node-compatible (\"node\"/\"default\") entry\n // points avoids runtime import failures.\n ssr: {\n resolve: {\n conditions: [\n \"source\",\n ...defaultServerConditions.filter(\n (condition) => condition !== \"module\",\n ),\n ],\n externalConditions: [\"source\"],\n },\n },\n });\n}\n\n/** @overload */\n/**\n * Creates a configuration with extended timeouts for long-running tests.\n * @param projectDirOrConfig - Either the absolute project directory or an existing configuration to extend.\n * @param customConfig - Additional configuration to merge when a project directory is supplied.\n * @returns A Vitest configuration suited for long-running suites.\n */\nexport function createLongRunningTestConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n const longRunningConfig: VitestConfig = {\n ...config,\n test: {\n testTimeout: 30000, // 30 seconds for long-running tests\n hookTimeout: 30000, // 30 seconds for setup/teardown hooks\n ...config.test,\n },\n };\n\n if (projectDir !== undefined) {\n return createVitestConfig(projectDir, longRunningConfig);\n }\n return createVitestConfig(longRunningConfig);\n}\n\n// Re-export for convenience\nexport { createReactConfig, createReactConfigWithPlugins } from \"./react.js\";\n\nexport default createVitestConfig;\n","/**\n * Workspace utility functions for discovering monorepo structure\n *\n * These are extracted from global-setup.ts so they can be reused by\n * the Vitest config factories (e.g. auto self-package aliasing).\n * @module @reasonabletech/config-vitest/workspace\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport * as path from \"node:path\";\n\n/**\n * Walks up the directory tree from `startDir` until it finds a directory\n * containing `pnpm-workspace.yaml`, which marks the monorepo root.\n *\n * Returns `startDir` unchanged if no workspace root is found (e.g. when\n * running outside the monorepo).\n * @param startDir - The directory to start searching from\n * @returns The absolute path to the monorepo root, or `startDir` if not found\n */\nexport function findRepoRoot(startDir: string): string {\n let currentDir = startDir;\n for (;;) {\n if (existsSync(path.join(currentDir, \"pnpm-workspace.yaml\"))) {\n return currentDir;\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return startDir;\n }\n currentDir = parentDir;\n }\n}\n\n/**\n * Reads the `name` field from the `package.json` in the given directory.\n *\n * Returns `null` when:\n * - No `package.json` exists at `packageDir`\n * - The file cannot be parsed as JSON\n * - The `name` field is missing, empty, or not a string\n * @param packageDir - The directory containing the package.json to read\n * @returns The package name string, or `null` if unavailable\n */\nexport function readPackageName(packageDir: string): string | null {\n const packageJsonPath = path.join(packageDir, \"package.json\");\n if (!existsSync(packageJsonPath)) {\n return null;\n }\n\n try {\n const parsed = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as {\n name?: unknown;\n };\n return typeof parsed.name === \"string\" && parsed.name.length > 0\n ? parsed.name\n : null;\n } catch {\n return null;\n }\n}\n","/**\n * Node.js-specific Vitest configuration\n * @module @reasonabletech/config-vitest/node\n */\n\nimport type { InlineConfig } from \"vitest/node\";\nimport type { UserConfig } from \"vite\";\nimport { createVitestConfig, type DeepReadonly } from \"./index.js\";\n\n/** Immutable Vitest configuration type combining Vite UserConfig with Vitest InlineConfig */\nexport type VitestConfig = DeepReadonly<UserConfig> & {\n readonly test?: DeepReadonly<InlineConfig>;\n};\n\n// Empty readonly config for default parameters\nconst EMPTY_CONFIG = {} as const satisfies VitestConfig;\n\n/**\n * Node.js-specific configuration options\n */\nexport const nodeConfig = {\n test: {\n environment: \"node\" as const,\n include: [\"tests/**/*.test.ts\"],\n },\n} as const satisfies VitestConfig;\n\n/**\n * Creates a Vitest configuration for Node.js-based packages.\n * @param projectDirOrConfig - Either the absolute project directory (use `import.meta.dirname`) or a prebuilt config.\n * @param customConfig - Additional configuration to merge when a project directory is provided.\n * @returns A Vitest configuration optimized for Node.js environments\n */\nexport function createNodeConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof createVitestConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n // Merge node-specific settings with consumer config\n const mergedConfig = {\n ...nodeConfig,\n ...config,\n test: {\n ...nodeConfig.test,\n ...config.test,\n },\n };\n\n // Delegate to createVitestConfig which handles aliasing and base config.\n // When projectDir is present, pass both args so createVitestConfig generates aliases.\n // When absent, pass the merged config as the sole argument.\n if (projectDir !== undefined) {\n return createVitestConfig(\n projectDir,\n mergedConfig as Parameters<typeof createVitestConfig>[1],\n );\n }\n return createVitestConfig(\n mergedConfig as Parameters<typeof createVitestConfig>[0],\n );\n}\n\nexport default createNodeConfig;\n"],"mappings":";AAKA,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,QAAAC,aAAY;AACrB,SAAS,yBAAyB,+BAA+B;AACjE,SAAS,oBAAoB;;;ACA7B,SAA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I,eAAe,UACjB,eAAe,MACf,2BAA2B,UAAU;AAAA;AAAA,QAEvC,GAAI,eAAe,UACjB,eAAe,MAAM,EAAE,KAAK,GAAG,UAAU,OAAO;AAAA;AAAA,QAElD,GAAG,OAAO,SAAS;AAAA,MACrB;AAAA,MACA,GAAG,OAAO;AAAA,IACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAaA,KAAK;AAAA,MACH,SAAS;AAAA,QACP,YAAY;AAAA,UACV;AAAA,UACA,GAAG,wBAAwB;AAAA,YACzB,CAAC,cAAc,cAAc;AAAA,UAC/B;AAAA,QACF;AAAA,QACA,oBAAoB,CAAC,QAAQ;AAAA,MAC/B;AAAA,IACF;AAAA,EACF,CAAC;AACH;;;AE7NA,IAAMC,gBAAe,CAAC;AAKf,IAAM,aAAa;AAAA,EACxB,MAAM;AAAA,IACJ,aAAa;AAAA,IACb,SAAS,CAAC,oBAAoB;AAAA,EAChC;AACF;AAQO,SAAS,iBACd,oBACA,cACuC;AAEvC,MAAI;AACJ,MAAI;AAEJ,MAAI,OAAO,uBAAuB,UAAU;AAC1C,iBAAa;AACb,aAAS,gBAAgBA;AAAA,EAC3B,OAAO;AACL,iBAAa;AACb,aAAS,sBAAsBA;AAAA,EACjC;AAGA,QAAM,eAAe;AAAA,IACnB,GAAG;AAAA,IACH,GAAG;AAAA,IACH,MAAM;AAAA,MACJ,GAAG,WAAW;AAAA,MACd,GAAG,OAAO;AAAA,IACZ;AAAA,EACF;AAKA,MAAI,eAAe,QAAW;AAC5B,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACA,SAAO;AAAA,IACL;AAAA,EACF;AACF;AAEA,IAAO,eAAQ;","names":["existsSync","join","join","existsSync","EMPTY_CONFIG"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React-specific Vitest configuration
|
|
3
|
+
* @module @reasonabletech/config-vitest/react
|
|
4
|
+
*/
|
|
5
|
+
import { type PluginOption } from "vite";
|
|
6
|
+
import { defineConfig } from "vitest/config";
|
|
7
|
+
import type { InlineConfig } from "vitest/node";
|
|
8
|
+
import { type DeepReadonly } from "./index.js";
|
|
9
|
+
type VitestConfig = DeepReadonly<{
|
|
10
|
+
test?: InlineConfig;
|
|
11
|
+
resolve?: {
|
|
12
|
+
alias?: Record<string, string>;
|
|
13
|
+
[key: string]: unknown;
|
|
14
|
+
};
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* React-specific configuration options
|
|
19
|
+
*/
|
|
20
|
+
export declare const reactConfig: {
|
|
21
|
+
test: {
|
|
22
|
+
environment: string;
|
|
23
|
+
exclude: string[];
|
|
24
|
+
silent: boolean;
|
|
25
|
+
onConsoleLog: () => boolean | undefined;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
/** @overload */
|
|
29
|
+
/**
|
|
30
|
+
* Creates a Vitest configuration for React-based packages.
|
|
31
|
+
* @param projectDirOrConfig - Either the absolute project directory or an existing configuration to merge.
|
|
32
|
+
* @param customConfig - Additional configuration options for the project-dir overload.
|
|
33
|
+
* @returns A Vitest configuration optimized for React components.
|
|
34
|
+
*/
|
|
35
|
+
export declare function createReactConfig(projectDirOrConfig?: string | VitestConfig, customConfig?: VitestConfig): ReturnType<typeof defineConfig>;
|
|
36
|
+
/** @overload */
|
|
37
|
+
/**
|
|
38
|
+
* Creates a Vitest configuration for React-based packages with plugins.
|
|
39
|
+
* @param plugins - Array of Vite plugins to include.
|
|
40
|
+
* @param projectDirOrConfig - Either the absolute project directory or an existing configuration to merge.
|
|
41
|
+
* @param customConfig - Additional configuration options for the project-dir overload.
|
|
42
|
+
* @returns A Vitest configuration optimized for React components with plugins.
|
|
43
|
+
*/
|
|
44
|
+
export declare function createReactConfigWithPlugins(plugins?: readonly PluginOption[], projectDirOrConfig?: string | VitestConfig, customConfig?: VitestConfig): ReturnType<typeof defineConfig>;
|
|
45
|
+
export default createReactConfig;
|
|
46
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/react.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAG3D,KAAK,YAAY,GAAG,YAAY,CAAC;IAC/B,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB,OAAO,CAAC,EAAE;QACR,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;KACxB,CAAC;IACF,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC,CAAC;AAuDH;;GAEG;AACH,eAAO,MAAM,WAAW;;;;;4BAMF,OAAO,GAAG,SAAS;;CAKxC,CAAC;AAEF,gBAAgB;AAChB;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,kBAAkB,CAAC,EAAE,MAAM,GAAG,YAAY,EAC1C,YAAY,CAAC,EAAE,YAAY,GAC1B,UAAU,CAAC,OAAO,YAAY,CAAC,CAiBjC;AAED,gBAAgB;AAChB;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,GAAE,SAAS,YAAY,EAAkB,EAChD,kBAAkB,CAAC,EAAE,MAAM,GAAG,YAAY,EAC1C,YAAY,CAAC,EAAE,YAAY,GAC1B,UAAU,CAAC,OAAO,YAAY,CAAC,CAqGjC;AAED,eAAe,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// src/react.ts
|
|
2
|
+
import { existsSync as existsSync3 } from "fs";
|
|
3
|
+
import { join as join3 } from "path";
|
|
4
|
+
import {
|
|
5
|
+
defaultClientConditions as defaultClientConditions2,
|
|
6
|
+
defaultServerConditions as defaultServerConditions2
|
|
7
|
+
} from "vite";
|
|
8
|
+
import { defineConfig as defineConfig2 } from "vitest/config";
|
|
9
|
+
import react from "@vitejs/plugin-react";
|
|
10
|
+
|
|
11
|
+
// src/index.ts
|
|
12
|
+
import { existsSync as existsSync2 } from "fs";
|
|
13
|
+
import { join as join2 } from "path";
|
|
14
|
+
import { defaultClientConditions, defaultServerConditions } from "vite";
|
|
15
|
+
import { defineConfig } from "vitest/config";
|
|
16
|
+
|
|
17
|
+
// src/workspace.ts
|
|
18
|
+
import { existsSync, readFileSync } from "fs";
|
|
19
|
+
import * as path from "path";
|
|
20
|
+
function readPackageName(packageDir) {
|
|
21
|
+
const packageJsonPath = path.join(packageDir, "package.json");
|
|
22
|
+
if (!existsSync(packageJsonPath)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const parsed = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
27
|
+
return typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : null;
|
|
28
|
+
} catch {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/index.ts
|
|
34
|
+
var baseConfig = {
|
|
35
|
+
test: {
|
|
36
|
+
testTimeout: 1e4,
|
|
37
|
+
// 10 seconds default timeout
|
|
38
|
+
hookTimeout: 1e4,
|
|
39
|
+
// 10 seconds for setup/teardown hooks
|
|
40
|
+
coverage: {
|
|
41
|
+
provider: "v8",
|
|
42
|
+
reporter: ["text", "html", "lcov", "json"],
|
|
43
|
+
reportsDirectory: "./generated/test-coverage",
|
|
44
|
+
exclude: [
|
|
45
|
+
"**/node_modules/**",
|
|
46
|
+
"**/dist/**",
|
|
47
|
+
"**/tests/**",
|
|
48
|
+
"**/*.d.ts",
|
|
49
|
+
"**/*.config.{js,ts,mjs,mts}",
|
|
50
|
+
"**/coverage/**",
|
|
51
|
+
"**/examples/**",
|
|
52
|
+
"**/src/index.ts",
|
|
53
|
+
"**/src/*/index.ts",
|
|
54
|
+
"**/src/types/**",
|
|
55
|
+
"tsup.config.ts",
|
|
56
|
+
"vitest.config.mts",
|
|
57
|
+
"tailwind.config.mjs",
|
|
58
|
+
"**/vitest.setup.ts"
|
|
59
|
+
],
|
|
60
|
+
thresholds: process.env.VITEST_COVERAGE_THRESHOLDS_DISABLED === "true" ? {
|
|
61
|
+
lines: 0,
|
|
62
|
+
functions: 0,
|
|
63
|
+
branches: 0,
|
|
64
|
+
statements: 0
|
|
65
|
+
} : {
|
|
66
|
+
lines: 100,
|
|
67
|
+
functions: 100,
|
|
68
|
+
branches: 100,
|
|
69
|
+
statements: 100
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/react.ts
|
|
76
|
+
var EMPTY_CONFIG = {};
|
|
77
|
+
var EMPTY_PLUGINS = [];
|
|
78
|
+
function generateSelfPackageAliases(projectDir) {
|
|
79
|
+
const packageName = readPackageName(projectDir);
|
|
80
|
+
if (packageName === null) {
|
|
81
|
+
return {};
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
[packageName]: `${projectDir}/src`
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function autoDetectSetupFiles(projectDir) {
|
|
88
|
+
if (projectDir === void 0 || projectDir === "") {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const vitestSetupPath = join3(projectDir, "vitest.setup.ts");
|
|
92
|
+
if (existsSync3(vitestSetupPath)) {
|
|
93
|
+
return ["./vitest.setup.ts"];
|
|
94
|
+
}
|
|
95
|
+
const testsSetupPath = join3(projectDir, "tests/setup.ts");
|
|
96
|
+
if (existsSync3(testsSetupPath)) {
|
|
97
|
+
return ["./tests/setup.ts"];
|
|
98
|
+
}
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
function autoDetectIncludePatterns() {
|
|
102
|
+
return ["tests/**/*.test.{ts,tsx,js,jsx}"];
|
|
103
|
+
}
|
|
104
|
+
var reactConfig = {
|
|
105
|
+
test: {
|
|
106
|
+
environment: "jsdom",
|
|
107
|
+
exclude: ["**/node_modules/**", "**/dist/**"],
|
|
108
|
+
// Suppress vitest's unhandled error reporting for expected test errors
|
|
109
|
+
silent: false,
|
|
110
|
+
onConsoleLog: () => {
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
function createReactConfig(projectDirOrConfig, customConfig) {
|
|
116
|
+
let projectDir;
|
|
117
|
+
let config;
|
|
118
|
+
if (typeof projectDirOrConfig === "string") {
|
|
119
|
+
projectDir = projectDirOrConfig;
|
|
120
|
+
config = customConfig ?? {};
|
|
121
|
+
} else {
|
|
122
|
+
projectDir = void 0;
|
|
123
|
+
config = projectDirOrConfig ?? {};
|
|
124
|
+
}
|
|
125
|
+
if (projectDir !== void 0) {
|
|
126
|
+
return createReactConfigWithPlugins([react()], projectDir, config);
|
|
127
|
+
}
|
|
128
|
+
return createReactConfigWithPlugins([react()], config);
|
|
129
|
+
}
|
|
130
|
+
function createReactConfigWithPlugins(plugins = EMPTY_PLUGINS, projectDirOrConfig, customConfig) {
|
|
131
|
+
let projectDir;
|
|
132
|
+
let config;
|
|
133
|
+
if (typeof projectDirOrConfig === "string") {
|
|
134
|
+
projectDir = projectDirOrConfig;
|
|
135
|
+
config = customConfig ?? EMPTY_CONFIG;
|
|
136
|
+
} else {
|
|
137
|
+
projectDir = void 0;
|
|
138
|
+
config = projectDirOrConfig ?? EMPTY_CONFIG;
|
|
139
|
+
}
|
|
140
|
+
const autoSetupFiles = config.test?.setupFiles !== void 0 && config.test.setupFiles.length > 0 ? [] : autoDetectSetupFiles(projectDir);
|
|
141
|
+
const autoIncludePatterns = config.test?.include !== void 0 && config.test.include.length > 0 ? [] : autoDetectIncludePatterns();
|
|
142
|
+
return defineConfig2({
|
|
143
|
+
plugins: [...plugins],
|
|
144
|
+
...baseConfig,
|
|
145
|
+
...reactConfig,
|
|
146
|
+
...config,
|
|
147
|
+
test: {
|
|
148
|
+
...baseConfig.test,
|
|
149
|
+
...reactConfig.test,
|
|
150
|
+
// Auto-detect setupFiles if not explicitly provided
|
|
151
|
+
...autoSetupFiles.length > 0 && { setupFiles: autoSetupFiles },
|
|
152
|
+
// Auto-detect include patterns if not explicitly provided
|
|
153
|
+
...autoIncludePatterns.length > 0 && { include: autoIncludePatterns },
|
|
154
|
+
...config.test,
|
|
155
|
+
coverage: {
|
|
156
|
+
...baseConfig.test.coverage,
|
|
157
|
+
...config.test?.coverage,
|
|
158
|
+
exclude: [
|
|
159
|
+
"**/node_modules/**",
|
|
160
|
+
"**/dist/**",
|
|
161
|
+
"**/tests/**",
|
|
162
|
+
"**/*.d.ts",
|
|
163
|
+
"**/*.config.{js,ts,mjs,mts}",
|
|
164
|
+
"**/coverage/**",
|
|
165
|
+
"**/examples/**",
|
|
166
|
+
"**/src/index.ts",
|
|
167
|
+
"**/src/*/index.ts",
|
|
168
|
+
"**/src/types/**",
|
|
169
|
+
"tsup.config.ts",
|
|
170
|
+
"vitest.config.mts",
|
|
171
|
+
"tailwind.config.mjs",
|
|
172
|
+
"**/.next/**",
|
|
173
|
+
"**/vitest.setup.ts",
|
|
174
|
+
"**/types.ts"
|
|
175
|
+
]
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
resolve: {
|
|
179
|
+
// Prefer "source" condition in package.json exports, allowing
|
|
180
|
+
// Vitest to resolve workspace dependencies directly to TypeScript source
|
|
181
|
+
// files without requiring a prior build step.
|
|
182
|
+
//
|
|
183
|
+
// Since Vite 6, resolve.conditions REPLACES the defaults instead of
|
|
184
|
+
// extending them. We must include defaultClientConditions to preserve
|
|
185
|
+
// standard conditions like "module", "browser", "development|production".
|
|
186
|
+
conditions: ["source", ...defaultClientConditions2],
|
|
187
|
+
// Deduplicate React to prevent multiple-instance issues in tests.
|
|
188
|
+
// This is Vite's standard API for singleton enforcement — no filesystem
|
|
189
|
+
// paths needed, Vite resolves from the project root automatically.
|
|
190
|
+
dedupe: [
|
|
191
|
+
"react",
|
|
192
|
+
"react-dom",
|
|
193
|
+
"react/jsx-runtime",
|
|
194
|
+
"react/jsx-dev-runtime"
|
|
195
|
+
],
|
|
196
|
+
alias: {
|
|
197
|
+
// Auto-generate self-package alias
|
|
198
|
+
...projectDir !== void 0 && projectDir !== "" && generateSelfPackageAliases(projectDir),
|
|
199
|
+
// Standard "@" → src alias
|
|
200
|
+
...projectDir !== void 0 && projectDir !== "" && { "@": `${projectDir}/src` },
|
|
201
|
+
// Consumer-provided aliases override everything above
|
|
202
|
+
...config.resolve?.alias
|
|
203
|
+
},
|
|
204
|
+
...config.resolve
|
|
205
|
+
},
|
|
206
|
+
// Vitest runs in Vite's SSR mode. Since Vite 6, ssr.resolve.conditions is
|
|
207
|
+
// independent from resolve.conditions and defaults to defaultServerConditions.
|
|
208
|
+
// We must explicitly include "source" in ssr.resolve.conditions so
|
|
209
|
+
// workspace dependencies resolve to TypeScript source during test execution.
|
|
210
|
+
ssr: {
|
|
211
|
+
resolve: {
|
|
212
|
+
conditions: ["source", ...defaultServerConditions2],
|
|
213
|
+
externalConditions: ["source"]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
var react_default = createReactConfig;
|
|
219
|
+
export {
|
|
220
|
+
createReactConfig,
|
|
221
|
+
createReactConfigWithPlugins,
|
|
222
|
+
react_default as default,
|
|
223
|
+
reactConfig
|
|
224
|
+
};
|
|
225
|
+
//# sourceMappingURL=react.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/react.ts","../../src/index.ts","../../src/workspace.ts"],"sourcesContent":["/**\n * React-specific Vitest configuration\n * @module @reasonabletech/config-vitest/react\n */\n\nimport { existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport {\n defaultClientConditions,\n defaultServerConditions,\n type PluginOption,\n} from \"vite\";\nimport { defineConfig } from \"vitest/config\";\nimport type { InlineConfig } from \"vitest/node\";\nimport react from \"@vitejs/plugin-react\";\nimport { baseConfig, type DeepReadonly } from \"./index.js\";\nimport { readPackageName } from \"./workspace.js\";\n\ntype VitestConfig = DeepReadonly<{\n test?: InlineConfig;\n resolve?: {\n alias?: Record<string, string>;\n [key: string]: unknown;\n };\n [key: string]: unknown;\n}>;\n\n// Empty readonly config and plugins array for default parameters\nconst EMPTY_CONFIG = {} as const satisfies VitestConfig;\nconst EMPTY_PLUGINS = [] as const satisfies readonly PluginOption[];\n\n/**\n * Generates resolve aliases mapping a package's own name to its source directory.\n * @param projectDir - Absolute path to the project directory\n * @returns A record mapping the package name to its `src/` directory\n * @see index.ts for the equivalent function used in the base config\n */\nfunction generateSelfPackageAliases(\n projectDir: string,\n): Record<string, string> {\n const packageName = readPackageName(projectDir);\n if (packageName === null) {\n return {};\n }\n return {\n [packageName]: `${projectDir}/src`,\n };\n}\n\n/**\n * Auto-detects setup files in a project directory\n * @param projectDir - The project directory path\n * @returns Array of detected setup file paths\n */\nfunction autoDetectSetupFiles(projectDir?: string): string[] {\n if (projectDir === undefined || projectDir === \"\") {\n return [];\n }\n\n const vitestSetupPath = join(projectDir, \"vitest.setup.ts\");\n if (existsSync(vitestSetupPath)) {\n return [\"./vitest.setup.ts\"];\n }\n\n const testsSetupPath = join(projectDir, \"tests/setup.ts\");\n if (existsSync(testsSetupPath)) {\n return [\"./tests/setup.ts\"];\n }\n\n return [];\n}\n\n/**\n * Auto-detects test include patterns based on project structure\n * @returns Array of include patterns\n */\nfunction autoDetectIncludePatterns(): string[] {\n return [\"tests/**/*.test.{ts,tsx,js,jsx}\"];\n}\n\n/**\n * React-specific configuration options\n */\nexport const reactConfig = {\n test: {\n environment: \"jsdom\",\n exclude: [\"**/node_modules/**\", \"**/dist/**\"],\n // Suppress vitest's unhandled error reporting for expected test errors\n silent: false,\n onConsoleLog: (): boolean | undefined => {\n // Allow packages to customize console filtering in their own config\n return undefined;\n },\n },\n};\n\n/** @overload */\n/**\n * Creates a Vitest configuration for React-based packages.\n * @param projectDirOrConfig - Either the absolute project directory or an existing configuration to merge.\n * @param customConfig - Additional configuration options for the project-dir overload.\n * @returns A Vitest configuration optimized for React components.\n */\nexport function createReactConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? ({} as const);\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? ({} as const);\n }\n\n if (projectDir !== undefined) {\n return createReactConfigWithPlugins([react()], projectDir, config);\n }\n return createReactConfigWithPlugins([react()], config);\n}\n\n/** @overload */\n/**\n * Creates a Vitest configuration for React-based packages with plugins.\n * @param plugins - Array of Vite plugins to include.\n * @param projectDirOrConfig - Either the absolute project directory or an existing configuration to merge.\n * @param customConfig - Additional configuration options for the project-dir overload.\n * @returns A Vitest configuration optimized for React components with plugins.\n */\nexport function createReactConfigWithPlugins(\n plugins: readonly PluginOption[] = EMPTY_PLUGINS,\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n // Auto-detect configuration if not explicitly provided\n const autoSetupFiles =\n config.test?.setupFiles !== undefined && config.test.setupFiles.length > 0\n ? []\n : autoDetectSetupFiles(projectDir);\n const autoIncludePatterns =\n config.test?.include !== undefined && config.test.include.length > 0\n ? []\n : autoDetectIncludePatterns();\n\n return defineConfig({\n plugins: [...plugins],\n ...baseConfig,\n ...reactConfig,\n ...config,\n test: {\n ...baseConfig.test,\n ...reactConfig.test,\n // Auto-detect setupFiles if not explicitly provided\n ...(autoSetupFiles.length > 0 && { setupFiles: autoSetupFiles }),\n // Auto-detect include patterns if not explicitly provided\n ...(autoIncludePatterns.length > 0 && { include: autoIncludePatterns }),\n ...config.test,\n coverage: {\n ...baseConfig.test.coverage,\n ...config.test?.coverage,\n exclude: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/tests/**\",\n \"**/*.d.ts\",\n \"**/*.config.{js,ts,mjs,mts}\",\n \"**/coverage/**\",\n \"**/examples/**\",\n \"**/src/index.ts\",\n \"**/src/*/index.ts\",\n \"**/src/types/**\",\n \"tsup.config.ts\",\n \"vitest.config.mts\",\n \"tailwind.config.mjs\",\n \"**/.next/**\",\n \"**/vitest.setup.ts\",\n \"**/types.ts\",\n ],\n },\n },\n resolve: {\n // Prefer \"source\" condition in package.json exports, allowing\n // Vitest to resolve workspace dependencies directly to TypeScript source\n // files without requiring a prior build step.\n //\n // Since Vite 6, resolve.conditions REPLACES the defaults instead of\n // extending them. We must include defaultClientConditions to preserve\n // standard conditions like \"module\", \"browser\", \"development|production\".\n conditions: [\"source\", ...defaultClientConditions],\n // Deduplicate React to prevent multiple-instance issues in tests.\n // This is Vite's standard API for singleton enforcement — no filesystem\n // paths needed, Vite resolves from the project root automatically.\n dedupe: [\n \"react\",\n \"react-dom\",\n \"react/jsx-runtime\",\n \"react/jsx-dev-runtime\",\n ],\n alias: {\n // Auto-generate self-package alias\n ...(projectDir !== undefined &&\n projectDir !== \"\" &&\n generateSelfPackageAliases(projectDir)),\n // Standard \"@\" → src alias\n ...(projectDir !== undefined &&\n projectDir !== \"\" && { \"@\": `${projectDir}/src` }),\n // Consumer-provided aliases override everything above\n ...config.resolve?.alias,\n },\n ...config.resolve,\n },\n // Vitest runs in Vite's SSR mode. Since Vite 6, ssr.resolve.conditions is\n // independent from resolve.conditions and defaults to defaultServerConditions.\n // We must explicitly include \"source\" in ssr.resolve.conditions so\n // workspace dependencies resolve to TypeScript source during test execution.\n ssr: {\n resolve: {\n conditions: [\"source\", ...defaultServerConditions],\n externalConditions: [\"source\"],\n },\n },\n });\n}\n\nexport default createReactConfig;\n","/**\n * Base Vitest configuration for all packages\n * @module @reasonabletech/config-vitest\n */\n\nimport { existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { defaultClientConditions, defaultServerConditions } from \"vite\";\nimport { defineConfig } from \"vitest/config\";\nimport type { InlineConfig } from \"vitest/node\";\nimport { readPackageName } from \"./workspace.js\";\n\n/** Recursively makes all properties of T readonly */\nexport type DeepReadonly<T> = {\n readonly [P in keyof T]: T[P] extends ReadonlyArray<infer U>\n ? ReadonlyArray<DeepReadonly<U>>\n : T[P] extends Array<infer U>\n ? ReadonlyArray<DeepReadonly<U>>\n : T[P] extends object\n ? DeepReadonly<T[P]>\n : T[P];\n};\n\n/**\n * Immutable configuration object accepted by {@link createVitestConfig} and\n * related helpers.\n */\nexport type VitestConfig = DeepReadonly<{\n test?: InlineConfig;\n resolve?: {\n conditions?: string[];\n alias?: Record<string, string>;\n [key: string]: unknown;\n };\n [key: string]: unknown;\n}>;\n\n// Empty readonly config for default parameters\nconst EMPTY_CONFIG = {} as const satisfies VitestConfig;\n\n/**\n * Base configuration options that apply to all test environments\n */\nexport const baseConfig = {\n test: {\n testTimeout: 10000, // 10 seconds default timeout\n hookTimeout: 10000, // 10 seconds for setup/teardown hooks\n coverage: {\n provider: \"v8\" as const,\n reporter: [\"text\", \"html\", \"lcov\", \"json\"],\n reportsDirectory: \"./generated/test-coverage\",\n exclude: [\n \"**/node_modules/**\",\n \"**/dist/**\",\n \"**/tests/**\",\n \"**/*.d.ts\",\n \"**/*.config.{js,ts,mjs,mts}\",\n \"**/coverage/**\",\n \"**/examples/**\",\n \"**/src/index.ts\",\n \"**/src/*/index.ts\",\n \"**/src/types/**\",\n \"tsup.config.ts\",\n \"vitest.config.mts\",\n \"tailwind.config.mjs\",\n \"**/vitest.setup.ts\",\n ],\n thresholds:\n process.env.VITEST_COVERAGE_THRESHOLDS_DISABLED === \"true\"\n ? {\n lines: 0,\n functions: 0,\n branches: 0,\n statements: 0,\n }\n : {\n lines: 100,\n functions: 100,\n branches: 100,\n statements: 100,\n },\n },\n },\n};\n\n/**\n * Generates resolve aliases that map a package's own name back to its source\n * directory. This allows test files to `import { foo } from \"@reasonabletech/my-pkg\"`\n * and have Vite resolve it to the local `src/` tree instead of requiring a prior\n * build step.\n *\n * Placed before user-supplied aliases so consumers can override if needed.\n * @param projectDir - Absolute path to the project directory\n * @returns A record mapping the package name to its `src/` directory\n */\nfunction generateSelfPackageAliases(\n projectDir: string,\n): Record<string, string> {\n const packageName = readPackageName(projectDir);\n if (packageName === null) {\n return {};\n }\n return {\n [packageName]: `${projectDir}/src`,\n };\n}\n\n/**\n * Auto-detects setup files in a project directory\n * @param projectDir - The project directory path\n * @returns Array of detected setup file paths\n */\nfunction autoDetectSetupFiles(projectDir?: string): string[] {\n if (projectDir === undefined || projectDir === \"\") {\n return [];\n }\n\n const vitestSetupPath = join(projectDir, \"vitest.setup.ts\");\n if (existsSync(vitestSetupPath)) {\n return [\"./vitest.setup.ts\"];\n }\n\n const testsSetupPath = join(projectDir, \"tests/setup.ts\");\n if (existsSync(testsSetupPath)) {\n return [\"./tests/setup.ts\"];\n }\n\n return [];\n}\n\n/**\n * Auto-detects test include patterns based on project structure\n * @returns Array of include patterns\n */\nfunction autoDetectIncludePatterns(): string[] {\n return [\"tests/**/*.test.{ts,tsx,js,jsx}\"];\n}\n\n/** @overload */\n/**\n * Creates a merged configuration from the base and any custom options.\n * @param projectDirOrConfig - Either the absolute project directory (use `import.meta.dirname`) or a prebuilt config.\n * @param customConfig - Additional configuration to merge when a project directory is provided.\n * @returns A merged Vitest configuration tailored for the caller.\n */\nexport function createVitestConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n // Auto-detect configuration if not explicitly provided\n const autoSetupFiles =\n config.test?.setupFiles !== undefined && config.test.setupFiles.length > 0\n ? []\n : autoDetectSetupFiles(projectDir);\n const autoIncludePatterns =\n config.test?.include !== undefined && config.test.include.length > 0\n ? []\n : autoDetectIncludePatterns();\n\n return defineConfig({\n ...baseConfig,\n ...config,\n test: {\n ...baseConfig.test,\n // Auto-detect setupFiles if not explicitly provided\n ...(autoSetupFiles.length > 0 && { setupFiles: autoSetupFiles }),\n // Auto-detect include patterns if not explicitly provided\n ...(autoIncludePatterns.length > 0 && { include: autoIncludePatterns }),\n ...config.test,\n coverage: {\n ...baseConfig.test.coverage,\n ...config.test?.coverage,\n },\n },\n resolve: {\n // Prefer \"source\" condition in package.json exports, allowing\n // Vitest to resolve workspace dependencies directly to TypeScript source\n // files without requiring a prior build step.\n //\n // Since Vite 6, resolve.conditions REPLACES the defaults instead of\n // extending them. We must include defaultClientConditions to preserve\n // standard conditions like \"module\", \"browser\", \"development|production\".\n conditions: [\"source\", ...defaultClientConditions],\n alias: {\n // Work around packages whose \"module\" entry is bundler-oriented but not\n // directly Node.js ESM-resolvable in Vitest (e.g. extensionless internal\n // specifiers). Prefer the Node/CJS build for tests.\n \"@opentelemetry/api\": \"@opentelemetry/api/build/src/index.js\",\n // Auto-generate self-package alias (e.g. \"@reasonabletech/utils\" → \"./src\")\n ...(projectDir !== undefined &&\n projectDir !== \"\" &&\n generateSelfPackageAliases(projectDir)),\n // Standard \"@\" → src alias\n ...(projectDir !== undefined &&\n projectDir !== \"\" && { \"@\": `${projectDir}/src` }),\n // Consumer-provided aliases override everything above\n ...config.resolve?.alias,\n },\n ...config.resolve,\n },\n // Vitest runs in Vite's SSR mode. Since Vite 6, ssr.resolve.conditions is\n // independent from resolve.conditions and defaults to defaultServerConditions.\n // We must explicitly include \"source\" in ssr.resolve.conditions so\n // workspace dependencies resolve to TypeScript source during test execution.\n // Additionally, ssr.resolve.externalConditions passes \"source\" to\n // Node.js when it natively resolves externalized packages.\n //\n // Note: We intentionally omit the \"module\" export condition for SSR tests.\n // Some third-party packages expose a \"module\" build that is bundler-friendly\n // but not directly Node.js ESM-resolvable (for example: extensionless internal\n // specifiers). For tests, preferring Node-compatible (\"node\"/\"default\") entry\n // points avoids runtime import failures.\n ssr: {\n resolve: {\n conditions: [\n \"source\",\n ...defaultServerConditions.filter(\n (condition) => condition !== \"module\",\n ),\n ],\n externalConditions: [\"source\"],\n },\n },\n });\n}\n\n/** @overload */\n/**\n * Creates a configuration with extended timeouts for long-running tests.\n * @param projectDirOrConfig - Either the absolute project directory or an existing configuration to extend.\n * @param customConfig - Additional configuration to merge when a project directory is supplied.\n * @returns A Vitest configuration suited for long-running suites.\n */\nexport function createLongRunningTestConfig(\n projectDirOrConfig?: string | VitestConfig,\n customConfig?: VitestConfig,\n): ReturnType<typeof defineConfig> {\n // Handle overloaded parameters\n let projectDir: string | undefined;\n let config: VitestConfig;\n\n if (typeof projectDirOrConfig === \"string\") {\n projectDir = projectDirOrConfig;\n config = customConfig ?? EMPTY_CONFIG;\n } else {\n projectDir = undefined;\n config = projectDirOrConfig ?? EMPTY_CONFIG;\n }\n\n const longRunningConfig: VitestConfig = {\n ...config,\n test: {\n testTimeout: 30000, // 30 seconds for long-running tests\n hookTimeout: 30000, // 30 seconds for setup/teardown hooks\n ...config.test,\n },\n };\n\n if (projectDir !== undefined) {\n return createVitestConfig(projectDir, longRunningConfig);\n }\n return createVitestConfig(longRunningConfig);\n}\n\n// Re-export for convenience\nexport { createReactConfig, createReactConfigWithPlugins } from \"./react.js\";\n\nexport default createVitestConfig;\n","/**\n * Workspace utility functions for discovering monorepo structure\n *\n * These are extracted from global-setup.ts so they can be reused by\n * the Vitest config factories (e.g. auto self-package aliasing).\n * @module @reasonabletech/config-vitest/workspace\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport * as path from \"node:path\";\n\n/**\n * Walks up the directory tree from `startDir` until it finds a directory\n * containing `pnpm-workspace.yaml`, which marks the monorepo root.\n *\n * Returns `startDir` unchanged if no workspace root is found (e.g. when\n * running outside the monorepo).\n * @param startDir - The directory to start searching from\n * @returns The absolute path to the monorepo root, or `startDir` if not found\n */\nexport function findRepoRoot(startDir: string): string {\n let currentDir = startDir;\n for (;;) {\n if (existsSync(path.join(currentDir, \"pnpm-workspace.yaml\"))) {\n return currentDir;\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return startDir;\n }\n currentDir = parentDir;\n }\n}\n\n/**\n * Reads the `name` field from the `package.json` in the given directory.\n *\n * Returns `null` when:\n * - No `package.json` exists at `packageDir`\n * - The file cannot be parsed as JSON\n * - The `name` field is missing, empty, or not a string\n * @param packageDir - The directory containing the package.json to read\n * @returns The package name string, or `null` if unavailable\n */\nexport function readPackageName(packageDir: string): string | null {\n const packageJsonPath = path.join(packageDir, \"package.json\");\n if (!existsSync(packageJsonPath)) {\n return null;\n }\n\n try {\n const parsed = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as {\n name?: unknown;\n };\n return typeof parsed.name === \"string\" && parsed.name.length > 0\n ? parsed.name\n : null;\n } catch {\n return null;\n }\n}\n"],"mappings":";AAKA,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,QAAAC,aAAY;AACrB;AAAA,EACE,2BAAAC;AAAA,EACA,2BAAAC;AAAA,OAEK;AACP,SAAS,gBAAAC,qBAAoB;AAE7B,OAAO,WAAW;;;ACTlB,SAAS,cAAAC,mBAAkB;AAC3B,SAAS,QAAAC,aAAY;AACrB,SAAS,yBAAyB,+BAA+B;AACjE,SAAS,oBAAoB;;;ACA7B,SAA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AC,QAAQ;AAAA,MAC/B;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAEA,IAAO,gBAAQ;","names":["existsSync","join","defaultClientConditions","defaultServerConditions","defineConfig","existsSync","join","join","existsSync","defineConfig","defaultClientConditions","defaultServerConditions"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workspace utility functions for discovering monorepo structure
|
|
3
|
+
*
|
|
4
|
+
* These are extracted from global-setup.ts so they can be reused by
|
|
5
|
+
* the Vitest config factories (e.g. auto self-package aliasing).
|
|
6
|
+
* @module @reasonabletech/config-vitest/workspace
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Walks up the directory tree from `startDir` until it finds a directory
|
|
10
|
+
* containing `pnpm-workspace.yaml`, which marks the monorepo root.
|
|
11
|
+
*
|
|
12
|
+
* Returns `startDir` unchanged if no workspace root is found (e.g. when
|
|
13
|
+
* running outside the monorepo).
|
|
14
|
+
* @param startDir - The directory to start searching from
|
|
15
|
+
* @returns The absolute path to the monorepo root, or `startDir` if not found
|
|
16
|
+
*/
|
|
17
|
+
export declare function findRepoRoot(startDir: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Reads the `name` field from the `package.json` in the given directory.
|
|
20
|
+
*
|
|
21
|
+
* Returns `null` when:
|
|
22
|
+
* - No `package.json` exists at `packageDir`
|
|
23
|
+
* - The file cannot be parsed as JSON
|
|
24
|
+
* - The `name` field is missing, empty, or not a string
|
|
25
|
+
* @param packageDir - The directory containing the package.json to read
|
|
26
|
+
* @returns The package name string, or `null` if unavailable
|
|
27
|
+
*/
|
|
28
|
+
export declare function readPackageName(packageDir: string): string | null;
|
|
29
|
+
//# sourceMappingURL=workspace.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace.d.ts","sourceRoot":"","sources":["../../src/workspace.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAarD;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAgBjE"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/workspace.ts
|
|
2
|
+
import { existsSync, readFileSync } from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
function findRepoRoot(startDir) {
|
|
5
|
+
let currentDir = startDir;
|
|
6
|
+
for (; ; ) {
|
|
7
|
+
if (existsSync(path.join(currentDir, "pnpm-workspace.yaml"))) {
|
|
8
|
+
return currentDir;
|
|
9
|
+
}
|
|
10
|
+
const parentDir = path.dirname(currentDir);
|
|
11
|
+
if (parentDir === currentDir) {
|
|
12
|
+
return startDir;
|
|
13
|
+
}
|
|
14
|
+
currentDir = parentDir;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function readPackageName(packageDir) {
|
|
18
|
+
const packageJsonPath = path.join(packageDir, "package.json");
|
|
19
|
+
if (!existsSync(packageJsonPath)) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
const parsed = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
24
|
+
return typeof parsed.name === "string" && parsed.name.length > 0 ? parsed.name : null;
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
findRepoRoot,
|
|
31
|
+
readPackageName
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=workspace.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/workspace.ts"],"sourcesContent":["/**\n * Workspace utility functions for discovering monorepo structure\n *\n * These are extracted from global-setup.ts so they can be reused by\n * the Vitest config factories (e.g. auto self-package aliasing).\n * @module @reasonabletech/config-vitest/workspace\n */\n\nimport { existsSync, readFileSync } from \"node:fs\";\nimport * as path from \"node:path\";\n\n/**\n * Walks up the directory tree from `startDir` until it finds a directory\n * containing `pnpm-workspace.yaml`, which marks the monorepo root.\n *\n * Returns `startDir` unchanged if no workspace root is found (e.g. when\n * running outside the monorepo).\n * @param startDir - The directory to start searching from\n * @returns The absolute path to the monorepo root, or `startDir` if not found\n */\nexport function findRepoRoot(startDir: string): string {\n let currentDir = startDir;\n for (;;) {\n if (existsSync(path.join(currentDir, \"pnpm-workspace.yaml\"))) {\n return currentDir;\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return startDir;\n }\n currentDir = parentDir;\n }\n}\n\n/**\n * Reads the `name` field from the `package.json` in the given directory.\n *\n * Returns `null` when:\n * - No `package.json` exists at `packageDir`\n * - The file cannot be parsed as JSON\n * - The `name` field is missing, empty, or not a string\n * @param packageDir - The directory containing the package.json to read\n * @returns The package name string, or `null` if unavailable\n */\nexport function readPackageName(packageDir: string): string | null {\n const packageJsonPath = path.join(packageDir, \"package.json\");\n if (!existsSync(packageJsonPath)) {\n return null;\n }\n\n try {\n const parsed = JSON.parse(readFileSync(packageJsonPath, \"utf-8\")) as {\n name?: unknown;\n };\n return typeof parsed.name === \"string\" && parsed.name.length > 0\n ? parsed.name\n : null;\n } catch {\n return null;\n }\n}\n"],"mappings":";AAQA,SAAS,YAAY,oBAAoB;AACzC,YAAY,UAAU;AAWf,SAAS,aAAa,UAA0B;AACrD,MAAI,aAAa;AACjB,aAAS;AACP,QAAI,WAAgB,UAAK,YAAY,qBAAqB,CAAC,GAAG;AAC5D,aAAO;AAAA,IACT;AAEA,UAAM,YAAiB,aAAQ,UAAU;AACzC,QAAI,cAAc,YAAY;AAC5B,aAAO;AAAA,IACT;AACA,iBAAa;AAAA,EACf;AACF;AAYO,SAAS,gBAAgB,YAAmC;AACjE,QAAM,kBAAuB,UAAK,YAAY,cAAc;AAC5D,MAAI,CAAC,WAAW,eAAe,GAAG;AAChC,WAAO;AAAA,EACT;AAEA,MAAI;AACF,UAAM,SAAS,KAAK,MAAM,aAAa,iBAAiB,OAAO,CAAC;AAGhE,WAAO,OAAO,OAAO,SAAS,YAAY,OAAO,KAAK,SAAS,IAC3D,OAAO,OACP;AAAA,EACN,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|