forked from Shopify/hydrogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsup.config.ts
57 lines (54 loc) · 1.52 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
import path from 'node:path';
import fs from 'node:fs/promises';
import {defineConfig} from 'tsup';
const commonConfig = {
minify: false,
bundle: false,
splitting: true,
treeshake: true,
sourcemap: true,
};
export default defineConfig([
{
...commonConfig,
format: 'esm',
// Force bundling types in files here so that they can later be
// bundled in Hydrogen. Otherwise, TSUP fails to bundle them later.
dts: {entry: ['src/index.ts', 'src/patch.ts']},
entry: ['src/**/*.ts'],
outDir: 'dist/esm',
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, ''));
},
},
{
...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');"),
);
}),
);
},
},
],
},
]);