@rpcbase/test 0.234.0 → 0.236.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/test",
3
- "version": "0.234.0",
3
+ "version": "0.236.0",
4
4
  "type": "module",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -1,7 +1,78 @@
1
+ import path from "node:path"
2
+ import { createRequire } from "node:module"
3
+
1
4
  import { defineConfig } from "vitest/config"
2
5
 
6
+
7
+ const require = createRequire(import.meta.url)
8
+
9
+ function loadTsconfig() {
10
+ let ts
11
+ try {
12
+ ts = require("typescript")
13
+ } catch (_err) {
14
+ throw new Error(
15
+ "@rpcbase/test: TypeScript is required to resolve path aliases. Please add it as a devDependency.",
16
+ )
17
+ }
18
+
19
+ const cwd = process.cwd()
20
+ const configPath =
21
+ ts.findConfigFile(cwd, ts.sys.fileExists, "tsconfig.json") ||
22
+ ts.findConfigFile(cwd, ts.sys.fileExists, "tsconfig.base.json")
23
+
24
+ if (!configPath) {
25
+ throw new Error("@rpcbase/test: no tsconfig found for alias resolution")
26
+ }
27
+
28
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
29
+ if (configFile.error) {
30
+ throw new Error(`@rpcbase/test: unable to read tsconfig at ${configPath}`)
31
+ }
32
+
33
+ const parsed = ts.parseJsonConfigFileContent(
34
+ configFile.config,
35
+ ts.sys,
36
+ path.dirname(configPath),
37
+ )
38
+
39
+ return {
40
+ path: configPath,
41
+ baseUrl: parsed.options?.baseUrl
42
+ ? path.resolve(path.dirname(configPath), parsed.options.baseUrl)
43
+ : path.dirname(configPath),
44
+ paths: parsed.options?.paths,
45
+ }
46
+ }
47
+
48
+ function pathsToAlias(tsconfig) {
49
+ const paths = tsconfig?.paths
50
+ if (!paths) return []
51
+
52
+ const baseUrl = tsconfig.baseUrl || path.dirname(tsconfig.path)
53
+ const alias = []
54
+
55
+ for (const [key, targets] of Object.entries(paths)) {
56
+ if (!Array.isArray(targets)) continue
57
+
58
+ const find = key.replace(/\*$/, "")
59
+
60
+ for (const target of targets) {
61
+ const cleanTarget = target.replace(/\*$/, "")
62
+ const replacement = path.resolve(baseUrl, cleanTarget)
63
+ alias.push({ find, replacement })
64
+ }
65
+ }
66
+
67
+ return alias
68
+ }
69
+
70
+ const tsconfig = loadTsconfig()
71
+ const alias = pathsToAlias(tsconfig)
72
+
3
73
  export default defineConfig({
4
74
  test: {
5
75
  include: ["{src,lib}/**/*.test.{js,ts,tsx}"],
6
76
  },
77
+ resolve: alias.length > 0 ? { alias } : undefined,
7
78
  })