-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
130 lines (115 loc) · 3.72 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { isolatedDeclaration } from 'oxc-transform';
import type { Options } from 'tsup';
import { defineConfig } from 'tsup';
const pkgName = 'react-querybuilder_chakra2';
const generateDTS = async (projDir: string): Promise<void> => {
const g = new Bun.Glob('**/*.{ts,tsx}');
const files = g.scan(path.join(projDir, '/src/'));
let fileCount = 0;
for await (const filePath of files) {
// Skip test and test utility files
if (
/(_internal|\.test|(t|T)estUtils)\.tsx?$/.test(filePath) ||
filePath.startsWith('internal/')
) {
continue;
}
const originalSource = await Bun.file(path.join(projDir, '/src/', filePath)).text();
const { code } = isolatedDeclaration(filePath, originalSource);
// Write the CJS DTS file
await Bun.write(path.join(projDir, '/dist/types/', filePath.replace(/\.tsx?$/, '.d.ts')), code);
// Convert DTS file to ESM-in-CJS-context
const lines = code.split('\n');
for (const line in lines) {
const eximLine = lines[line].match(/^(ex|im)port .* from "(\..*)";$/);
if (eximLine) {
const resolvedExImPath = path.join(projDir, '/src/', path.parse(filePath).dir, eximLine[2]);
if (
(await Bun.file(`${resolvedExImPath}.ts`).exists()) ||
(await Bun.file(`${resolvedExImPath}.tsx`).exists())
) {
lines[line] = lines[line].replace(/";$/, `.mjs";`);
} else if (
(await Bun.file(`${resolvedExImPath}/index.ts`).exists()) ||
(await Bun.file(`${resolvedExImPath}/index.tsx`).exists())
) {
lines[line] = lines[line].replace(/";$/, `/index.mjs";`);
}
}
}
await Bun.write(
path.join(projDir, '/dist/types-esm/', filePath.replace(/\.tsx?$/, '.d.mts')),
lines.join('\n')
);
fileCount++;
}
console.log(`${fileCount} DTS files generated.`);
};
export default defineConfig(async options => {
const entryPoint = `src/index.tsx`;
const commonOptions = {
entry: { [pkgName]: entryPoint },
sourcemap: true,
...options,
} satisfies Options;
const productionOptions = {
minify: true,
replaceNodeEnv: true,
} satisfies Options;
const opts: Options[] = [
// ESM, standard bundler dev, embedded `process` references
{
...commonOptions,
format: 'esm',
clean: true,
onSuccess: () => generateDTS(import.meta.dir),
},
// ESM, Webpack 4 support. Target ES2017 syntax to compile away optional chaining and spreads
{
...commonOptions,
entry: { [`${pkgName}.legacy-esm`]: entryPoint },
// ESBuild outputs `'.mjs'` by default for the 'esm' format. Force '.js'
outExtension: () => ({ js: '.js' }),
target: 'es2017',
format: 'esm',
},
// ESM for use in browsers. Minified, with `process` compiled away
{
...commonOptions,
...productionOptions,
entry: { [`${pkgName}.production`]: entryPoint },
format: 'esm',
outExtension: () => ({ js: '.mjs' }),
},
// CJS development
{
...commonOptions,
entry: { [`${pkgName}.cjs.development`]: entryPoint },
format: 'cjs',
outDir: './dist/cjs/',
},
// CJS production
{
...commonOptions,
...productionOptions,
entry: { [`${pkgName}.cjs.production`]: entryPoint },
format: 'cjs',
outDir: './dist/cjs/',
onSuccess: async () => {
await writeFile(
`dist/cjs/index.js`,
`'use strict';
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${pkgName}.cjs.production.js');
} else {
module.exports = require('./${pkgName}.cjs.development.js');
}
`
);
},
},
];
return opts;
});