-
Notifications
You must be signed in to change notification settings - Fork 276
/
tsup.config.ts
81 lines (76 loc) · 2.38 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
import path from 'node:path';
import fs from 'node:fs/promises';
import {defineConfig} from 'tsup';
import {generateDtsBundle} from 'dts-bundle-generator';
const commonConfig = {
minify: false,
bundle: false,
splitting: true,
treeshake: true,
sourcemap: true,
};
export default defineConfig([
{
...commonConfig,
format: 'esm',
entry: ['src/**/*.ts'],
outDir: 'dist/esm',
// TSUP does not bundle types properly for this package,
// use dts-bundle-generator instead.
dts: false,
async onSuccess() {
const schemaFile = 'dist/esm/schema.js';
const content = await fs.readFile(schemaFile, 'utf8');
// Uncomment createRequire for ESM:
await fs.writeFile(schemaFile, content.replace(/\/\/!/g, ''));
const [dts] = await generateDtsBundle([
{
filePath: './src/index.ts',
libraries: {
// Bundle types from these libraries to avoid
// the need to install them as dependencies in consumers.
// Specifically, 'type-fest' might be installed in a consumer
// with an older version that doesn't include `IsNever` or
// a similar type.
inlinedLibraries: ['@shopify/graphql-codegen', 'type-fest'],
},
output: {noBanner: true},
},
]);
await fs.writeFile(
'dist/esm/index.d.ts',
// For some reason, dts-bundle-generator exports PresetConfig
// from @shopify/graphql-codegen, which conflicts with the
// overwritten type in src/preset.ts. This is a workaround.
dts.replace('export type PresetConfig', 'type PresetConfig'),
);
},
},
{
...commonConfig,
format: 'cjs',
dts: false,
entry: ['src/**/*.ts'],
outDir: 'dist/cjs',
plugins: [
{
// Replace .js with .cjs in require() calls:
name: 'replace-require-extension',
async buildEnd({writtenFiles}) {
await Promise.all(
writtenFiles
.filter(({name}) => name.endsWith('.cjs'))
.map(async ({name}) => {
const filepath = path.resolve('.', name);
const contents = await fs.readFile(filepath, 'utf8');
await fs.writeFile(
filepath,
contents.replace(/\.js'\);/g, ".cjs');"),
);
}),
);
},
},
],
},
]);