|
1 | | -import { basename, dirname, relative } from 'pathe' |
| 1 | +import { existsSync, readFileSync } from 'node:fs' |
| 2 | +import { basename, dirname, relative, resolve } from 'pathe' |
2 | 3 | import type { UserConfig as ViteConfig, Plugin as VitePlugin } from 'vite' |
3 | 4 | import { configDefaults } from '../../defaults' |
4 | 5 | import { generateScopedClassName } from '../../integrations/css/css-modules' |
@@ -35,10 +36,20 @@ export function WorkspaceVitestPlugin(project: WorkspaceProject, options: Worksp |
35 | 36 | const root = testConfig.root || viteConfig.root || options.root |
36 | 37 | let name = testConfig.name |
37 | 38 | if (!name) { |
38 | | - if (typeof options.workspacePath === 'string') |
39 | | - name = basename(options.workspacePath.endsWith('/') ? options.workspacePath.slice(0, -1) : dirname(options.workspacePath)) |
40 | | - else |
| 39 | + if (typeof options.workspacePath === 'string') { |
| 40 | + // if there is a package.json, read the name from it |
| 41 | + const dir = options.workspacePath.endsWith('/') |
| 42 | + ? options.workspacePath.slice(0, -1) |
| 43 | + : dirname(options.workspacePath) |
| 44 | + const pkgJsonPath = resolve(dir, 'package.json') |
| 45 | + if (existsSync(pkgJsonPath)) |
| 46 | + name = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')).name |
| 47 | + if (typeof name !== 'string' || !name) |
| 48 | + name = basename(dir) |
| 49 | + } |
| 50 | + else { |
41 | 51 | name = options.workspacePath.toString() |
| 52 | + } |
42 | 53 | } |
43 | 54 |
|
44 | 55 | const config: ViteConfig = { |
|
0 commit comments