bunup 0.14.7 → 0.14.9
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.
- package/dist/cli/index.js +24 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/plugins.d.ts +2 -108
- package/dist/plugins.js +6 -403
- package/dist/shared/{bunup-q245t34y.js → bunup-g3r7h8pa.js} +100 -68
- package/dist/shared/bunup-rh8gn8xh.js +869 -0
- package/dist/shared/{bunup-s6gfzz2v.d.ts → bunup-zenttvjh.d.ts} +148 -13
- package/package.json +1 -1
- package/dist/shared/bunup-r5agr54b.js +0 -466
package/dist/cli/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
printBuildReport,
|
|
6
6
|
processLoadedConfigs,
|
|
7
7
|
resolveBuildOptions
|
|
8
|
-
} from "../shared/bunup-
|
|
8
|
+
} from "../shared/bunup-g3r7h8pa.js";
|
|
9
9
|
import {
|
|
10
10
|
BunupCLIError,
|
|
11
11
|
BunupWatchError,
|
|
@@ -18,13 +18,13 @@ import {
|
|
|
18
18
|
logTime,
|
|
19
19
|
logger,
|
|
20
20
|
parseErrorMessage
|
|
21
|
-
} from "../shared/bunup-
|
|
21
|
+
} from "../shared/bunup-rh8gn8xh.js";
|
|
22
22
|
|
|
23
23
|
// packages/bunup/src/cli/index.ts
|
|
24
24
|
import { loadConfig } from "coffi";
|
|
25
25
|
import pc3 from "picocolors";
|
|
26
26
|
// packages/bunup/package.json
|
|
27
|
-
var version = "0.14.
|
|
27
|
+
var version = "0.14.9";
|
|
28
28
|
|
|
29
29
|
// packages/bunup/src/watch.ts
|
|
30
30
|
import path from "path";
|
|
@@ -97,14 +97,15 @@ import { cli, z } from "zlye";
|
|
|
97
97
|
var program = cli().name("bunup").version(version).description("A blazing-fast build tool for your TypeScript/React libraries \u2014 built on Bun").with({
|
|
98
98
|
ignoreOptionDefaultValue: true
|
|
99
99
|
}).example([
|
|
100
|
-
pc2.gray(`${pc2.blue("bunup
|
|
100
|
+
pc2.gray(`${pc2.blue("bunup")} # Basic build`),
|
|
101
|
+
pc2.gray(`${pc2.blue("bunup src/index.ts")} # Single entry file`),
|
|
101
102
|
pc2.gray(`${pc2.blue("bunup src/**/*.ts")} # Glob pattern for multiple files`),
|
|
102
|
-
pc2.gray(`${pc2.blue("bunup
|
|
103
|
-
pc2.gray(`${pc2.blue("bunup
|
|
104
|
-
pc2.gray(`${pc2.blue("bunup
|
|
105
|
-
pc2.gray(`${pc2.blue("bunup src/
|
|
106
|
-
pc2.gray(`${pc2.blue("bunup
|
|
107
|
-
pc2.gray(`${pc2.blue("bunup
|
|
103
|
+
pc2.gray(`${pc2.blue("bunup --watch")} # Watch mode`),
|
|
104
|
+
pc2.gray(`${pc2.blue("bunup --format cjs,esm")} # Multiple formats`),
|
|
105
|
+
pc2.gray(`${pc2.blue("bunup --target bun")} # Bun target`),
|
|
106
|
+
pc2.gray(`${pc2.blue("bunup src/cli.ts")} # Multiple entries`),
|
|
107
|
+
pc2.gray(`${pc2.blue("bunup --dts.splitting")} # Declaration splitting`),
|
|
108
|
+
pc2.gray(`${pc2.blue("bunup --no-clean")} # Disable cleaning output directory before build`)
|
|
108
109
|
]).option("entry", z.union(z.string().describe("Entry file or glob pattern"), z.array(z.string()).describe("Multiple entry files or globs")).alias("e").optional()).option("config", z.string().describe("Path to configuration file").alias("c").example("./configs/custom.bunup.config.js").optional()).option("filter", z.array(z.string()).describe("Filter workspace packages by name").optional()).option("name", z.string().describe("Name of the build configuration (for logging and identification)").example("my-library").optional()).option("out-dir", z.string().describe("Output directory for bundled files").alias("o").default("dist")).option("format", z.union(z.string().choices(["esm", "cjs", "iife"]).describe("Single output format"), z.array(z.string().choices(["esm", "cjs", "iife"])).describe("Multiple output formats")).alias("f").default("esm")).option("minify", z.boolean().describe("Enable all minification options (whitespace, identifiers, syntax)").optional()).option("minify-whitespace", z.boolean().describe("Minify whitespace in the output to reduce file size").optional()).option("minify-identifiers", z.boolean().describe("Minify identifiers by renaming variables to shorter names").optional()).option("minify-syntax", z.boolean().describe("Minify syntax by optimizing code structure").optional()).option("watch", z.boolean().describe("Watch for file changes and rebuild automatically").optional()).option("clean", z.boolean().describe("Clean the output directory before building").default(true)).option("silent", z.boolean().describe("Disable logging during the build process").alias("q").optional()).option("splitting", z.boolean().describe("Enable code splitting").default(true, "enabled by default for ESM format")).option("conditions", z.array(z.string()).describe("Package.json export conditions for import resolution").optional()).option("target", z.string().choices(["bun", "node", "browser"]).describe("Target environment for the bundle").alias("t").default("node")).option("external", z.array(z.string()).describe("External packages that should not be bundled").optional()).option("no-external", z.array(z.string()).describe("Packages that should be bundled even if listed in external").optional()).option("shims", z.boolean().describe("Enable shims for Node.js globals and ESM/CJS interoperability").optional()).option("report", z.object({
|
|
109
110
|
gzip: z.boolean().describe("Enable gzip compression size calculation").default(true),
|
|
110
111
|
brotli: z.boolean().describe("Enable brotli compression size calculation").optional(),
|
|
@@ -127,8 +128,19 @@ var program = cli().name("bunup").version(version).description("A blazing-fast b
|
|
|
127
128
|
"text",
|
|
128
129
|
"css",
|
|
129
130
|
"html"
|
|
130
|
-
])).describe("File extension to loader mapping").example("--loader.'.css'=text --loader.'.txt'=file").optional()).option("public-path", z.string().describe("Public path prefix for assets and chunk files").example("https://cdn.example.com/").optional()).option("ignore-dce-annotations", z.boolean().describe("Ignore dead code elimination annotations (@__PURE__, sideEffects)")).option("emit-dce-annotations", z.boolean().describe("Force emit @__PURE__ annotations even with minification")).option("on-success", z.string().describe("Command to run after successful build").optional()).option("
|
|
131
|
-
|
|
131
|
+
])).describe("File extension to loader mapping").example("--loader.'.css'=text --loader.'.txt'=file").optional()).option("public-path", z.string().describe("Public path prefix for assets and chunk files").example("https://cdn.example.com/").optional()).option("ignore-dce-annotations", z.boolean().describe("Ignore dead code elimination annotations (@__PURE__, sideEffects)")).option("emit-dce-annotations", z.boolean().describe("Force emit @__PURE__ annotations even with minification")).option("on-success", z.string().describe("Command to run after successful build").optional()).option("exports", z.union(z.boolean(), z.object({
|
|
132
|
+
exclude: z.array(z.string()).describe("Entry points to exclude from exports field").optional(),
|
|
133
|
+
"exclude-css": z.boolean().describe("Whether to exclude CSS files from exports field").optional(),
|
|
134
|
+
"include-package-json": z.boolean().describe('Whether to include "./package.json" in exports field').default(true),
|
|
135
|
+
all: z.boolean().describe("Whether to add wildcard export for deep imports").optional()
|
|
136
|
+
})).describe("Configure automatic package.json exports generation").optional()).option("unused", z.union(z.boolean(), z.object({
|
|
137
|
+
level: z.string().choices(["warn", "error"]).describe("The level of reporting for unused dependencies").default("warn"),
|
|
138
|
+
ignore: z.array(z.string()).describe("Dependencies to ignore when checking for unused dependencies").optional()
|
|
139
|
+
})).describe("Detect and report unused dependencies").optional()).option("css", z.object({
|
|
140
|
+
"typed-modules": z.boolean().describe("Generate TypeScript definitions for CSS modules").default(true),
|
|
141
|
+
inject: z.union(z.boolean(), z.object({
|
|
142
|
+
minify: z.boolean().describe("Whether to minify the styles being injected").optional()
|
|
143
|
+
})).describe("Inject CSS styles into document head at runtime").optional()
|
|
132
144
|
}).optional()).rest("entries", z.string().describe("Entry point files to bundle"));
|
|
133
145
|
var parseCliOptions = (argv) => {
|
|
134
146
|
const result = program.parse(argv);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Arrayable, BuildContext, BuildMeta, BuildOptions, BuildOutput, BuildOutputFile, BunupPlugin, DefineConfigItem, DefineWorkspaceItem, WithOptional } from "./shared/bunup-
|
|
1
|
+
import { Arrayable, BuildContext, BuildMeta, BuildOptions, BuildOutput, BuildOutputFile, BunupPlugin, DefineConfigItem, DefineWorkspaceItem, WithOptional } from "./shared/bunup-zenttvjh";
|
|
2
2
|
declare function build(userOptions: Partial<BuildOptions>, rootDir?: string): Promise<BuildOutput>;
|
|
3
3
|
declare function defineConfig(options: Arrayable<DefineConfigItem>): Arrayable<DefineConfigItem>;
|
|
4
4
|
declare function defineWorkspace(options: WithOptional<DefineWorkspaceItem, "config">[], sharedOptions?: Partial<DefineConfigItem>): DefineWorkspaceItem[];
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
3
|
build
|
|
4
|
-
} from "./shared/bunup-
|
|
5
|
-
import"./shared/bunup-
|
|
4
|
+
} from "./shared/bunup-g3r7h8pa.js";
|
|
5
|
+
import"./shared/bunup-rh8gn8xh.js";
|
|
6
6
|
// packages/bunup/src/define.ts
|
|
7
7
|
function defineConfig(options) {
|
|
8
8
|
return options;
|
package/dist/plugins.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BunupPlugin, BunupPluginHooks, exports, injectStyles, unused } from "./shared/bunup-zenttvjh";
|
|
2
2
|
type CopyOptions = {
|
|
3
3
|
/** Whether to follow symbolic links when copying files. */
|
|
4
4
|
followSymlinks?: boolean
|
|
@@ -28,115 +28,9 @@ declare class CopyBuilder {
|
|
|
28
28
|
get name(): string;
|
|
29
29
|
get hooks(): BunupPluginHooks;
|
|
30
30
|
}
|
|
31
|
-
type CustomExports = Record<string, string | Record<string, string | Record<string, string>>>;
|
|
32
|
-
type Exclude = ((ctx: BuildContext) => string[] | undefined) | string[];
|
|
33
|
-
interface ExportsPluginOptions {
|
|
34
|
-
/**
|
|
35
|
-
* Additional export fields to preserve alongside automatically generated exports
|
|
36
|
-
*
|
|
37
|
-
* @see https://bunup.dev/docs/builtin-plugins/exports#customexports
|
|
38
|
-
*/
|
|
39
|
-
customExports?: (ctx: BuildContext) => CustomExports | undefined;
|
|
40
|
-
/**
|
|
41
|
-
* Entry points to exclude from the exports field
|
|
42
|
-
*
|
|
43
|
-
* @see https://bunup.dev/docs/builtin-plugins/exports#exclude
|
|
44
|
-
*/
|
|
45
|
-
exclude?: Exclude;
|
|
46
|
-
/**
|
|
47
|
-
* Whether to exclude CSS files from being added to the exports field
|
|
48
|
-
*
|
|
49
|
-
* @default false
|
|
50
|
-
*/
|
|
51
|
-
excludeCss?: boolean;
|
|
52
|
-
/**
|
|
53
|
-
* Whether to include "./package.json": "./package.json" in the exports field
|
|
54
|
-
*
|
|
55
|
-
* @default true
|
|
56
|
-
* @see https://bunup.dev/docs/builtin-plugins/exports#includepackagejson
|
|
57
|
-
*/
|
|
58
|
-
includePackageJson?: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Whether to add a wildcard export that allows deep imports
|
|
61
|
-
*
|
|
62
|
-
* When true, adds "./*": "./*" to exports, making all files accessible
|
|
63
|
-
* When false (default), only explicit exports are accessible
|
|
64
|
-
*
|
|
65
|
-
* @default false
|
|
66
|
-
* @see https://bunup.dev/docs/builtin-plugins/exports#all
|
|
67
|
-
*/
|
|
68
|
-
all?: boolean;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* A plugin that generates the exports field in the package.json file automatically.
|
|
72
|
-
*
|
|
73
|
-
* @see https://bunup.dev/docs/builtin-plugins/exports
|
|
74
|
-
*/
|
|
75
|
-
declare function exports(options?: ExportsPluginOptions): BunupPlugin;
|
|
76
31
|
import { BunPlugin } from "bun";
|
|
77
|
-
type InjectStylesPluginOptions = {
|
|
78
|
-
/**
|
|
79
|
-
* Custom function to inject CSS into the document head.
|
|
80
|
-
*
|
|
81
|
-
* By default, bunup uses its own `injectStyle` function that creates a `<style>`
|
|
82
|
-
* tag and appends it to the document head. You can provide your own injection
|
|
83
|
-
* logic to customize how styles are applied to the document.
|
|
84
|
-
*
|
|
85
|
-
* @param css - The processed CSS string (already JSON stringified)
|
|
86
|
-
* @param filePath - The original file path of the CSS file being processed
|
|
87
|
-
* @returns JavaScript code that will inject the styles when executed
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* ```ts
|
|
91
|
-
* injectStyles({
|
|
92
|
-
* inject: (css, filePath) => {
|
|
93
|
-
* return `
|
|
94
|
-
* const style = document.createElement('style');
|
|
95
|
-
* style.setAttribute('data-source', '${filePath}');
|
|
96
|
-
* style.textContent = ${css};
|
|
97
|
-
* document.head.appendChild(style);
|
|
98
|
-
* `;
|
|
99
|
-
* }
|
|
100
|
-
* })
|
|
101
|
-
* ```
|
|
102
|
-
*
|
|
103
|
-
* The default injection handles cases like when `document` is undefined (e.g., server-side rendering) and compatibility with older browsers. Consider these when implementing custom injection logic.
|
|
104
|
-
*/
|
|
105
|
-
inject?: (css: string, filePath: string) => MaybePromise<string>
|
|
106
|
-
/**
|
|
107
|
-
* Whether to minify the styles being injected.
|
|
108
|
-
*
|
|
109
|
-
* @default true
|
|
110
|
-
*/
|
|
111
|
-
minify?: boolean
|
|
112
|
-
};
|
|
113
|
-
/**
|
|
114
|
-
* A plugin that injects styles into the document head at runtime instead of bundling them to the build output.
|
|
115
|
-
*
|
|
116
|
-
* @see https://bunup.dev/docs/builtin-plugins/inject-styles
|
|
117
|
-
*/
|
|
118
|
-
declare function injectStyles(options?: InjectStylesPluginOptions): BunPlugin;
|
|
119
|
-
import { BunPlugin as BunPlugin2 } from "bun";
|
|
120
32
|
/**
|
|
121
33
|
* A plugin that provides shims for Node.js globals and ESM/CJS interoperability.
|
|
122
34
|
*/
|
|
123
|
-
declare function shims():
|
|
124
|
-
interface UnusedOptions {
|
|
125
|
-
/**
|
|
126
|
-
* The level of reporting for unused dependencies
|
|
127
|
-
* @default 'warn'
|
|
128
|
-
*/
|
|
129
|
-
level?: "warn" | "error";
|
|
130
|
-
/**
|
|
131
|
-
* Dependencies to ignore when checking for unused dependencies
|
|
132
|
-
* @default []
|
|
133
|
-
*/
|
|
134
|
-
ignore?: string[];
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* A plugin that detects and reports unused dependencies.
|
|
138
|
-
*
|
|
139
|
-
* @see https://bunup.dev/docs/builtin-plugins/unused
|
|
140
|
-
*/
|
|
141
|
-
declare function unused(options?: UnusedOptions): BunupPlugin;
|
|
35
|
+
declare function shims(): BunPlugin;
|
|
142
36
|
export { unused, shims, injectStyles, exports, copy };
|
package/dist/plugins.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
CSS_RE,
|
|
4
|
-
JS_DTS_RE,
|
|
5
|
-
cleanPath,
|
|
6
|
-
detectFileFormatting,
|
|
7
3
|
ensureArray,
|
|
8
|
-
|
|
4
|
+
exports,
|
|
5
|
+
injectStyles,
|
|
9
6
|
isGlobPattern,
|
|
10
7
|
logger,
|
|
11
|
-
shims
|
|
12
|
-
|
|
8
|
+
shims,
|
|
9
|
+
unused
|
|
10
|
+
} from "./shared/bunup-rh8gn8xh.js";
|
|
13
11
|
|
|
14
|
-
// packages/bunup/src/plugins/
|
|
12
|
+
// packages/bunup/src/plugins/copy.ts
|
|
15
13
|
import { basename, extname, join } from "path";
|
|
16
14
|
function copy(pattern) {
|
|
17
15
|
return new CopyBuilder(pattern);
|
|
@@ -91,401 +89,6 @@ async function copyFile(sourcePath, finalDestinationPath) {
|
|
|
91
89
|
const sourceFile = Bun.file(sourcePath);
|
|
92
90
|
await Bun.write(finalDestinationPath, sourceFile);
|
|
93
91
|
}
|
|
94
|
-
// packages/bunup/src/plugins/built-in/exports.ts
|
|
95
|
-
import path from "path";
|
|
96
|
-
function exports(options = {}) {
|
|
97
|
-
return {
|
|
98
|
-
name: "exports",
|
|
99
|
-
hooks: {
|
|
100
|
-
onBuildDone: async (ctx) => {
|
|
101
|
-
await processPackageJsonExports(ctx, options);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
async function processPackageJsonExports(ctx, options) {
|
|
107
|
-
const { output, options: buildOptions, meta } = ctx;
|
|
108
|
-
if (!meta.packageJson.path || !meta.packageJson.data) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
const { exportsField, entryPoints } = generateExportsFields(output.files, options.exclude, options.excludeCss, ctx);
|
|
113
|
-
const updatedFiles = createUpdatedFilesArray(meta.packageJson.data, buildOptions.outDir);
|
|
114
|
-
const mergedExports = mergeCustomExportsWithGenerated(exportsField, options.customExports, ctx);
|
|
115
|
-
const finalExports = addPackageJsonOrWildcardExport(mergedExports, options.includePackageJson, options.all);
|
|
116
|
-
const newPackageJson = createUpdatedPackageJson(meta.packageJson.data, entryPoints, finalExports, updatedFiles);
|
|
117
|
-
if (Bun.deepEquals(newPackageJson, meta.packageJson.data)) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const formatting = await detectFileFormatting(meta.packageJson.path);
|
|
121
|
-
let jsonContent = JSON.stringify(newPackageJson, null, formatting.indentation);
|
|
122
|
-
if (formatting.hasTrailingNewline) {
|
|
123
|
-
jsonContent += `
|
|
124
|
-
`;
|
|
125
|
-
}
|
|
126
|
-
await Bun.write(meta.packageJson.path, jsonContent);
|
|
127
|
-
} catch {
|
|
128
|
-
logger.error("Failed to update package.json");
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
function generateExportsFields(files, exclude, excludeCss, ctx) {
|
|
132
|
-
const filteredFiles = filterFiles(files, exclude, ctx);
|
|
133
|
-
const { filesByExportKey, allDtsFiles, cssFiles } = groupFilesByExportKey(filteredFiles);
|
|
134
|
-
const exportsField = createExportEntries(filesByExportKey);
|
|
135
|
-
if (!excludeCss) {
|
|
136
|
-
addCssToExports(exportsField, cssFiles);
|
|
137
|
-
}
|
|
138
|
-
const entryPoints = extractEntryPoints(exportsField, allDtsFiles);
|
|
139
|
-
return { exportsField, entryPoints };
|
|
140
|
-
}
|
|
141
|
-
function groupFilesByExportKey(files) {
|
|
142
|
-
const filesByExportKey = new Map;
|
|
143
|
-
const allDtsFiles = new Map;
|
|
144
|
-
const cssFiles = [];
|
|
145
|
-
for (const file of files) {
|
|
146
|
-
const exportKey = getExportKey(cleanPath(file.pathRelativeToOutdir));
|
|
147
|
-
if (CSS_RE.test(file.fullPath)) {
|
|
148
|
-
cssFiles.push(file);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
const format = file.format === "esm" ? "import" : "require";
|
|
152
|
-
if (!filesByExportKey.has(exportKey)) {
|
|
153
|
-
filesByExportKey.set(exportKey, new Map);
|
|
154
|
-
allDtsFiles.set(exportKey, []);
|
|
155
|
-
}
|
|
156
|
-
const formatMap = filesByExportKey.get(exportKey);
|
|
157
|
-
const dtsFiles = allDtsFiles.get(exportKey);
|
|
158
|
-
if (formatMap && dtsFiles) {
|
|
159
|
-
if (!formatMap.has(format)) {
|
|
160
|
-
formatMap.set(format, { dts: undefined, source: undefined });
|
|
161
|
-
}
|
|
162
|
-
const fileEntry = formatMap.get(format);
|
|
163
|
-
if (fileEntry) {
|
|
164
|
-
if (file.dts) {
|
|
165
|
-
fileEntry.dts = file;
|
|
166
|
-
dtsFiles.push(file);
|
|
167
|
-
} else {
|
|
168
|
-
fileEntry.source = file;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return { filesByExportKey, allDtsFiles, cssFiles };
|
|
174
|
-
}
|
|
175
|
-
function createExportEntries(filesByExportKey) {
|
|
176
|
-
const exportsField = {};
|
|
177
|
-
for (const [exportKey, formatMap] of filesByExportKey.entries()) {
|
|
178
|
-
exportsField[exportKey] = {};
|
|
179
|
-
let hasFormatSpecificTypes = false;
|
|
180
|
-
let primaryTypesPath;
|
|
181
|
-
for (const [format, files] of formatMap.entries()) {
|
|
182
|
-
const formatKey = format;
|
|
183
|
-
if (files.dts && files.source) {
|
|
184
|
-
exportsField[exportKey][formatKey] = {
|
|
185
|
-
types: `./${cleanPath(files.dts.pathRelativeToRootDir)}`,
|
|
186
|
-
default: `./${cleanPath(files.source.pathRelativeToRootDir)}`
|
|
187
|
-
};
|
|
188
|
-
hasFormatSpecificTypes = true;
|
|
189
|
-
if (!primaryTypesPath) {
|
|
190
|
-
primaryTypesPath = `./${cleanPath(files.dts.pathRelativeToRootDir)}`;
|
|
191
|
-
}
|
|
192
|
-
} else if (files.source) {
|
|
193
|
-
exportsField[exportKey][formatKey] = `./${cleanPath(files.source.pathRelativeToRootDir)}`;
|
|
194
|
-
if (files.dts) {
|
|
195
|
-
primaryTypesPath = `./${cleanPath(files.dts.pathRelativeToRootDir)}`;
|
|
196
|
-
}
|
|
197
|
-
} else if (files.dts) {
|
|
198
|
-
primaryTypesPath = `./${cleanPath(files.dts.pathRelativeToRootDir)}`;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
if (!hasFormatSpecificTypes && primaryTypesPath) {
|
|
202
|
-
exportsField[exportKey].types = primaryTypesPath;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return exportsField;
|
|
206
|
-
}
|
|
207
|
-
function extractEntryPoints(exportsField, allDtsFiles) {
|
|
208
|
-
const entryPoints = {};
|
|
209
|
-
const dotExport = exportsField["."];
|
|
210
|
-
if (!dotExport || typeof dotExport === "string") {
|
|
211
|
-
return entryPoints;
|
|
212
|
-
}
|
|
213
|
-
for (const [field, value] of Object.entries(dotExport)) {
|
|
214
|
-
if (field === "types")
|
|
215
|
-
continue;
|
|
216
|
-
const entryPoint = exportFieldToEntryPoint(field);
|
|
217
|
-
if (typeof value === "string") {
|
|
218
|
-
entryPoints[entryPoint] = value;
|
|
219
|
-
} else if (value && typeof value === "object" && "default" in value) {
|
|
220
|
-
entryPoints[entryPoint] = value.default;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
const dotEntryDtsFiles = allDtsFiles.get(".");
|
|
224
|
-
if (dotEntryDtsFiles?.length) {
|
|
225
|
-
const standardDts = findStandardDtsFile(dotEntryDtsFiles);
|
|
226
|
-
if (standardDts) {
|
|
227
|
-
entryPoints.types = `./${cleanPath(standardDts.pathRelativeToRootDir)}`;
|
|
228
|
-
} else {
|
|
229
|
-
entryPoints.types = extractTypesFromExport(dotExport);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return entryPoints;
|
|
233
|
-
}
|
|
234
|
-
function findStandardDtsFile(dtsFiles) {
|
|
235
|
-
return dtsFiles.find((file) => file.pathRelativeToRootDir.endsWith(".d.ts") && !file.pathRelativeToRootDir.endsWith(".d.mts") && !file.pathRelativeToRootDir.endsWith(".d.cts"));
|
|
236
|
-
}
|
|
237
|
-
function extractTypesFromExport(dotExport) {
|
|
238
|
-
const typesValue = dotExport.types;
|
|
239
|
-
if (typeof typesValue === "string") {
|
|
240
|
-
return typesValue;
|
|
241
|
-
}
|
|
242
|
-
if (typesValue && typeof typesValue === "object" && "types" in typesValue) {
|
|
243
|
-
return typesValue.types;
|
|
244
|
-
}
|
|
245
|
-
const importValue = dotExport.import;
|
|
246
|
-
if (importValue && typeof importValue === "object" && "types" in importValue) {
|
|
247
|
-
return importValue.types;
|
|
248
|
-
}
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
function createUpdatedFilesArray(packageJsonData, outDir) {
|
|
252
|
-
const existingFiles = Array.isArray(packageJsonData.files) ? packageJsonData.files : [];
|
|
253
|
-
return [...new Set([...existingFiles, outDir])];
|
|
254
|
-
}
|
|
255
|
-
function mergeCustomExportsWithGenerated(baseExports, customExportsProvider, ctx) {
|
|
256
|
-
const mergedExports = { ...baseExports };
|
|
257
|
-
if (!customExportsProvider) {
|
|
258
|
-
return mergedExports;
|
|
259
|
-
}
|
|
260
|
-
const customExports = customExportsProvider(ctx);
|
|
261
|
-
if (!customExports) {
|
|
262
|
-
return mergedExports;
|
|
263
|
-
}
|
|
264
|
-
for (const [key, value] of Object.entries(customExports)) {
|
|
265
|
-
if (typeof value === "string") {
|
|
266
|
-
mergedExports[key] = value;
|
|
267
|
-
} else {
|
|
268
|
-
const existingExport = mergedExports[key];
|
|
269
|
-
if (typeof existingExport === "object" && existingExport !== null) {
|
|
270
|
-
mergedExports[key] = { ...existingExport, ...value };
|
|
271
|
-
} else {
|
|
272
|
-
mergedExports[key] = value;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return mergedExports;
|
|
277
|
-
}
|
|
278
|
-
function createUpdatedPackageJson(originalData, entryPoints, exports2, files) {
|
|
279
|
-
const { main, module, types, ...restPackageJson } = originalData;
|
|
280
|
-
const newPackageJson = {
|
|
281
|
-
...Object.fromEntries(Object.entries({
|
|
282
|
-
name: originalData.name,
|
|
283
|
-
description: originalData.description,
|
|
284
|
-
version: originalData.version,
|
|
285
|
-
type: originalData.type,
|
|
286
|
-
private: originalData.private
|
|
287
|
-
}).filter(([_, value]) => value !== undefined)),
|
|
288
|
-
files,
|
|
289
|
-
...entryPoints,
|
|
290
|
-
exports: exports2
|
|
291
|
-
};
|
|
292
|
-
for (const key in restPackageJson) {
|
|
293
|
-
if (Object.hasOwn(restPackageJson, key) && !Object.hasOwn(newPackageJson, key)) {
|
|
294
|
-
newPackageJson[key] = restPackageJson[key];
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
return newPackageJson;
|
|
298
|
-
}
|
|
299
|
-
function filterFiles(files, exclude, ctx) {
|
|
300
|
-
return files.filter((file) => (JS_DTS_RE.test(file.fullPath) || CSS_RE.test(file.fullPath)) && (file.kind === "entry-point" || file.kind === "asset") && (file.format === "esm" || file.format === "cjs" || CSS_RE.test(file.fullPath)) && (!file.entrypoint || !isExcluded(file.entrypoint, exclude, ctx)));
|
|
301
|
-
}
|
|
302
|
-
var DEFAULT_CLI_EXCLUSIONS = [
|
|
303
|
-
"cli.ts",
|
|
304
|
-
"cli/index.ts",
|
|
305
|
-
"src/cli.ts",
|
|
306
|
-
"src/cli/index.ts"
|
|
307
|
-
];
|
|
308
|
-
function isExcluded(entrypoint, exclude, ctx) {
|
|
309
|
-
const userPatterns = typeof exclude === "function" ? exclude(ctx) : exclude;
|
|
310
|
-
const allPatterns = [...DEFAULT_CLI_EXCLUSIONS, ...userPatterns ?? []];
|
|
311
|
-
return allPatterns.some((pattern) => new Bun.Glob(pattern).match(entrypoint));
|
|
312
|
-
}
|
|
313
|
-
function getExportKey(pathRelativeToOutdir) {
|
|
314
|
-
const pathSegments = cleanPath(removeExtension(pathRelativeToOutdir)).split("/");
|
|
315
|
-
if (pathSegments.length === 1 && pathSegments[0].startsWith("index")) {
|
|
316
|
-
return ".";
|
|
317
|
-
}
|
|
318
|
-
return `./${pathSegments.filter((segment) => !segment.startsWith("index")).join("/")}`;
|
|
319
|
-
}
|
|
320
|
-
function removeExtension(filePath) {
|
|
321
|
-
const basename2 = path.basename(filePath);
|
|
322
|
-
const firstDotIndex = basename2.indexOf(".");
|
|
323
|
-
if (firstDotIndex === -1) {
|
|
324
|
-
return filePath;
|
|
325
|
-
}
|
|
326
|
-
const nameWithoutExtensions = basename2.slice(0, firstDotIndex);
|
|
327
|
-
const directory = path.dirname(filePath);
|
|
328
|
-
return directory === "." ? nameWithoutExtensions : path.join(directory, nameWithoutExtensions);
|
|
329
|
-
}
|
|
330
|
-
function addCssToExports(exportsField, cssFiles) {
|
|
331
|
-
if (cssFiles.length === 0)
|
|
332
|
-
return;
|
|
333
|
-
for (const cssFile of cssFiles) {
|
|
334
|
-
const exportKey = getCssExportKey(cleanPath(cssFile.pathRelativeToOutdir));
|
|
335
|
-
exportsField[exportKey] = `./${cleanPath(cssFile.pathRelativeToRootDir)}`;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
function getCssExportKey(pathRelativeToOutdir) {
|
|
339
|
-
const pathSegments = cleanPath(removeExtension(pathRelativeToOutdir)).split("/");
|
|
340
|
-
const fileName = pathSegments[pathSegments.length - 1];
|
|
341
|
-
if (fileName === "index") {
|
|
342
|
-
if (pathSegments.length === 1) {
|
|
343
|
-
return "./styles.css";
|
|
344
|
-
} else {
|
|
345
|
-
return `./${pathSegments.slice(0, -1).join("/")}.css`;
|
|
346
|
-
}
|
|
347
|
-
} else {
|
|
348
|
-
return `./${pathSegments.join("/")}.css`;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
function addPackageJsonOrWildcardExport(exports2, includePackageJson, all) {
|
|
352
|
-
const finalExports = { ...exports2 };
|
|
353
|
-
if (all) {
|
|
354
|
-
finalExports["./*"] = "./*";
|
|
355
|
-
} else if (includePackageJson !== false) {
|
|
356
|
-
if (!finalExports["./package.json"]) {
|
|
357
|
-
finalExports["./package.json"] = "./package.json";
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return finalExports;
|
|
361
|
-
}
|
|
362
|
-
function exportFieldToEntryPoint(exportField) {
|
|
363
|
-
switch (exportField) {
|
|
364
|
-
case "types":
|
|
365
|
-
return "types";
|
|
366
|
-
case "require":
|
|
367
|
-
return "main";
|
|
368
|
-
default:
|
|
369
|
-
return "module";
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
// packages/bunup/src/plugins/built-in/inject-styles.ts
|
|
373
|
-
import path2 from "path";
|
|
374
|
-
import { getDefaultCssBrowserTargets } from "@bunup/shared";
|
|
375
|
-
import { transform } from "lightningcss";
|
|
376
|
-
function injectStyles(options) {
|
|
377
|
-
const { inject, minify = true } = options ?? {};
|
|
378
|
-
return {
|
|
379
|
-
name: "bunup:inject-styles",
|
|
380
|
-
async setup(build) {
|
|
381
|
-
build.onResolve({ filter: /^__inject-style$/ }, () => {
|
|
382
|
-
return {
|
|
383
|
-
path: "__inject-style",
|
|
384
|
-
namespace: "__inject-style"
|
|
385
|
-
};
|
|
386
|
-
});
|
|
387
|
-
build.onLoad({ filter: /^__inject-style$/, namespace: "__inject-style" }, () => {
|
|
388
|
-
return {
|
|
389
|
-
contents: `
|
|
390
|
-
export default function injectStyle(css) {
|
|
391
|
-
if (!css || typeof document === 'undefined') return
|
|
392
|
-
|
|
393
|
-
const head = document.head || document.getElementsByTagName('head')[0]
|
|
394
|
-
const style = document.createElement('style')
|
|
395
|
-
head.appendChild(style)
|
|
396
|
-
|
|
397
|
-
if (style.styleSheet) {
|
|
398
|
-
style.styleSheet.cssText = css
|
|
399
|
-
} else {
|
|
400
|
-
style.appendChild(document.createTextNode(css))
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
`,
|
|
404
|
-
loader: "js"
|
|
405
|
-
};
|
|
406
|
-
});
|
|
407
|
-
build.onLoad({ filter: CSS_RE }, async (args) => {
|
|
408
|
-
const source = await Bun.file(args.path).text();
|
|
409
|
-
const { code, warnings } = transform({
|
|
410
|
-
filename: path2.basename(args.path),
|
|
411
|
-
code: Buffer.from(source),
|
|
412
|
-
minify,
|
|
413
|
-
targets: getDefaultCssBrowserTargets()
|
|
414
|
-
});
|
|
415
|
-
for (const warning of warnings) {
|
|
416
|
-
logger.warn(warning.message);
|
|
417
|
-
}
|
|
418
|
-
const stringifiedCode = JSON.stringify(code.toString());
|
|
419
|
-
const js = inject ? await inject(stringifiedCode, args.path) : `import injectStyle from '__inject-style';injectStyle(${stringifiedCode})`;
|
|
420
|
-
return {
|
|
421
|
-
contents: js,
|
|
422
|
-
loader: "js"
|
|
423
|
-
};
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
// packages/bunup/src/plugins/built-in/unused.ts
|
|
429
|
-
import pc from "picocolors";
|
|
430
|
-
function unused(options = {}) {
|
|
431
|
-
const { level = "warn", ignore = [] } = options;
|
|
432
|
-
return {
|
|
433
|
-
name: "unused",
|
|
434
|
-
hooks: {
|
|
435
|
-
onBuildDone: async (ctx) => {
|
|
436
|
-
const { options: buildOptions, output, meta } = ctx;
|
|
437
|
-
if (buildOptions.watch) {
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
const transpiler = new Bun.Transpiler({
|
|
441
|
-
loader: "js"
|
|
442
|
-
});
|
|
443
|
-
const jsFiles = output.files.filter((file) => file.fullPath.endsWith(".js"));
|
|
444
|
-
const packageDependencies = typeof meta.packageJson.data?.dependencies === "object" ? meta.packageJson.data.dependencies : {};
|
|
445
|
-
const externals = [
|
|
446
|
-
...buildOptions.external ?? [],
|
|
447
|
-
...buildOptions.noExternal ?? []
|
|
448
|
-
];
|
|
449
|
-
const allImportPaths = new Set;
|
|
450
|
-
for (const file of jsFiles) {
|
|
451
|
-
const code = await Bun.file(file.fullPath).text();
|
|
452
|
-
const codeWithoutShebang = code.replace(/^#!.*$/m, "");
|
|
453
|
-
const importPaths = transpiler.scanImports(codeWithoutShebang).map((imp) => imp.path);
|
|
454
|
-
for (const importPath of importPaths) {
|
|
455
|
-
if (externals.some((ex) => typeof ex === "string" ? importPath.startsWith(ex) : ex.test(importPath)))
|
|
456
|
-
continue;
|
|
457
|
-
if (importPath.startsWith("node:") || importPath.startsWith("bun:"))
|
|
458
|
-
continue;
|
|
459
|
-
allImportPaths.add(importPath);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
const allDependencies = Object.keys(packageDependencies);
|
|
463
|
-
const unusedDependencies = allDependencies.filter((dependency) => {
|
|
464
|
-
if (ignore.includes(dependency))
|
|
465
|
-
return false;
|
|
466
|
-
return !Array.from(allImportPaths).some((importPath) => importPath === dependency || importPath.startsWith(`${dependency}/`));
|
|
467
|
-
});
|
|
468
|
-
if (unusedDependencies.length > 0) {
|
|
469
|
-
const count = unusedDependencies.length;
|
|
470
|
-
const depText = count === 1 ? "dependency" : "dependencies";
|
|
471
|
-
const coloredDeps = formatListWithAnd(unusedDependencies.map((dep) => pc.yellow(dep)));
|
|
472
|
-
const removeCommand = pc.cyan(`bun remove ${unusedDependencies.join(" ")}`);
|
|
473
|
-
const message = [
|
|
474
|
-
`
|
|
475
|
-
Your project${buildOptions.name ? ` ${buildOptions.name}` : ""} has ${count} unused ${depText}: ${coloredDeps}.`,
|
|
476
|
-
`You can remove ${count === 1 ? "it" : "them"} with ${removeCommand}`
|
|
477
|
-
].join(" ");
|
|
478
|
-
if (level === "error") {
|
|
479
|
-
logger.log(pc.red(message));
|
|
480
|
-
process.exit(1);
|
|
481
|
-
} else {
|
|
482
|
-
logger.log(message);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
92
|
export {
|
|
490
93
|
unused,
|
|
491
94
|
shims,
|