-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
64 lines (53 loc) · 1.79 KB
/
tsup.config.ts
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
/* eslint-disable no-undef */
import { defineConfig } from 'tsup';
import * as path from 'path';
import * as fs from 'fs';
const OUT_DIR = path.resolve(__dirname, 'lib');
const SRC_DIR = path.resolve(__dirname, 'src');
export default defineConfig(async () => {
const entry = await makeEntryMap(SRC_DIR);
return {
entry,
outDir: OUT_DIR,
format: ['cjs', 'esm'],
splitting: false,
sourcemap: true,
clean: true,
treeshake: true,
platform: 'neutral',
target: 'es5',
minify: true,
dts: { entry },
bundle: true,
};
});
/**
* Make a entry map for tsup.
* Entry map is a mapping between entry name and path to the file.
* It will treat all nested files as entry points, except files starting with `_`.
*/
async function makeEntryMap(dirPath: string, aliases?: Record<string, string>) {
const dir = await fs.promises.opendir(dirPath);
const entries: Record<string, string> = {};
for await (const dirent of dir) {
const isFile = dirent.isFile();
const isDir = dirent.isDirectory();
const isTsFile = isFile && dirent.name.endsWith('.ts');
const isValidEntryFile = !dirent.name.startsWith('_') && (isDir || isTsFile);
if (!isValidEntryFile) {
continue;
}
if (dirent.isDirectory()) {
const subDirPath = path.resolve(dirPath, dirent.name);
Object.assign(entries, await makeEntryMap(subDirPath, aliases));
}
if (dirent.isFile()) {
const name = dirent.name.split('.')[0];
const alias = aliases?.[name] ?? name;
const direntPath = path.resolve(dirPath, dirent.name);
entries[alias] = direntPath;
continue;
}
}
return entries;
}