-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
52 lines (52 loc) · 1.37 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
import { defineConfig } from 'tsup';
import fs from 'node:fs';
import path from 'node:path';
import { findUp } from 'find-up';
export default defineConfig({
entry: ['src/index.ts'],
// **/!(*.d).ts
shims: true,
legacyOutput: true,
clean: true,
target: 'node16',
format: ['esm'],
dts: true,
esbuildPlugins: [
{
name: 'alias',
setup(build) {
build.onLoad(
{
filter: /.tsx?$/,
},
async ({ path: modulePath }) => {
const raw = (
await fs.promises.readFile(modulePath, 'utf-8')
).toString();
const rootSrc = await findUp('src', {
cwd: path.dirname(modulePath),
type: 'directory',
});
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,
};
}
);
},
},
],
});