Skip to content

Commit 24f2751

Browse files
committed
fix(workspace): cwd defaults to config dirname, support filter for all configs
1 parent 3b89705 commit 24f2751

File tree

6 files changed

+62
-45
lines changed

6 files changed

+62
-45
lines changed

packages/create-tsdown/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"access": "public"
4242
},
4343
"scripts": {
44-
"dev": "node ./src/run.ts"
44+
"dev": "node ./src/run.ts",
45+
"build": "unrun ../../src/run.ts -c ../../tsdown.config.ts -F ."
4546
},
4647
"dependencies": {
4748
"@clack/prompts": "catalog:create",

packages/migrate/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"access": "public"
4242
},
4343
"scripts": {
44-
"dev": "node ./src/run.ts"
44+
"dev": "node ./src/run.ts",
45+
"build": "unrun ../../src/run.ts -c ../../tsdown.config.ts -F ."
4546
},
4647
"dependencies": {
4748
"@antfu/ni": "catalog:migrate",

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ cli
6060
.option('-W, --workspace [dir]', 'Enable workspace mode')
6161
.option(
6262
'-F, --filter <pattern>',
63-
'Filter workspace packages, e.g. /regex/ or substring',
63+
'Filter configs (cwd or name), e.g. /pkg-name$/ or pkg-name',
6464
)
6565
.option(
6666
'--exports',

src/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function loadConfigFile(
132132
}
133133

134134
return {
135-
configs: exported,
135+
configs: exported.map((config) => ({ cwd, ...config })),
136136
file,
137137
}
138138
}

src/config/index.ts

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030

3131
export * from './types.ts'
3232

33-
const debug = createDebug('tsdown:options')
33+
const debugLog = createDebug('tsdown:options')
3434

3535
const DEFAULT_EXCLUDE_WORKSPACE = [
3636
'**/node_modules/**',
@@ -52,16 +52,16 @@ export async function resolveConfig(inlineConfig: InlineConfig): Promise<{
5252
configs: ResolvedConfig[]
5353
files: string[]
5454
}> {
55-
debug('inline config %O', inlineConfig)
55+
debugLog('inline config %O', inlineConfig)
5656

5757
const { configs: rootConfigs, file } = await loadConfigFile(inlineConfig)
5858
const files: string[] = []
5959
if (file) {
6060
files.push(file)
61-
debug('loaded root user config file %s', file)
62-
debug('root user configs %O', rootConfigs)
61+
debugLog('loaded root user config file %s', file)
62+
debugLog('root user configs %O', rootConfigs)
6363
} else {
64-
debug('no root user config file found')
64+
debugLog('no root user config file found')
6565
}
6666

6767
const configs: ResolvedConfig[] = (
@@ -72,15 +72,22 @@ export async function resolveConfig(inlineConfig: InlineConfig): Promise<{
7272
if (workspaceFiles) {
7373
files.push(...workspaceFiles)
7474
}
75-
return Promise.all(
76-
workspaceConfigs
77-
.filter((config) => !config.workspace || config.entry)
78-
.map((config) => resolveUserConfig(config, inlineConfig)),
79-
)
75+
return (
76+
await Promise.all(
77+
workspaceConfigs
78+
.filter((config) => !config.workspace || config.entry)
79+
.map((config) => resolveUserConfig(config, inlineConfig)),
80+
)
81+
).filter((config) => !!config)
8082
}),
8183
)
8284
).flat()
83-
debug('resolved configs %O', configs)
85+
debugLog('resolved configs %O', configs)
86+
87+
if (configs.length === 0) {
88+
throw new Error('No valid configuration found.')
89+
}
90+
8491
return { configs, files }
8592
}
8693

