-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.config.ts
208 lines (171 loc) · 5.28 KB
/
build.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { resolve, dirname } from "pathe";
import { defineBuildConfig } from "unbuild";
import { fileURLToPath } from "url";
/**
* The list of supported bundler names.
*/
export type BundlerName =
| "jsr"
| "bun"
| "copy"
| "mkdist"
| "rollup"
| "untyped";
/**
* BuildPublishConfig describes the configuration for version bumping,
* build output directories, bundler selection, publishing options,
* and additional build settings.
*/
export type BuildPublishConfig = {
/**
* The bump mode for versioning, used if the user didn't provide --bump:
* - "autoPatch": 1.2.3 -> 1.2.4
* - "autoMinor": 1.2.3 -> 1.3.0
* - "autoMajor": 1.2.3 -> 2.0.0
*/
bump: "autoPatch" | "autoMinor" | "autoMajor";
/** Enables more detailed log output */
verbose: boolean;
/** Do a dry run (publish --dry-run) instead of a real publish */
dryRun: boolean;
/** Build only but skip publishing (and do not remove dist folders) */
pausePublish: boolean;
/** Allow publishing with uncommitted changes (--allow-dirty) */
allowDirty: boolean;
/** Allow JSR to use slow types (--allow-slow-types) */
jsrSlowTypes: boolean;
/** Target registry: "npm", "jsr", or "npm-jsr". */
registry: "npm" | "jsr" | "npm-jsr";
/** Output directory for NPM build artifacts */
npmDistDir: string;
/** Output directory for JSR build artifacts */
jsrDistDir: string;
/** Directory where the source code resides */
rootSrcDir: string;
/** Bundler to use for NPM builds */
builderNpm: BundlerName;
/** Bundler to use for JSR builds */
builderJsr: BundlerName;
/** Minify the build output */
shouldMinify: boolean;
/** Split the build output into chunks */
splitting: boolean;
/**
* Whether to generate sourcemaps.
* For Bun/Rollup: boolean | "inline" | "none" | "linked" | "external"
*/
sourcemap: boolean | "inline" | "none" | "linked" | "external";
/** Output format: "esm", "cjs", or "iife" */
format: "esm" | "cjs" | "iife";
/** Build target environment: "node", "bun", or "browser" */
target: "node" | "bun" | "browser";
/** Public path for the output */
publicPath: string;
/**
* Tracks if the version bump is disabled.
* Used to prevent re-bumping on retry.
*/
disableBump: boolean;
/** Indicates which target was last built ("npm" or "jsr")
* Please don't change this unless you know what you're doing.
*/
lastBuildFor: "npm" | "jsr";
/** Flag indicating if the build is for JSR */
isJSR: boolean;
};
const CURRENT_DIR = dirname(fileURLToPath(import.meta.url));
/**
* Default configuration for the publishing script.
*/
export const pubConfig: BuildPublishConfig = {
// Publish config
registry: "npm-jsr",
pausePublish: false,
// Bump config
bump: "autoPatch",
disableBump: false,
// Output directories
npmDistDir: resolve(CURRENT_DIR, "dist-npm"),
jsrDistDir: resolve(CURRENT_DIR, "dist-jsr"),
rootSrcDir: resolve(CURRENT_DIR, "src"),
// Bundler options
builderNpm: "mkdist",
builderJsr: "jsr",
// Build config
format: "esm",
target: "node",
publicPath: "/",
sourcemap: "none",
shouldMinify: true,
splitting: false,
// Publish flags
jsrSlowTypes: true,
allowDirty: true,
dryRun: false,
// Build/pub helpers
verbose: false,
// Build overrides – don't change these manually
lastBuildFor: "npm",
isJSR: false,
};
/**
* Helper to compute the Rollup sourcemap option based on the configuration.
*/
function getRollupSourcemapOption(
sourcemap: boolean | "inline" | "none" | "linked" | "external",
): boolean | "inline" {
if (sourcemap === "none") return false;
if (sourcemap === "inline") return sourcemap;
if (sourcemap === "linked" || sourcemap === "external" || sourcemap)
return true;
return false;
}
/**
* Helper for Bun builds: convert the sourcemap option to a Bun-friendly value.
*/
export function getBunSourcemapOption(
sourcemap: boolean | "inline" | "none" | "linked" | "external",
): "none" | "inline" | "external" {
if (sourcemap === "none" || sourcemap === false) return "none";
if (sourcemap === "inline") return "inline";
// For "linked", "external", or boolean true, return "external"
return "external";
}
// Select the appropriate bundler based on the last build target.
const selectedBuilder =
pubConfig.lastBuildFor === "npm"
? pubConfig.builderNpm
: pubConfig.builderJsr;
const shouldMinify = pubConfig.shouldMinify;
const buildConfig =
// Use defineBuildConfig only if not using the "bun" or "jsr" bundlers.
selectedBuilder !== "bun" && selectedBuilder !== "jsr"
? defineBuildConfig({
declaration: false,
clean: false,
entries: [
{
outDir:
pubConfig.lastBuildFor === "npm"
? `${pubConfig.npmDistDir}/bin`
: `${pubConfig.jsrDistDir}/bin`,
builder: selectedBuilder,
format: pubConfig.format === "esm" ? "esm" : "cjs",
input: "src",
ext: "js",
},
],
rollup: {
emitCJS: false,
inlineDependencies: true,
esbuild: {
target: "es2023",
minify: shouldMinify,
},
output: {
sourcemap: getRollupSourcemapOption(pubConfig.sourcemap),
},
},
})
: undefined;
export default buildConfig;