Skip to content

Commit 0b7e4f4

Browse files
committed
perf: reload config via import-without-cache
1 parent 22fd904 commit 0b7e4f4

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"cac": "catalog:prod",
9393
"empathic": "catalog:prod",
9494
"hookable": "catalog:prod",
95+
"import-without-cache": "catalog:prod",
9596
"obug": "catalog:prod",
9697
"rolldown": "catalog:prod",
9798
"rolldown-plugin-dts": "catalog:prod",

pnpm-lock.yaml

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

pnpm-workspace.yaml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ overrides:
77
vite: catalog:docs
88

99
catalogs:
10+
create:
11+
'@clack/prompts': ^0.11.0
12+
giget: ^2.0.0
1013
dev:
1114
'@sxzz/eslint-config': ^7.4.0
1215
'@sxzz/prettier-config': ^2.2.6
@@ -22,7 +25,6 @@ catalogs:
2225
rolldown-plugin-require-cjs: ^0.3.2
2326
typescript: ~5.9.3
2427
vitest: ^4.0.14
25-
2628
docs:
2729
'@shikijs/vitepress-twoslash': ^3.17.0
2830
'@unocss/eslint-plugin': ^66.5.9
@@ -38,19 +40,22 @@ catalogs:
3840
vitepress-plugin-llms: ^1.9.3
3941
vue: ^3.5.25
4042
vue-tsc: ^3.1.5
41-
43+
migrate:
44+
'@antfu/ni': ^27.0.1
45+
consola: ^3.4.2
46+
diff: ^8.0.2
4247
peer:
4348
'@arethetypeswrong/core': ^0.18.2
4449
'@vitejs/devtools': ^0.0.0-alpha.18
4550
publint: ^0.3.15
4651
unplugin-lightningcss: ^0.4.3
4752
unplugin-unused: ^0.5.6
48-
4953
prod:
5054
ansis: ^4.2.0
5155
cac: ^6.7.14
5256
empathic: ^2.0.0
5357
hookable: ^5.5.3
58+
import-without-cache: ^0.1.3
5459
is-in-ci: ^2.0.0
5560
obug: ^2.1.1
5661
package-manager-detector: ^1.6.0
@@ -63,15 +68,6 @@ catalogs:
6368
unconfig-core: ^7.4.1
6469
unrun: ^0.2.14
6570

66-
create:
67-
'@clack/prompts': ^0.11.0
68-
giget: ^2.0.0
69-
70-
migrate:
71-
'@antfu/ni': ^27.0.1
72-
consola: ^3.4.2
73-
diff: ^8.0.2
74-
7571
ignoredBuiltDependencies:
7672
- rolldown
7773
- unrs-resolver

src/config/config.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'node:path'
33
import process from 'node:process'
44
import { pathToFileURL } from 'node:url'
55
import { underline } from 'ansis'
6+
import { init, isSupported } from 'import-without-cache'
67
import isInCi from 'is-in-ci'
78
import { createDebug } from 'obug'
89
import { createConfigCoreLoader } from 'unconfig-core'
@@ -52,11 +53,6 @@ export async function loadViteConfig(
5253
}
5354

5455
const configPrefix = 'tsdown.config'
55-
let noCacheLoad = false
56-
57-
export function setNoCacheLoad(): void {
58-
noCacheLoad = true
59-
}
6056

6157
export async function loadConfigFile(
6258
inlineConfig: InlineConfig,
@@ -110,7 +106,7 @@ export async function loadConfigFile(
110106
sources,
111107
cwd,
112108
stopAt: workspace && path.dirname(workspace),
113-
}).load(noCacheLoad)
109+
}).load(true)
114110

115111
let exported: UserConfigExport = []
116112
let file: string | undefined
@@ -146,15 +142,13 @@ type Parser = 'native' | 'unrun'
146142
function resolveConfigLoader(
147143
configLoader: InlineConfig['configLoader'] = 'auto',
148144
): Parser {
149-
if (noCacheLoad) {
150-
return 'unrun'
151-
} else if (configLoader === 'auto') {
145+
if (configLoader === 'auto') {
152146
const nativeTS = !!(
153147
process.features.typescript ||
154148
process.versions.bun ||
155149
process.versions.deno
156150
)
157-
return nativeTS ? 'native' : 'unrun'
151+
return nativeTS && isSupported ? 'native' : 'unrun'
158152
} else {
159153
return configLoader === 'native' ? 'native' : 'unrun'
160154
}
@@ -184,7 +178,18 @@ function createParser(loader: Parser) {
184178
}
185179

186180
async function nativeImport(id: string) {
187-
const mod = await import(pathToFileURL(id).href).catch((error) => {
181+
const url = pathToFileURL(id)
182+
const importAttributes: Record<string, string> = Object.create(null)
183+
if (isSupported) {
184+
importAttributes.cache = 'no'
185+
init()
186+
} else {
187+
url.searchParams.set('no-cache', crypto.randomUUID())
188+
}
189+
190+
const mod = await import(url.href, {
191+
with: importAttributes,
192+
}).catch((error) => {
188193
const cannotFindModule = error?.message?.includes?.('Cannot find module')
189194
if (cannotFindModule) {
190195
const configError = new Error(

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import path from 'node:path'
22
import { fileURLToPath } from 'node:url'
33
import { bold, green } from 'ansis'
4+
import { clearCJSCache } from 'import-without-cache'
45
import {
56
build as rolldownBuild,
67
watch as rolldownWatch,
78
type BuildOptions,
89
type RolldownWatcher,
910
} from 'rolldown'
10-
import { setNoCacheLoad } from './config/config.ts'
1111
import {
1212
resolveConfig,
1313
type DebugOptions,
@@ -62,8 +62,7 @@ export async function build(
6262
restarting = true
6363

6464
await Promise.all(disposeCbs.map((cb) => cb()))
65-
66-
setNoCacheLoad()
65+
clearCJSCache()
6766
build(userOptions)
6867
}
6968

0 commit comments

Comments
 (0)