|
1 | | -import fs from "node:fs"; |
2 | | -import path from "node:path"; |
3 | | -import { fileURLToPath } from "node:url"; |
4 | 1 | import { svelte, vitePreprocess } from "@sveltejs/vite-plugin-svelte"; |
5 | 2 | import { defineConfig } from "vitest/config"; |
6 | | -import pkg from "./package.json"; |
| 3 | +import { |
| 4 | + generateAliasesFromExports, |
| 5 | + getDirname, |
| 6 | + testConfig, |
| 7 | +} from "./tests/utils"; |
7 | 8 |
|
8 | | -const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
9 | | - |
10 | | -/** |
11 | | - * Gets the directory name from import.meta.url (ESM equivalent of __dirname). |
12 | | - * @param url - The import.meta.url from the calling file |
13 | | - * @returns The directory path |
14 | | - */ |
15 | | -export function getDirname(url: string): string { |
16 | | - return path.dirname(fileURLToPath(url)); |
17 | | -} |
18 | | - |
19 | | -/** |
20 | | - * Generates Vite aliases from package.json exports for component subpath imports. |
21 | | - * Resolves imports like `carbon-components-svelte/Theme/Theme.svelte` to |
22 | | - * `./src/Theme/Theme.svelte` since these subpaths aren't in package.json |
23 | | - * exports and Vite needs runtime resolution (tsconfig only handles types). |
24 | | - * |
25 | | - * @param baseDir - Base directory for resolving src path (defaults to __dirname) |
26 | | - * @param srcRelativePath - Relative path to src directory from baseDir (defaults to "./src") |
27 | | - */ |
28 | | -export function generateAliasesFromExports( |
29 | | - baseDir: string = __dirname, |
30 | | - srcRelativePath: string = "./src", |
31 | | -) { |
32 | | - const aliases: Record<string, string> = {}; |
33 | | - const exports = pkg.exports; |
34 | | - |
35 | | - const srcSvelteExport = exports["./src/*.svelte"]; |
36 | | - if (!srcSvelteExport) return aliases; |
37 | | - |
38 | | - const srcDir = path.resolve(baseDir, srcRelativePath); |
39 | | - if (!fs.existsSync(srcDir)) return aliases; |
40 | | - |
41 | | - function scanDirectory(dir: string, basePath: string = "") { |
42 | | - const entries = fs.readdirSync(dir, { withFileTypes: true }); |
43 | | - |
44 | | - for (const entry of entries) { |
45 | | - const fullPath = path.join(dir, entry.name); |
46 | | - const relativePath = path.join(basePath, entry.name); |
47 | | - |
48 | | - if (entry.isDirectory()) { |
49 | | - scanDirectory(fullPath, relativePath); |
50 | | - } else if (entry.isFile() && entry.name.endsWith(".svelte")) { |
51 | | - const importPath = relativePath; |
52 | | - const aliasKey = `${pkg.name}/${importPath}`; |
53 | | - aliases[aliasKey] = path.resolve(baseDir, srcRelativePath, importPath); |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - scanDirectory(srcDir); |
59 | | - return aliases; |
60 | | -} |
| 9 | +const __dirname = getDirname(import.meta.url); |
61 | 10 |
|
62 | 11 | export default defineConfig({ |
63 | 12 | root: "./tests", |
64 | 13 | plugins: [svelte({ preprocess: [vitePreprocess()] })], |
65 | 14 | resolve: { |
66 | 15 | conditions: ["browser"], |
67 | | - alias: generateAliasesFromExports(), |
| 16 | + alias: generateAliasesFromExports(__dirname), |
68 | 17 | }, |
69 | 18 | test: { |
70 | | - globals: true, |
71 | | - environment: "jsdom", |
72 | | - clearMocks: true, |
73 | | - // Suppress `console` output in CI. |
74 | | - silent: !!process.env.CI, |
| 19 | + ...testConfig, |
75 | 20 | setupFiles: ["./tests/setup-tests.ts"], |
76 | 21 | }, |
77 | 22 | }); |
0 commit comments