@@ -89,7 +96,8 @@ async function resolveWorkspace(
8996
inlineConfig: InlineConfig,
9097
): Promise<{ configs: UserConfig[]; files?: string[] }> {
9198
const normalized = { ...config, ...inlineConfig }
92-
const rootCwd = normalized.cwd || process.cwd()
99+
const rootCwd = config.cwd!
100+
93101
let { workspace } = normalized
94102
if (!workspace) return { configs: [normalized], files: [] }
95103

@@ -130,25 +138,11 @@ async function resolveWorkspace(
130138
throw new Error('No workspace packages found, please check your config')
131139
}
132140

133-
if (inlineConfig.filter) {
134-
inlineConfig.filter = resolveRegex(inlineConfig.filter)
135-
packages = packages.filter((path) => {
136-
return typeof inlineConfig.filter === 'string'
137-
? path.includes(inlineConfig.filter)
138-
: Array.isArray(inlineConfig.filter)
139-
? inlineConfig.filter.some((filter) => path.includes(filter))
140-
: inlineConfig.filter!.test(path)
141-
})
142-
if (packages.length === 0) {
143-
throw new Error('No packages matched the filters')
144-
}
145-
}
146-
147141
const files: string[] = []
148142
const configs = (
149143
await Promise.all(
150144
packages.map(async (cwd) => {
151-
debug('loading workspace config %s', cwd)
145+
debugLog('loading workspace config %s', cwd)
152146
const { configs, file } = await loadConfigFile(
153147
{
154148
...inlineConfig,
@@ -158,17 +152,13 @@ async function resolveWorkspace(
158152
cwd,
159153
)
160154
if (file) {
161-
debug('loaded workspace config file %s', file)
155+
debugLog('loaded workspace config file %s', file)
162156
files.push(file)
163157
} else {
164-
debug('no workspace config file found in %s', cwd)
158+
debugLog('no workspace config file found in %s', cwd)
165159
}
166160
return configs.map(
167-
(config): UserConfig => ({
168-
...normalized,
169-
cwd,
170-
...config,
171-
}),
161+
(config): UserConfig => ({ ...normalized, ...config }),
172162
)
173163
}),
174164
)
@@ -177,10 +167,29 @@ async function resolveWorkspace(
177167
return { configs, files }
178168
}
179169

170+
function filterConfig(
171+
filter: InlineConfig['filter'],
172+
configCwd: string,
173+
name?: string,
174+
) {
175+
if (!filter) return true
176+
177+
let cwd = path.relative(process.cwd(), configCwd)
178+
if (cwd === '') cwd = '.'
179+
180+
if (filter instanceof RegExp) {
181+
return (name && filter.test(name)) || filter.test(cwd)
182+
}
183+
184+
return toArray(filter).some(
185+
(value) => (name && name === value) || cwd === value,
186+
)
187+
}
188+
180189
async function resolveUserConfig(
181190
userConfig: UserConfig,
182191
inlineConfig: InlineConfig,
183-
): Promise<ResolvedConfig> {
192+
): Promise<ResolvedConfig | undefined> {
184193
let {
185194
entry,
186195
format = ['es'],
@@ -228,6 +237,16 @@ async function resolveUserConfig(
228237
debug = false,
229238
} = userConfig
230239

240+
const pkg = await readPackageJson(cwd)
241+
if (workspace) {
242+
name ||= pkg?.name
243+
}
244+
245+
if (!filterConfig(inlineConfig.filter, cwd, name)) {
246+
debugLog('[filter] skipping config %s', cwd)
247+
return
248+
}
249+
231250
const logger = createLogger(logLevel, {
232251
customLogger,
233252
failOnWarn: resolveFeatureOption(failOnWarn, true),
@@ -253,10 +272,6 @@ async function resolveUserConfig(
253272
outDir = path.resolve(cwd, outDir)
254273
clean = resolveClean(clean, outDir, cwd)
255274

256-
const pkg = await readPackageJson(cwd)
257-
if (workspace) {
258-
name ||= pkg?.name
259-
}
260275
entry = await resolveEntry(logger, entry, cwd, name)
261276
if (dts == null) {
262277
dts = !!(pkg?.types || pkg?.typings || hasExportsTypes(pkg))

src/config/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,9 @@ export interface InlineConfig extends UserConfig {
499499
configLoader?: 'auto' | 'native' | 'unconfig' | 'unrun'
500500

501501
/**
502-
* Filter workspace packages. This option is only available in workspace mode.
502+
* Filter configs by cwd or name.
503503
*/
504-
filter?: RegExp | string | string[]
504+
filter?: RegExp | Arrayable<string>
505505
}
506506

507507
export type UserConfigFn = (

0 commit comments

Comments
 (0)