forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdown.config.ts
More file actions
124 lines (119 loc) · 3.75 KB
/
Copy pathtsdown.config.ts
File metadata and controls
124 lines (119 loc) · 3.75 KB
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
import { readFileSync } from 'fs';
import { defineConfig } from 'tsdown';
// Read package.json for version constants
const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'));
// Extract minimum Node.js version from engines field (e.g., ">=20.0.0" → 20)
// This is injected into entrypoint.ts for the version check
// Note: Assumes engines.node is a simple semver constraint like ">=20.0.0"
const enginesNode: string = packageJson.engines?.node ?? '';
let minNodeVersion = parseInt(enginesNode.replace(/[^\d.]/g, ''), 10);
if (Number.isNaN(minNodeVersion)) {
console.warn(
`[tsdown] Warning: Could not parse engines.node "${enginesNode}". Defaulting to Node.js 20.`,
);
minNodeVersion = 20;
}
// Build-time constants injected into all builds
// These replace the __PROMPTFOO_*__ placeholders in source files
// Note: tsdown define requires all values to be strings
const versionDefines = {
__PROMPTFOO_VERSION__: JSON.stringify(packageJson.version),
__PROMPTFOO_POSTHOG_KEY__: JSON.stringify(process.env.PROMPTFOO_POSTHOG_KEY || ''),
__PROMPTFOO_MIN_NODE_VERSION__: String(minNodeVersion),
};
// All configs use clean: false. Use `npm run build:clean` for explicit cleaning.
// This prevents race conditions when multiple configs share the same outDir.
export default defineConfig([
// Server (ESM only) - stable path for workflows
{
entry: { 'server/index': 'src/server/index.ts' },
format: ['esm'],
target: 'node20',
outDir: 'dist/src',
splitting: false,
shims: true,
sourcemap: true,
clean: false,
fixedExtension: false, // Use .js extension for ESM since package.json has type: module
define: {
...versionDefines,
BUILD_FORMAT: '"esm"',
'process.env.BUILD_FORMAT': '"esm"',
},
external: [
// Externalize all bare module imports so Node resolves CJS deps natively
/^[a-z@][^:]*/,
],
},
// CLI binary (ESM only)
{
entry: ['src/entrypoint.ts', 'src/main.ts'],
format: ['esm'],
target: 'node20',
outDir: 'dist/src',
clean: false,
shims: true, // Provides __dirname, __filename shims automatically
sourcemap: true,
fixedExtension: false, // Use .js extension for ESM since package.json has type: module
define: {
...versionDefines,
BUILD_FORMAT: '"esm"',
'process.env.BUILD_FORMAT': '"esm"',
},
outputOptions: {
banner: '#!/usr/bin/env node',
},
external: [
// Externalize all bare module imports so Node resolves CJS deps natively
/^[a-z@][^:]*/,
// Ensure critical native deps remain external
'better-sqlite3',
'playwright',
'sharp',
'@swc/core',
'esbuild',
'fsevents',
],
},
// Library ESM build
{
entry: ['src/index.ts'],
format: ['esm'],
target: 'node20',
outDir: 'dist/src',
splitting: false,
treeshake: true,
sourcemap: true,
shims: true, // Ensure library ESM build has shims
clean: false,
fixedExtension: false, // Use .js extension for ESM since package.json has type: module
define: {
...versionDefines,
BUILD_FORMAT: '"esm"',
'process.env.BUILD_FORMAT': '"esm"',
},
external: [
// Externalize all bare module imports so Node resolves CJS deps natively
/^[a-z@][^:]*/,
],
},
// Library CJS build for compatibility
{
entry: ['src/index.ts'],
format: ['cjs'],
target: 'node20',
outDir: 'dist/src',
sourcemap: true,
clean: false,
fixedExtension: true, // Use .cjs extension for CJS output
define: {
...versionDefines,
BUILD_FORMAT: '"cjs"',
'process.env.BUILD_FORMAT': '"cjs"',
},
external: [
// Externalize all bare module imports so Node resolves CJS deps natively
/^[a-z@][^:]*/,
],
},
]);