|
1 | | -import fs from "node:fs"; |
2 | | -import path from "node:path"; |
3 | | -import { fileURLToPath } from "node:url"; |
4 | 1 | import { svelte } from "@sveltejs/vite-plugin-svelte"; |
5 | 2 | import { defineConfig } from "vitest/config"; |
6 | | -import pkg from "../package.json"; |
| 3 | +import { generateAliasesFromExports, getDirname } from "../vite.config"; |
7 | 4 |
|
8 | | -const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
9 | | - |
10 | | -/** |
11 | | - * Generates Vite aliases from package.json exports for component subpath imports. |
12 | | - * Resolves imports like `carbon-components-svelte/Theme/Theme.svelte` to |
13 | | - * `./src/Theme/Theme.svelte` since these subpaths aren't in package.json |
14 | | - * exports and Vite needs runtime resolution (tsconfig only handles types). |
15 | | - */ |
16 | | -function generateAliasesFromExports() { |
17 | | - const aliases: Record<string, string> = {}; |
18 | | - const exports = pkg.exports; |
19 | | - |
20 | | - const srcSvelteExport = exports["./src/*.svelte"]; |
21 | | - if (!srcSvelteExport) return aliases; |
22 | | - |
23 | | - const srcDir = path.resolve(__dirname, "../src"); |
24 | | - if (!fs.existsSync(srcDir)) return aliases; |
25 | | - |
26 | | - function scanDirectory(dir: string, basePath: string = "") { |
27 | | - const entries = fs.readdirSync(dir, { withFileTypes: true }); |
28 | | - |
29 | | - for (const entry of entries) { |
30 | | - const fullPath = path.join(dir, entry.name); |
31 | | - const relativePath = path.join(basePath, entry.name); |
32 | | - |
33 | | - if (entry.isDirectory()) { |
34 | | - scanDirectory(fullPath, relativePath); |
35 | | - } else if (entry.isFile() && entry.name.endsWith(".svelte")) { |
36 | | - const importPath = relativePath; |
37 | | - const aliasKey = `${pkg.name}/${importPath}`; |
38 | | - aliases[aliasKey] = path.resolve(__dirname, "../src", importPath); |
39 | | - } |
40 | | - } |
41 | | - } |
42 | | - |
43 | | - scanDirectory(srcDir); |
44 | | - return aliases; |
45 | | -} |
| 5 | +const __dirname = getDirname(import.meta.url); |
46 | 6 |
|
47 | 7 | export default defineConfig({ |
48 | | - plugins: [svelte()], |
49 | 8 | resolve: { |
50 | 9 | conditions: ["browser"], |
51 | | - alias: generateAliasesFromExports(), |
| 10 | + alias: generateAliasesFromExports(__dirname, "../src"), |
52 | 11 | }, |
| 12 | + // @ts-expect-error |
| 13 | + plugins: [svelte()], |
53 | 14 | server: { |
54 | 15 | fs: { |
55 | 16 | allow: [".."], |
|
0 commit comments