-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
85 lines (79 loc) · 2.03 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from 'node:fs';
import path from 'node:path';
import { defineConfig } from 'tsup';
import type { Options } from 'tsup';
type BuildTarget = 'cli' | 'lib';
function createMerge(common: Options) {
return (config: Options) => {
return {
...common,
...config,
};
};
}
export default defineConfig((options) => {
const isDev = !!options.watch;
const isProd = !isDev;
const common: Options = {
clean: true,
target: 'node16',
shims: true,
dts: true,
sourcemap: isDev,
minify: isProd,
esbuildPlugins: [
{
name: 'alias',
setup(build) {
build.onLoad(
{
filter: /.tsx?$/,
},
async ({ path: modulePath }) => {
const raw = (
await fs.promises.readFile(modulePath, 'utf-8')
).toString();
const rootSrc = path.resolve(__dirname, 'src');
const ext = modulePath.endsWith('ts') ? 'ts' : 'js';
const matchResult = raw.match(/(['"])@\/(.+)\1/);
if (!matchResult) {
return {
contents: raw,
loader: ext,
};
}
const [rawContent, quota, remainPath] = matchResult;
const finalContent = raw.replace(
rawContent,
`${quota}${path.join(rootSrc as string, remainPath)}${quota}`
);
return {
contents: finalContent,
loader: ext,
};
}
);
},
},
],
};
const merge = createMerge(common);
const cliConfig = merge({
entry: ['src/cli.ts'],
outDir: 'dist',
format: ['cjs'],
});
const libConfig = merge({
entry: ['src/index.ts'],
outDir: 'lib',
format: ['cjs', 'esm'],
});
if (options.env) {
const { target } = options.env as {
target: BuildTarget;
};
return target === 'cli' ? cliConfig : libConfig;
}
// build all
return [cliConfig, libConfig];
});