-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.config.ts
268 lines (221 loc) · 6.81 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { fileURLToPath } from "node:url";
import { resolve, dirname } from "pathe";
import { defineBuildConfig } from "unbuild";
/** ------------------------------------
* COMMON OPTIONS
* ------------------------------------ */
const isCLI = true;
const declaration = false;
const pausePublish = false;
const disableBump = true;
const verbose = false;
/** ------------------------------------
* CONFIGURATION
* ------------------------------------ */
/**
* 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 = {
/** Version bump mode: "autoPatch" (1.2.3 → 1.2.4), "autoMinor" (1.2.3 → 1.3.0), or "autoMajor" (1.2.3 → 2.0.0) */
bump: "autoPatch" | "autoMinor" | "autoMajor";
/** Enables verbose logging */
verbose: boolean;
/** Dry run mode for publishing (does not actually publish) */
dryRun: boolean;
/** Build-only mode: skip publishing and do not remove distribution 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 containing the source code */
rootSrcDir: string;
/** Bundler to use for NPM builds */
builderNpm: BundlerName;
/** Bundler to use for JSR builds */
builderJsr: BundlerName;
/** Whether to minify the build output */
shouldMinify: boolean;
/** Whether to split the build output into chunks */
splitting: boolean;
/**
* Sourcemap configuration.
* Options: boolean, "inline", "none", "linked", or "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 assets */
publicPath: string;
/** Flag to disable version bumping (prevents re-bumping on retry) */
disableBump: boolean;
/** Indicates which target was last built ("npm" or "jsr") */
lastBuildFor: "npm" | "jsr";
/** Flag indicating if the build is for JSR */
isJSR: boolean;
/** Flag indicating if the build is for a CLI package */
isCLI: boolean;
/** Configuration for libraries to be built and published */
libs?: Record<string, LibConfig>;
};
/**
* Configuration for a library to be built and published.
*/
export type LibConfig = {
/** The path to the main entry file for the library */
main: string;
/** Optional description for the library */
description?: string;
};
const ROOT_DIR = dirname(fileURLToPath(import.meta.url));
/**
* Default configuration for the publishing script.
*/
export const pubConfig: BuildPublishConfig = {
// Publish configuration
registry: "npm-jsr",
pausePublish,
// Bump configuration
bump: "autoPatch",
disableBump,
// Output directories
npmDistDir: resolve(ROOT_DIR, "dist-npm"),
jsrDistDir: resolve(ROOT_DIR, "dist-jsr"),
rootSrcDir: resolve(ROOT_DIR, "src"),
// Bundler options
builderNpm: "mkdist",
builderJsr: "jsr",
// Build configuration
format: "esm",
target: "node",
publicPath: "/",
sourcemap: "none",
shouldMinify: true,
splitting: false,
// Publish flags
jsrSlowTypes: true,
allowDirty: true,
dryRun: false,
// Helpers
verbose,
isCLI,
// Build overrides – do not modify these manually
lastBuildFor: "npm",
isJSR: false,
// Libs config
libs: {
"@reliverse/config": {
main: "src/libs/config/config-main.ts",
description: "Configuration utilities for @reliverse/cli",
},
"@reliverse/sdk": {
main: "src/libs/sdk/sdk-main.ts",
description: "SDK for interacting with @reliverse/cli and reliverse.org",
},
},
};
/**
* Computes the Rollup sourcemap option based on the given configuration.
* @param sourcemap - Sourcemap configuration.
* @returns "inline" if inline is specified; true for linked/external or boolean true; otherwise false.
*/
function getRollupSourcemapOption(
sourcemap: boolean | "inline" | "none" | "linked" | "external",
): boolean | "inline" {
switch (sourcemap) {
case "none":
return false;
case "inline":
return "inline";
case "linked":
case "external":
return true;
default:
return !!sourcemap;
}
}
/**
* Converts the sourcemap option to a Bun-friendly value.
* @param sourcemap - Sourcemap configuration.
* @returns "none", "inline", or "external".
*/
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";
}
// Determine the appropriate bundler based on the last build target.
const selectedBuilder: BundlerName =
pubConfig.lastBuildFor === "npm"
? pubConfig.builderNpm
: pubConfig.builderJsr;
const shouldMinify = pubConfig.shouldMinify;
// Compute the output directory for build entries.
const outputBinDir =
pubConfig.lastBuildFor === "npm"
? resolve(pubConfig.npmDistDir, "bin")
: resolve(pubConfig.jsrDistDir, "bin");
// Toggle flag for library builds – similar to lastBuildFor toggling.
export const isNextBuildLib = {
// @reliverse/config
config: false,
};
/**
* Build configuration using unbuild.
* Only defined when not using "bun" or "jsr" bundlers.
*/
const buildConfig =
selectedBuilder !== "bun" && selectedBuilder !== "jsr"
? defineBuildConfig({
declaration,
clean: false,
entries: [
{
input: isNextBuildLib.config
? "src/utils/libs/config/schemaConfig.ts"
: "src",
outDir: isNextBuildLib.config
? `dist-libs/config/${outputBinDir}`
: outputBinDir,
builder: selectedBuilder,
format: pubConfig.format === "esm" ? "esm" : "cjs",
ext: "js",
},
],
rollup: {
emitCJS: false,
inlineDependencies: true,
esbuild: {
target: "es2023",
minify: shouldMinify,
},
output: {
sourcemap: getRollupSourcemapOption(pubConfig.sourcemap),
},
},
})
: undefined;
export default buildConfig;