Skip to content

Commit bcb7a62

Browse files
committed
feat: support object entry
closes #341
1 parent 0ce7bb7 commit bcb7a62

File tree

7 files changed

+87
-11
lines changed

7 files changed

+87
-11
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"hookable": "catalog:prod",
9999
"import-without-cache": "catalog:prod",
100100
"obug": "catalog:prod",
101+
"picomatch": "catalog:prod",
101102
"rolldown": "catalog:prod",
102103
"rolldown-plugin-dts": "catalog:prod",
103104
"semver": "catalog:prod",
@@ -113,6 +114,7 @@
113114
"@sxzz/prettier-config": "catalog:dev",
114115
"@sxzz/test-utils": "catalog:dev",
115116
"@types/node": "catalog:dev",
117+
"@types/picomatch": "catalog:dev",
116118
"@types/semver": "catalog:dev",
117119
"@typescript/native-preview": "catalog:dev",
118120
"@unocss/eslint-plugin": "catalog:docs",

pnpm-lock.yaml

Lines changed: 17 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ catalogs:
1515
'@sxzz/prettier-config': ^2.2.6
1616
'@sxzz/test-utils': ^0.5.14
1717
'@types/node': ^25.0.2
18+
'@types/picomatch': ^4.0.2
1819
'@types/semver': ^7.7.1
1920
'@typescript/native-preview': 7.0.0-dev.20251216.1
2021
'@vitest/coverage-v8': 4.0.15
@@ -65,6 +66,7 @@ catalogs:
6566
is-in-ci: ^2.0.0
6667
obug: ^2.1.1
6768
package-manager-detector: ^1.6.0
69+
picomatch: ^4.0.3
6870
rolldown: 1.0.0-beta.53
6971
rolldown-plugin-dts: ^0.18.3
7072
semver: ^7.7.3

src/features/entry.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,34 @@ describe('toObjectEntry', () => {
4444
})
4545
})
4646

47+
test('object entry with globs', async (context) => {
48+
const { testDir } = await writeFixtures(context, {
49+
'index.ts': '',
50+
'src/foo.ts': '',
51+
'src/bar.ts': '',
52+
'test/test.ts': '',
53+
'nested/a/b/c.ts': '',
54+
})
55+
const result = await toObjectEntry(
56+
{
57+
'*.min': '*.ts',
58+
'lib/*': 'src/*.ts',
59+
test: 'test/test.ts',
60+
test2: 'test/*.ts',
61+
'nested/*': 'nested/**/*.ts',
62+
},
63+
testDir,
64+
)
65+
expect(result).toEqual({
66+
'index.min': path.join(testDir, 'index.ts'),
67+
'lib/foo': path.join(testDir, 'src/foo.ts'),
68+
'lib/bar': path.join(testDir, 'src/bar.ts'),
69+
test: 'test/test.ts',
70+
test2: 'test/*.ts',
71+
'nested/a/b/c': path.join(testDir, 'nested/a/b/c.ts'),
72+
})
73+
})
74+
4775
test('glob pattern', async (context) => {
4876
const { testDir } = await writeFixtures(context, {
4977
'src/foo.ts': '',

src/features/entry.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path'
2+
import picomatch from 'picomatch'
23
import { glob, isDynamicPattern } from 'tinyglobby'
3-
import { fsExists, lowestCommonAncestor } from '../utils/fs.ts'
4+
import { fsExists, lowestCommonAncestor, stripExtname } from '../utils/fs.ts'
45
import { slash } from '../utils/general.ts'
56
import type { UserConfig } from '../config/index.ts'
67
import type { Logger } from '../utils/logger.ts'
@@ -45,7 +46,32 @@ export async function toObjectEntry(
4546
entry = [entry]
4647
}
4748
if (!Array.isArray(entry)) {
48-
return entry
49+
// resolve object entry with globs
50+
return Object.fromEntries(
51+
(
52+
await Promise.all(
53+
Object.entries(entry).map(async ([key, value]) => {
54+
if (!key.includes('*')) return [[key, value]]
55+
56+
const valueGlob = picomatch.scan(value)
57+
const files = await glob(value, {
58+
cwd,
59+
expandDirectories: false,
60+
})
61+
62+
return files.map((file) => [
63+
slash(
64+
key.replaceAll(
65+
'*',
66+
stripExtname(path.relative(valueGlob.base, file)),
67+
),
68+
),
69+
path.resolve(cwd, file),
70+
])
71+
}),
72+
)
73+
).flat(),
74+
)
4975
}
5076

5177
const isGlob = entry.some((e) => isDynamicPattern(e))
@@ -66,12 +92,7 @@ export async function toObjectEntry(
6692
return Object.fromEntries(
6793
resolvedEntry.map((file) => {
6894
const relative = path.relative(base, file)
69-
return [
70-
slash(
71-
relative.slice(0, relative.length - path.extname(relative).length),
72-
),
73-
file,
74-
]
95+
return [slash(stripExtname(relative)), file]
7596
}),
7697
)
7798
}

src/features/pkg/exports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readFile, writeFile } from 'node:fs/promises'
22
import path from 'node:path'
33
import { RE_DTS } from 'rolldown-plugin-dts/filename'
44
import { detectIndentation } from '../../utils/format.ts'
5+
import { stripExtname } from '../../utils/fs.ts'
56
import { matchPattern, slash } from '../../utils/general.ts'
67
import type { NormalizedFormat, ResolvedConfig } from '../../config/types.ts'
78
import type {
@@ -128,8 +129,7 @@ export async function generateExports(
128129

129130
for (const chunk of filteredChunks) {
130131
const normalizedName = slash(chunk.fileName)
131-
const ext = path.extname(chunk.fileName)
132-
let name = normalizedName.slice(0, -ext.length)
132+
let name = stripExtname(normalizedName)
133133

134134
const isDts = name.endsWith('.d')
135135
if (isDts) {

src/utils/fs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { access, cp, rm, stat } from 'node:fs/promises'
2-
import { dirname, normalize, sep } from 'node:path'
2+
import { dirname, extname, normalize, sep } from 'node:path'
33
import type { Stats } from 'node:fs'
44

55
export function fsExists(path: string): Promise<boolean> {
@@ -45,3 +45,9 @@ export function lowestCommonAncestor(...filepaths: string[]): string {
4545
? sep + ancestor[0]
4646
: ancestor.join(sep)
4747
}
48+
49+
export function stripExtname(filePath: string): string {
50+
const ext = extname(filePath)
51+
if (!ext.length) return filePath
52+
return filePath.slice(0, -ext.length)
53+
}

0 commit comments

Comments
 (0)