@rpcbase/test 0.234.0 → 0.235.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/package.json +1 -1
- package/src/vitest.config.mjs +102 -0
package/package.json
CHANGED
package/src/vitest.config.mjs
CHANGED
|
@@ -1,7 +1,109 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import path from "node:path"
|
|
3
|
+
import { createRequire } from "node:module"
|
|
4
|
+
|
|
1
5
|
import { defineConfig } from "vitest/config"
|
|
2
6
|
|
|
7
|
+
|
|
8
|
+
const require = createRequire(import.meta.url)
|
|
9
|
+
|
|
10
|
+
function loadTsconfigWithTypescript() {
|
|
11
|
+
try {
|
|
12
|
+
const ts = require("typescript")
|
|
13
|
+
const cwd = process.cwd()
|
|
14
|
+
const configPath =
|
|
15
|
+
ts.findConfigFile(cwd, ts.sys.fileExists, "tsconfig.json") ||
|
|
16
|
+
ts.findConfigFile(cwd, ts.sys.fileExists, "tsconfig.base.json")
|
|
17
|
+
|
|
18
|
+
if (!configPath) return null
|
|
19
|
+
|
|
20
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
|
|
21
|
+
if (configFile.error) return null
|
|
22
|
+
|
|
23
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
24
|
+
configFile.config,
|
|
25
|
+
ts.sys,
|
|
26
|
+
path.dirname(configPath),
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
path: configPath,
|
|
31
|
+
config: configFile.config,
|
|
32
|
+
baseUrl: parsed.options?.baseUrl
|
|
33
|
+
? path.resolve(path.dirname(configPath), parsed.options.baseUrl)
|
|
34
|
+
: path.dirname(configPath),
|
|
35
|
+
paths: parsed.options?.paths,
|
|
36
|
+
}
|
|
37
|
+
} catch (_err) {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function loadTsconfigManually() {
|
|
43
|
+
const stripComments = (jsonWithComments) =>
|
|
44
|
+
jsonWithComments
|
|
45
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
46
|
+
.replace(/(^|\s+)\/\/.*$/gm, "")
|
|
47
|
+
|
|
48
|
+
const cwd = process.cwd()
|
|
49
|
+
const candidates = ["tsconfig.json", "tsconfig.base.json"]
|
|
50
|
+
|
|
51
|
+
for (const file of candidates) {
|
|
52
|
+
const fullPath = path.join(cwd, file)
|
|
53
|
+
if (!fs.existsSync(fullPath)) continue
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const raw = fs.readFileSync(fullPath, "utf8")
|
|
57
|
+
const parsed = JSON.parse(stripComments(raw))
|
|
58
|
+
const baseUrl = parsed.compilerOptions?.baseUrl
|
|
59
|
+
? path.resolve(path.dirname(fullPath), parsed.compilerOptions.baseUrl)
|
|
60
|
+
: path.dirname(fullPath)
|
|
61
|
+
return {
|
|
62
|
+
path: fullPath,
|
|
63
|
+
config: parsed,
|
|
64
|
+
baseUrl,
|
|
65
|
+
paths: parsed.compilerOptions?.paths,
|
|
66
|
+
}
|
|
67
|
+
} catch (_err) {
|
|
68
|
+
// ignore parsing errors; fall through to next candidate
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return null
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function loadTsconfig() {
|
|
76
|
+
return loadTsconfigWithTypescript() || loadTsconfigManually()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function pathsToAlias(tsconfig) {
|
|
80
|
+
const paths = tsconfig?.paths
|
|
81
|
+
if (!paths) return []
|
|
82
|
+
|
|
83
|
+
const baseUrl = tsconfig.baseUrl || path.dirname(tsconfig.path)
|
|
84
|
+
const alias = []
|
|
85
|
+
|
|
86
|
+
for (const [key, targets] of Object.entries(paths)) {
|
|
87
|
+
if (!Array.isArray(targets)) continue
|
|
88
|
+
|
|
89
|
+
const find = key.replace(/\*$/, "")
|
|
90
|
+
|
|
91
|
+
for (const target of targets) {
|
|
92
|
+
const cleanTarget = target.replace(/\*$/, "")
|
|
93
|
+
const replacement = path.resolve(baseUrl, cleanTarget)
|
|
94
|
+
alias.push({ find, replacement })
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return alias
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const tsconfig = loadTsconfig()
|
|
102
|
+
const alias = pathsToAlias(tsconfig)
|
|
103
|
+
|
|
3
104
|
export default defineConfig({
|
|
4
105
|
test: {
|
|
5
106
|
include: ["{src,lib}/**/*.test.{js,ts,tsx}"],
|
|
6
107
|
},
|
|
108
|
+
resolve: alias.length > 0 ? { alias } : undefined,
|
|
7
109
|
})
|