forked from h3ravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
80 lines (76 loc) · 2.93 KB
/
tsdown.config.ts
File metadata and controls
80 lines (76 loc) · 2.93 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { type UserConfig, defineConfig } from 'tsdown'
import { copyFile, glob, mkdir, readFile, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { exists, findUpConfig } from './utils/fs'
export const baseConfig: UserConfig = {
dts: true,
clean: true,
shims: true,
entry: ['src/index.ts'],
format: ['esm', 'cjs'],
sourcemap: false,
exports: true,
outExtensions: (e) => {
return ({
js: e.format === 'es' ? '.js' : '.cjs',
dts: '.d.ts'
})
},
hooks (hooks) {
hooks.hook('build:done', async (ctx) => {
try {
// Get the absolute output directory
const outDir = ctx.options.outDir ?? 'dist'
// Get the absolute base directory
const base = await findUpConfig('framework', 'package', ['json'])
if (!base) return
// Make globale DTS partern
const gdts = base.replace('package.json', 'packages/**/src/*.d.ts')
// Make globale stubs partern
const stubs = base.replace('package.json', 'packages/**/src/**/*.stub')
for await (const entry of glob([gdts, stubs])) {
const target = entry.replace('src', 'dist')
// Ensure the target dir exists
if (await exists(entry) && !await exists(path.dirname(target)))
await mkdir(path.dirname(target))
// Copy required files only for current package
if (await exists(entry) && entry.includes(ctx.options.cwd))
copyFile(entry, target)
// Augment the d.ts file to the index.d.ts
if (entry.includes('.d.ts')) {
for await (const indexFile of glob(path.join(outDir, 'index.d.*ts'))) {
const reference = `/// <reference path="./${path.basename(entry)}" />\n`
if (await exists(indexFile)) {
let content = await readFile(indexFile, 'utf8')
// Only add if it’s not already there
if (!content.includes(reference.trim())) {
content = reference + content
await writeFile(indexFile, content, 'utf8')
}
}
}
}
}
} catch { /** */ }
})
},
external: [
'fs',
'os',
'tsx',
'path',
'tsdown',
'dotenv',
'crypto',
'rollup',
'esbuild',
'edge.js',
'nodemailer',
'typescript',
/^@h3ravel\/.*/gi,
/^node:.*/gi,
/.*\/promises$/gi,
'fs-readdir-recursive',
],
}
export default defineConfig(baseConfig)