Skip to content

Commit 57132ea

Browse files
Gugustinettesxzz
andauthored
feat: add unrun as a config loader (#563)
Co-authored-by: Kevin Deng <[email protected]>
1 parent 86fa138 commit 57132ea

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

docs/options/config-file.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ This is useful if you want to rely solely on command-line options or default set
6969
- `auto` (default): Utilizes native runtime loading for TypeScript if supported; otherwise, defaults to `unconfig`.
7070
- `native`: Loads TypeScript configuration files using native runtime support. Requires a compatible environment, such as the latest Node.js, Deno, or Bun.
7171
- `unconfig`: Loads configuration files with the `unconfig` library, ensuring broad compatibility across different runtimes.
72+
- `unrun` _(experimental)_: Loads configuration files using the [`unrun`](https://gugustinette.github.io/unrun/) library, which provides similar compatibility as `unconfig` but with more performances.
7273

7374
> [!TIP]
74-
> Node.js does not natively support importing TypeScript files without specifying the file extension. If you are using Node.js and want to load a TypeScript config file without including the `.ts` extension, consider using the `unconfig` loader for seamless compatibility.
75+
> Node.js does not natively support importing TypeScript files without specifying the file extension. If you are using Node.js and want to load a TypeScript config file without including the `.ts` extension, consider using the `unconfig` or `unrun` loader for seamless compatibility.
7576
7677
## Extending Vite or Vitest Config (Experimental)
7778

docs/zh-CN/options/config-file.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ tsdown --no-config
6969
- `auto`(默认):如果运行时支持,则使用原生方式加载 TypeScript,否则回退到 `unconfig`
7070
- `native`:通过原生运行时支持加载 TypeScript 配置文件。需要兼容的环境,如最新版 Node.js、Deno 或 Bun。
7171
- `unconfig`:使用 `unconfig` 库加载配置文件,确保在不同运行时下具有广泛兼容性。
72+
- `unrun` _(experimental)_: Loads configuration files using the [`unrun`](https://gugustinette.github.io/unrun/) library, which provides similar compatibility as `unconfig` but with more performances.
7273

7374
> [!TIP]
74-
> Node.js 原生不支持在不指定文件扩展名的情况下导入 TypeScript 文件。如果您在 Node.js 环境下希望加载不带 `.ts` 扩展名的 TypeScript 配置文件,建议使用 `unconfig` 加载器以获得更好的兼容性。
75+
> Node.js 原生不支持在不指定文件扩展名的情况下导入 TypeScript 文件。如果您在 Node.js 环境下希望加载不带 `.ts` 扩展名的 TypeScript 配置文件,建议使用 `unconfig` `unrun` 加载器以获得更好的兼容性。
7576
7677
## 扩展 Vite 或 Vitest 配置(实验性功能){#extending-vite-or-vitest-config-experimental}
7778

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"scripts": {
4949
"lint": "eslint --cache --max-warnings 0 .",
5050
"lint:fix": "pnpm run lint --fix",
51-
"build": "tsx ./src/run.ts",
52-
"dev": "tsx ./src/run.ts",
51+
"build": "unrun ./src/run.ts",
52+
"dev": "unrun ./src/run.ts",
5353
"test": "vitest",
5454
"typecheck": "tsc --noEmit",
5555
"format": "prettier --cache --write .",
@@ -98,7 +98,8 @@
9898
"tinyexec": "catalog:prod",
9999
"tinyglobby": "catalog:prod",
100100
"tree-kill": "catalog:prod",
101-
"unconfig": "catalog:prod"
101+
"unconfig": "catalog:prod",
102+
"unrun": "catalog:prod"
102103
},
103104
"devDependencies": {
104105
"@arethetypeswrong/core": "catalog:dev",
@@ -117,7 +118,6 @@
117118
"prettier": "catalog:dev",
118119
"publint": "catalog:dev",
119120
"rolldown-plugin-require-cjs": "catalog:dev",
120-
"tsx": "catalog:dev",
121121
"typescript": "catalog:dev",
122122
"unocss": "catalog:docs",
123123
"unplugin-lightningcss": "catalog:dev",

pnpm-lock.yaml

Lines changed: 19 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ catalogs:
2222
prettier: ^3.6.2
2323
publint: ^0.3.15
2424
rolldown-plugin-require-cjs: ^0.3.1
25-
tsx: ^4.20.6
2625
typescript: ~5.9.3
2726
unplugin-lightningcss: ^0.4.3
2827
unplugin-unused: ^0.5.4
@@ -62,6 +61,7 @@ catalogs:
6261
tinyglobby: ^0.2.15
6362
tree-kill: ^1.2.2
6463
unconfig: ^7.3.3
64+
unrun: ^0.2.0
6565

6666
ignoredBuiltDependencies:
6767
- rolldown

src/options/config.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process'
33
import { pathToFileURL } from 'node:url'
44
import { underline } from 'ansis'
55
import { loadConfig } from 'unconfig'
6+
import { unrun } from 'unrun'
67
import { fsStat } from '../utils/fs'
78
import { toArray } from '../utils/general'
89
import { globalLogger } from '../utils/logger'
@@ -98,7 +99,12 @@ export async function loadConfigFile(
9899
{
99100
files: 'tsdown.config',
100101
extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''],
101-
parser: isNative ? nativeImport : 'auto',
102+
parser:
103+
options.configLoader === 'unrun'
104+
? unrunImport
105+
: isNative
106+
? nativeImport
107+
: 'auto',
102108
},
103109
{
104110
files: 'package.json',
@@ -146,3 +152,21 @@ async function nativeImport(id: string) {
146152
const config = mod.default || mod
147153
return config
148154
}
155+
156+
async function unrunImport(id: string) {
157+
const { module } = await unrun({
158+
path: pathToFileURL(id).href,
159+
}).catch((error) => {
160+
const cannotFindModule = error?.message?.includes?.('Cannot find module')
161+
if (cannotFindModule) {
162+
const configError = new Error(
163+
`Failed to load the config file. \`unrun\` is experimental; try setting the --config-loader CLI flag to \`unconfig\` instead.\n\n${error.message}`,
164+
)
165+
configError.cause = error
166+
throw configError
167+
} else {
168+
throw error
169+
}
170+
})
171+
return module
172+
}

src/options/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ export interface Options {
361361
* Config loader to use. It can only be set via CLI or API.
362362
* @default 'auto'
363363
*/
364-
configLoader?: 'auto' | 'native' | 'unconfig'
364+
configLoader?: 'auto' | 'native' | 'unconfig' | 'unrun'
365365

366366
/**
367367
* Reuse config from Vite or Vitest (experimental)

0 commit comments

Comments
 (0)