-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
38 lines (33 loc) · 1.23 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
import fs from 'fs';
import match from 'micromatch';
import path from 'path';
import { defineConfig } from 'tsup';
import pkg from './package.json';
const patterns = Object.keys(pkg.exports).flatMap((entry) => {
// add extensions to path glob
return path.parse(entry).ext ? entry : [`${entry}.{js,ts}`, `${entry}/index.{js,ts}`];
});
const srcPath = path.join(__dirname, 'src');
type Entry = Record<string, string>;
const entries = fs
.readdirSync(srcPath, { recursive: true, withFileTypes: true })
// filter out directories
.filter((entry) => entry.isFile())
// get filepath relative to src path
.map((entry) => path.relative(srcPath, path.join(entry.parentPath, entry.name)).replace(/\\/g, '/'))
// loop through entries and append matched entries
.reduce<Entry>((acc, filePath) => {
if (match([filePath], patterns)[0]) {
const { name, dir } = path.parse(filePath);
const outPath = path.posix.join(dir, name);
acc[outPath] = path.posix.join('src', filePath);
}
return acc;
}, {});
export default defineConfig((cfg) => {
const commons = { entry: entries, outDir: 'dist', clean: true };
return [
{ ...cfg, ...commons, format: ['esm'], dts: true },
{ ...cfg, ...commons, format: ['cjs'] },
];
});