-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
68 lines (60 loc) · 1.93 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
65
66
67
68
import { accessSync, constants as fsMode, unlinkSync as remove } from 'node:fs';
import { resolve as pathResolve } from 'node:path';
import { defineConfig } from 'tsup';
import { magenta } from './src';
const resolve = (...args: string[]) => pathResolve(__dirname, ...args);
const print = (label: string) => (text: string) => console.log(label, text);
export default defineConfig((opts) => ({
entry: ['src/index.ts'],
format: ['cjs', 'esm'],
target: ['es2015'],
outDir: 'dist',
bundle: true,
clean: !opts.watch,
minify: false,
treeshake: opts.watch ? false : 'smallest',
sourcemap: false,
splitting: false,
cjsInterop: true,
legacyOutput: false,
dts: true,
async onSuccess() {
if (opts.watch) return;
const label = magenta('HOOK');
const log = print(label);
// The `tsup` will emit separate d.ts and d.mts declarations starting from version 8.0.1,
// but this package does not need it currently. It will only increase the package size.
// See https://github.com/egoist/tsup/pull/934
const srcMtsFile = 'dist/index.d.mts';
const srcMtsFilePath = resolve(srcMtsFile);
const isExists = await wait(srcMtsFilePath);
if (!isExists) return;
remove(srcMtsFilePath);
log(`rm ${magenta(srcMtsFile)}`);
},
}));
function sleep(interval: number) {
return new Promise<void>((res) => {
setTimeout(() => {
res();
}, interval);
});
}
/** Exists sync for file or directory */
function access(path: string) {
try {
accessSync(path, fsMode.F_OK);
return true;
} catch {
return false;
}
}
/** Wait for file */
async function wait(path: string, timeout = 5000, interval = 1000) {
let poll = Math.floor(timeout / interval);
if (poll <= 0) return false;
while (poll--) {
if (access(path)) return true;
await sleep(interval);
}
}