-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
64 lines (56 loc) · 1.4 KB
/
tsdown.config.ts
File metadata and controls
64 lines (56 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import path from 'node:path'
import { defineConfig } from 'tsdown/config'
const getPackageName = (filePath: string): string | null => {
// 格式化为绝对路径
const absPath = path.resolve(filePath).replace(/\\/g, '/')
const idx = absPath.lastIndexOf('node_modules/')
if (idx === -1) return null
const suffix = absPath.slice(idx + 13)
const parts = suffix.split('/').filter(Boolean)
if (parts.length === 0) return null
// 组织包 (@scope/package)
if (parts[0].startsWith('@')) {
return parts.length > 1 ? `${parts[0]}/${parts[1]}` : null
}
// 普通包
return parts[0]
}
export default defineConfig({
entry: [
'src/index.ts',
'src/__dirname.ts'
],
outExtensions: (context) => {
if (context.format === 'es') {
return {
js: '.mjs',
dts: '.d.ts',
}
}
return { js: '.js', dts: '.d.ts' }
},
dts: true,
format: ['esm'],
shims: true,
target: 'node20',
platform: 'node',
sourcemap: false,
clean: true,
treeshake: true,
outputOptions (outputOptions) {
outputOptions.advancedChunks = {
groups: [
{
name (id: string) {
if (id.includes('node_modules')) {
return `chunks/${getPackageName(id)}/index.mjs`
}
// 返回 null 表示不干预该模块的分块决定
return null
},
},
],
}
return outputOptions
},
})