-
Notifications
You must be signed in to change notification settings - Fork 2
/
tsup.config.ts
50 lines (44 loc) · 1.24 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
import * as fs from 'node:fs';
import type { Plugin } from 'esbuild';
import { defineConfig } from 'tsup';
const injectShimsPlugin: Plugin = {
name: 'inject-shims-plugin',
setup(build) {
const outputFilePath = 'dist/bin/index.js';
const scriptFilePath = './scripts/cjs-shim.ts';
const startFromLine = 1;
build.onEnd((result) => {
// Read the script content from the specified file
let scriptToInject = '';
try {
scriptToInject = fs.readFileSync(scriptFilePath, 'utf8');
} catch (err) {
console.error(`Failed to read the script file ${scriptFilePath}:`, err);
return;
}
result.outputFiles = result.outputFiles?.map((outputFile) => {
if (!outputFile.path.endsWith(outputFilePath)) {
return outputFile;
}
const text = outputFile.text.split('\n');
text.splice(startFromLine, 0, scriptToInject);
return {
...outputFile,
text: text.join('\n'),
};
});
});
},
};
export default defineConfig({
entry: [
'src/index.ts',
'src/bin/index.ts',
'src/types/index.ts',
'src/schema/index.ts',
'src/utils/index.ts',
],
format: 'esm',
dts: true,
esbuildPlugins: [injectShimsPlugin],
});