bunup 0.8.47 → 0.8.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,376 +0,0 @@
1
- import {
2
- filterBunupBunPlugins,
3
- filterBunupPlugins,
4
- runPluginBuildDoneHooks,
5
- runPluginBuildStartHooks
6
- } from "./chunk-snvybwa2.js";
7
- import {
8
- loadPackageJson
9
- } from "./chunk-gh7z7s46.js";
10
- import {
11
- BunupBuildError,
12
- cleanOutDir,
13
- cleanPath,
14
- ensureArray,
15
- formatFileSize,
16
- getDefaultDtsExtention,
17
- getDefaultOutputExtension,
18
- getFilesFromGlobs,
19
- getPackageDeps,
20
- getShortFilePath,
21
- link,
22
- logTable,
23
- logger,
24
- setSilent
25
- } from "./chunk-a76fsvj7.js";
26
-
27
- // src/build.ts
28
- import path from "path";
29
- import { generateDts, logIsolatedDeclarationErrors } from "bun-dts";
30
- import pc2 from "picocolors";
31
-
32
- // src/plugins/internal/linter.ts
33
- var rules = [
34
- {
35
- check: (ctx) => {
36
- const hasMinification = !!(ctx.options.minify || ctx.options.minifyWhitespace || ctx.options.minifyIdentifiers || ctx.options.minifySyntax);
37
- return hasMinification && !ctx.options.sourcemap;
38
- },
39
- message: `You are using minification without source maps. Consider enabling source maps to help with debugging minified code. Learn more: ${link("https://bunup.dev/docs/guide/options#source-maps")}`,
40
- logLevel: "recommended"
41
- }
42
- ];
43
- function linter() {
44
- return {
45
- type: "bunup",
46
- name: "linter",
47
- hooks: {
48
- onBuildDone: (ctx) => {
49
- let hasWarnings = false;
50
- for (const rule of rules) {
51
- if (rule.check(ctx)) {
52
- if (!hasWarnings) {
53
- logger.space();
54
- }
55
- logger[rule.logLevel ?? "warn"](rule.message);
56
- hasWarnings = true;
57
- }
58
- }
59
- }
60
- }
61
- };
62
- }
63
-
64
- // src/plugins/internal/report.ts
65
- import pc from "picocolors";
66
- function report() {
67
- return {
68
- type: "bunup",
69
- name: "report",
70
- hooks: {
71
- onBuildDone: async ({ options, output }) => {
72
- if (options.watch)
73
- return;
74
- const files = await Promise.all(output.files.map(async (file) => {
75
- const name = file.relativePathToRootDir;
76
- const size = Bun.file(file.fullPath).size;
77
- const gzipSize = Bun.gzipSync(new Uint8Array(await Bun.file(file.fullPath).arrayBuffer())).length;
78
- const formattedGzipSize = formatFileSize(gzipSize);
79
- return {
80
- name,
81
- size,
82
- formattedSize: formatFileSize(size),
83
- gzipSize,
84
- formattedGzipSize
85
- };
86
- }));
87
- const totalSize = files.reduce((sum, file) => sum + file.size, 0);
88
- const formattedTotalSize = formatFileSize(totalSize);
89
- const totalGzipSize = files.reduce((sum, file) => sum + (file.gzipSize || 0), 0);
90
- const formattedTotalGzipSize = formatFileSize(totalGzipSize);
91
- const columns = [
92
- { header: "File", align: "left", color: pc.blue },
93
- { header: "Size", align: "right", color: pc.green },
94
- {
95
- header: "Gzip",
96
- align: "right",
97
- color: pc.magenta
98
- }
99
- ];
100
- const data = files.map((file) => {
101
- return {
102
- File: file.name,
103
- Size: file.formattedSize,
104
- Gzip: file.formattedGzipSize
105
- };
106
- });
107
- const footer = {
108
- File: "Total",
109
- Size: formattedTotalSize,
110
- Gzip: formattedTotalGzipSize
111
- };
112
- logger.space();
113
- logTable(columns, data, footer);
114
- logger.space();
115
- }
116
- }
117
- };
118
- }
119
-
120
- // src/plugins/internal/use-client.ts
121
- function useClient() {
122
- return {
123
- type: "bunup",
124
- name: "use-client",
125
- hooks: {
126
- onBuildDone: async ({ output }) => {
127
- for (const file of output.files) {
128
- let text = await Bun.file(file.fullPath).text();
129
- const hasUseClient = text.split(`
130
- `).some((line) => line.trim().startsWith(`"use client";`));
131
- if (hasUseClient) {
132
- text = text.replaceAll(`"use client";`, "");
133
- text = `"use client";
134
- ${text}`;
135
- }
136
- await Bun.write(file.fullPath, text);
137
- }
138
- }
139
- }
140
- };
141
- }
142
-
143
- // src/options.ts
144
- var DEFAULT_OPTIONS = {
145
- entry: ["src/index.ts"],
146
- format: ["cjs"],
147
- outDir: "dist",
148
- target: "node",
149
- clean: true
150
- };
151
- function createBuildOptions(partialOptions) {
152
- const options = {
153
- ...DEFAULT_OPTIONS,
154
- ...partialOptions
155
- };
156
- return {
157
- ...options,
158
- plugins: [...options.plugins ?? [], useClient(), linter(), report()]
159
- };
160
- }
161
- function getResolvedMinify(options) {
162
- const { minify, minifyWhitespace, minifyIdentifiers, minifySyntax } = options;
163
- const defaultValue = minify === true;
164
- return {
165
- whitespace: minifyWhitespace ?? defaultValue,
166
- identifiers: minifyIdentifiers ?? defaultValue,
167
- syntax: minifySyntax ?? defaultValue
168
- };
169
- }
170
- function getResolvedBytecode(bytecode, format) {
171
- return format === "cjs" ? bytecode : undefined;
172
- }
173
- function getResolvedSourcemap(sourcemap) {
174
- if (sourcemap === true) {
175
- return "inline";
176
- }
177
- return typeof sourcemap === "string" ? sourcemap : undefined;
178
- }
179
- function getResolvedDefine(define, env) {
180
- return {
181
- ...typeof env === "object" && Object.keys(env).reduce((acc, key) => {
182
- const value = JSON.stringify(env[key]);
183
- acc[`process.env.${key}`] = value;
184
- acc[`import.meta.env.${key}`] = value;
185
- return acc;
186
- }, {}),
187
- ...define
188
- };
189
- }
190
- function getResolvedSplitting(splitting, format) {
191
- return splitting === undefined ? format === "esm" : splitting;
192
- }
193
- function getResolvedDtsSplitting(splitting) {
194
- return splitting ?? true;
195
- }
196
- var DEFAULT_ENTRY_NAMING = "[dir]/[name].[ext]";
197
- function getResolvedNaming(fmt, packageType) {
198
- const replaceExt = (pattern) => pattern.replace(".[ext]", getDefaultOutputExtension(fmt, packageType));
199
- return replaceExt(DEFAULT_ENTRY_NAMING);
200
- }
201
- function getResolvedEnv(env) {
202
- return typeof env === "string" ? env : undefined;
203
- }
204
-
205
- // src/helpers/external.ts
206
- function getPackageDepsPatterns(packageJson) {
207
- return getPackageDeps(packageJson).map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`));
208
- }
209
- function matchesPattern(path, pattern) {
210
- return typeof pattern === "string" ? pattern === path : pattern.test(path);
211
- }
212
- function isExternal(path, options, packageJson) {
213
- const packageDepsPatterns = getPackageDepsPatterns(packageJson);
214
- const matchesExternalPattern = packageDepsPatterns.some((pattern) => pattern.test(path)) || options.external?.some((pattern) => matchesPattern(path, pattern));
215
- const isExcludedFromExternal = options.noExternal?.some((pattern) => matchesPattern(path, pattern));
216
- return matchesExternalPattern && !isExcludedFromExternal;
217
- }
218
-
219
- // src/plugins/internal/external-option.ts
220
- function externalOptionPlugin(options, packageJson) {
221
- return {
222
- name: "bunup:external-option-plugin",
223
- setup(build) {
224
- build.onResolve({ filter: /.*/ }, (args) => {
225
- const importPath = args.path;
226
- if (isExternal(importPath, options, packageJson)) {
227
- return {
228
- path: importPath,
229
- external: true
230
- };
231
- }
232
- return null;
233
- });
234
- }
235
- };
236
- }
237
-
238
- // src/build.ts
239
- async function build(partialOptions, rootDir = process.cwd()) {
240
- const buildOutput = {
241
- files: []
242
- };
243
- const options = createBuildOptions(partialOptions);
244
- if (!options.entry || options.entry.length === 0 || !options.outDir) {
245
- throw new BunupBuildError("Nothing to build. Please make sure you have provided a proper bunup configuration or cli arguments.");
246
- }
247
- if (options.clean) {
248
- cleanOutDir(rootDir, options.outDir);
249
- }
250
- setSilent(options.silent);
251
- const packageJson = await loadPackageJson(rootDir);
252
- if (packageJson.data && packageJson.path) {
253
- logger.info(`Using ${getShortFilePath(packageJson.path, 2)}`, {
254
- muted: true,
255
- identifier: options.name,
256
- once: `${packageJson.path}:${options.name}`
257
- });
258
- }
259
- const bunupPlugins = filterBunupPlugins(options.plugins);
260
- await runPluginBuildStartHooks(bunupPlugins, options);
261
- const packageType = packageJson.data?.type;
262
- const plugins = [
263
- externalOptionPlugin(options, packageJson.data),
264
- ...filterBunupBunPlugins(options.plugins).map((p) => p.plugin)
265
- ];
266
- const entrypoints = await getFilesFromGlobs(ensureArray(options.entry), rootDir);
267
- if (!entrypoints.length) {
268
- throw new BunupBuildError("The entrypoints you provided do not exist. Please make sure the entrypoints point to valid files.");
269
- }
270
- const buildPromises = options.format.flatMap(async (fmt) => {
271
- const result = await Bun.build({
272
- entrypoints: entrypoints.map((file) => `${rootDir}/${file}`),
273
- format: fmt,
274
- naming: getResolvedNaming(fmt, packageType),
275
- splitting: getResolvedSplitting(options.splitting, fmt),
276
- bytecode: getResolvedBytecode(options.bytecode, fmt),
277
- define: getResolvedDefine(options.define, options.env),
278
- minify: getResolvedMinify(options),
279
- outdir: `${rootDir}/${options.outDir}`,
280
- target: options.target,
281
- sourcemap: getResolvedSourcemap(options.sourcemap),
282
- loader: options.loader,
283
- drop: options.drop,
284
- banner: options.banner,
285
- footer: options.footer,
286
- publicPath: options.publicPath,
287
- env: getResolvedEnv(options.env),
288
- ignoreDCEAnnotations: options.ignoreDCEAnnotations,
289
- emitDCEAnnotations: options.emitDCEAnnotations,
290
- throw: false,
291
- plugins
292
- });
293
- for (const log of result.logs) {
294
- if (log.level === "error") {
295
- throw new BunupBuildError(log.message);
296
- }
297
- if (log.level === "warning")
298
- logger.warn(log.message);
299
- else if (log.level === "info")
300
- logger.info(log.message);
301
- }
302
- let entrypointIndex = 0;
303
- for (const file of result.outputs) {
304
- const relativePathToRootDir = getRelativePathToRootDir(file.path, rootDir);
305
- const relativePathToOutputDir = getRelativePathToOutputDir(relativePathToRootDir, options.outDir);
306
- if (file.kind === "entry-point") {
307
- logger.success(`${pc2.dim(`${options.outDir}/`)}${relativePathToOutputDir}`, {
308
- identifier: options.name
309
- });
310
- }
311
- buildOutput.files.push({
312
- fullPath: file.path,
313
- relativePathToRootDir,
314
- relativePathToOutputDir,
315
- dts: false,
316
- format: fmt,
317
- kind: file.kind,
318
- entrypoint: entrypoints[entrypointIndex]
319
- });
320
- entrypointIndex++;
321
- }
322
- });
323
- await Promise.all(buildPromises);
324
- if (options.dts) {
325
- const { entry, splitting, ...dtsOptions } = typeof options.dts === "object" ? options.dts : {};
326
- const dtsResult = await generateDts(ensureArray(entry ?? entrypoints), {
327
- cwd: rootDir,
328
- preferredTsConfigPath: options.preferredTsconfigPath,
329
- splitting: getResolvedDtsSplitting(splitting),
330
- ...dtsOptions
331
- });
332
- if (dtsResult.errors.length) {
333
- logIsolatedDeclarationErrors(dtsResult.errors, {
334
- shouldExit: true
335
- });
336
- }
337
- for (const fmt of options.format) {
338
- for (const file of dtsResult.files) {
339
- const dtsExtension = getDefaultDtsExtention(fmt, packageType);
340
- const relativePathToOutputDir = cleanPath(`${file.pathInfo.outputPathWithoutExtension}${dtsExtension}`);
341
- const relativePathToRootDir = cleanPath(`${options.outDir}/${relativePathToOutputDir}`);
342
- if (file.kind === "entry-point") {
343
- logger.success(`${pc2.dim(`${options.outDir}/`)}${relativePathToOutputDir}`, {
344
- identifier: options.name
345
- });
346
- }
347
- const fullPath = path.join(rootDir, relativePathToRootDir);
348
- await Bun.write(fullPath, file.dts);
349
- buildOutput.files.push({
350
- fullPath,
351
- relativePathToRootDir,
352
- relativePathToOutputDir,
353
- dts: true,
354
- format: fmt,
355
- kind: file.kind,
356
- entrypoint: file.entrypoint
357
- });
358
- }
359
- }
360
- }
361
- await runPluginBuildDoneHooks(bunupPlugins, options, buildOutput, {
362
- packageJson,
363
- rootDir
364
- });
365
- if (options.onSuccess) {
366
- await options.onSuccess(options);
367
- }
368
- }
369
- function getRelativePathToRootDir(filePath, rootDir) {
370
- return cleanPath(filePath).replace(`${cleanPath(rootDir)}/`, "");
371
- }
372
- function getRelativePathToOutputDir(relativePathToRootDir, outDir) {
373
- return cleanPath(relativePathToRootDir).replace(`${cleanPath(outDir)}/`, "");
374
- }
375
-
376
- export { createBuildOptions, build };