@hypernym/bundler 0.14.4 → 0.20.0
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/LICENSE.txt +2 -1
- package/README.md +126 -96
- package/dist/bin/index.mjs +178 -479
- package/dist/build/index.mjs +311 -0
- package/dist/index.d.mts +603 -658
- package/dist/index.mjs +50 -17
- package/dist/plugins/index.d.mts +14 -0
- package/dist/plugins/index.mjs +37 -0
- package/package.json +25 -30
- package/dist/index.cjs +0 -26
- package/dist/index.d.cts +0 -709
package/dist/bin/index.mjs
CHANGED
|
@@ -1,506 +1,205 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { build
|
|
8
|
-
import {
|
|
9
|
-
import { isString, isUndefined, isObject } from '@hypernym/utils';
|
|
10
|
-
import { rollup } from 'rollup';
|
|
11
|
-
import { getLogFilter } from 'rollup/getLogFilter';
|
|
12
|
-
import replacePlugin from '@rollup/plugin-replace';
|
|
13
|
-
import jsonPlugin from '@rollup/plugin-json';
|
|
14
|
-
import resolvePlugin from '@rollup/plugin-node-resolve';
|
|
15
|
-
import aliasPlugin from '@rollup/plugin-alias';
|
|
16
|
-
import { dts } from 'rollup-plugin-dts';
|
|
17
|
-
import { createFilter } from '@rollup/pluginutils';
|
|
18
|
-
|
|
19
|
-
const externals = [
|
|
20
|
-
/^node:/,
|
|
21
|
-
/^@types/,
|
|
22
|
-
/^@rollup/,
|
|
23
|
-
/^@hypernym/,
|
|
24
|
-
/^rollup/
|
|
25
|
-
];
|
|
2
|
+
import { createArgs } from "@hypernym/args";
|
|
3
|
+
import process, { cwd } from "node:process";
|
|
4
|
+
import { cyan, dim } from "@hypernym/colors";
|
|
5
|
+
import { isAbsolute, resolve } from "node:path";
|
|
6
|
+
import { exists, read, write } from "@hypernym/utils/fs";
|
|
7
|
+
import { build } from "rolldown";
|
|
8
|
+
import { build as build$1 } from "../build/index.mjs";
|
|
26
9
|
|
|
10
|
+
//#region src/bin/meta.ts
|
|
27
11
|
const name = `Hyperbundler`;
|
|
28
|
-
const version = `0.
|
|
12
|
+
const version = `0.20.0`;
|
|
29
13
|
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/logger.ts
|
|
30
16
|
const cl = console.log;
|
|
31
|
-
const separator =
|
|
17
|
+
const separator = `|`;
|
|
32
18
|
const logger = {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
19
|
+
info: (...args) => {
|
|
20
|
+
cl(name, dim(separator), ...args);
|
|
21
|
+
},
|
|
22
|
+
error: (...args) => {
|
|
23
|
+
cl();
|
|
24
|
+
cl(name, dim(separator), ...args);
|
|
25
|
+
cl();
|
|
26
|
+
},
|
|
27
|
+
exit: (message) => {
|
|
28
|
+
cl();
|
|
29
|
+
cl(name, dim(separator), message);
|
|
30
|
+
cl();
|
|
31
|
+
return process.exit();
|
|
32
|
+
}
|
|
47
33
|
};
|
|
48
34
|
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/utils/error.ts
|
|
49
37
|
function error(err) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
logger.error("Something went wrong...");
|
|
39
|
+
console.error(err);
|
|
40
|
+
return process.exit();
|
|
53
41
|
}
|
|
54
42
|
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/utils/format-ms.ts
|
|
55
45
|
function formatMs(ms) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
46
|
+
const s = 1e3;
|
|
47
|
+
const m = s * 60;
|
|
48
|
+
const h = m * 60;
|
|
49
|
+
const msAbs = Math.abs(ms);
|
|
50
|
+
if (msAbs >= h) return `${(ms / h).toFixed(2)}h`;
|
|
51
|
+
if (msAbs >= m) return `${(ms / m).toFixed(2)}m`;
|
|
52
|
+
if (msAbs >= s) return `${(ms / s).toFixed(2)}s`;
|
|
53
|
+
return `${ms}ms`;
|
|
64
54
|
}
|
|
65
55
|
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/utils/format-bytes.ts
|
|
66
58
|
function formatBytes(bytes) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const cts = dts ? "d.cts" : "cjs";
|
|
81
|
-
if (output.endsWith(".js")) output = `${output.slice(0, -2)}${ext}`;
|
|
82
|
-
else if (output.endsWith(".ts")) output = `${output.slice(0, -2)}${ext}`;
|
|
83
|
-
else if (output.endsWith(".mts")) output = `${output.slice(0, -3)}${ext}`;
|
|
84
|
-
else if (output.endsWith(".cts")) output = `${output.slice(0, -3)}${cts}`;
|
|
85
|
-
if (outDir.startsWith("./") || outDir.startsWith("../")) return output;
|
|
86
|
-
else return `./${output}`;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function getLongestOutput(outDir, entries) {
|
|
90
|
-
const outputs = [];
|
|
91
|
-
for (const entry of entries) {
|
|
92
|
-
if (entry.copy) outputs.push(entry.copy.output);
|
|
93
|
-
if (entry.input) {
|
|
94
|
-
const out = entry.output || getOutputPath(outDir, entry.input);
|
|
95
|
-
outputs.push(out);
|
|
96
|
-
}
|
|
97
|
-
if (entry.declaration || entry.dts) {
|
|
98
|
-
const dts = entry.declaration || entry.dts;
|
|
99
|
-
const out = entry.output || getOutputPath(outDir, dts, true);
|
|
100
|
-
outputs.push(out);
|
|
101
|
-
}
|
|
102
|
-
if (entry.template) outputs.push(entry.output);
|
|
103
|
-
}
|
|
104
|
-
return Math.max(...outputs.map((v) => v.length));
|
|
59
|
+
const decimals = 2;
|
|
60
|
+
const units = [
|
|
61
|
+
"B",
|
|
62
|
+
"KB",
|
|
63
|
+
"MB",
|
|
64
|
+
"GB",
|
|
65
|
+
"TB"
|
|
66
|
+
];
|
|
67
|
+
if (bytes === 0) return `0 B`;
|
|
68
|
+
const k = 1024;
|
|
69
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
70
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
71
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${units[i]}`;
|
|
105
72
|
}
|
|
106
73
|
|
|
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
|
-
if (args.config) {
|
|
138
|
-
const path = args.config;
|
|
139
|
-
const isConfig = await exists(path);
|
|
140
|
-
if (isConfig) return await loadConfig(cwd, path, defaults);
|
|
141
|
-
else return logger.exit(warnMessage);
|
|
142
|
-
}
|
|
143
|
-
const configName = "bundler.config";
|
|
144
|
-
const configExts = [".ts", ".js", ".mts", ".mjs"];
|
|
145
|
-
for (const ext of configExts) {
|
|
146
|
-
const path = `${configName}${ext}`;
|
|
147
|
-
const isConfig = await exists(path);
|
|
148
|
-
if (isConfig) return await loadConfig(cwd, path, defaults);
|
|
149
|
-
}
|
|
150
|
-
return logger.exit(warnMessage);
|
|
151
|
-
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/config.ts
|
|
76
|
+
/**
|
|
77
|
+
* List of global default patterns for external module identifiers.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { externals } from '@hypernym/bundler'
|
|
83
|
+
*
|
|
84
|
+
* export default defineConfig({
|
|
85
|
+
* entries: [
|
|
86
|
+
* {
|
|
87
|
+
* input: './src/index.ts',
|
|
88
|
+
* externals: [...externals, 'id', /regexp/]
|
|
89
|
+
* },
|
|
90
|
+
* ]
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
const externals = [
|
|
95
|
+
/^node:/,
|
|
96
|
+
/^@types/,
|
|
97
|
+
/^@rollup/,
|
|
98
|
+
/^@rolldown/,
|
|
99
|
+
/^@hypernym/,
|
|
100
|
+
/^rollup/,
|
|
101
|
+
/^rolldown/
|
|
102
|
+
];
|
|
152
103
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
return null;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/bin/loader.ts
|
|
106
|
+
async function getTSConfigPath(cwd$1, filePath = "tsconfig.json") {
|
|
107
|
+
const tsconfigPath = resolve(cwd$1, filePath);
|
|
108
|
+
const tsconfigFile = await exists(tsconfigPath);
|
|
109
|
+
if (tsconfigFile) return tsconfigPath;
|
|
161
110
|
}
|
|
162
|
-
function
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
...options,
|
|
183
|
-
sourcefile: id
|
|
184
|
-
});
|
|
185
|
-
return {
|
|
186
|
-
code: result.code,
|
|
187
|
-
map: result.map || null
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
|
-
async renderChunk(code, { fileName }) {
|
|
191
|
-
if (!options?.minify) return null;
|
|
192
|
-
if (/\.d\.(c|m)?tsx?$/.test(fileName)) return null;
|
|
193
|
-
const result = await transform(code, {
|
|
194
|
-
...options,
|
|
195
|
-
sourcefile: fileName,
|
|
196
|
-
minify: true
|
|
197
|
-
});
|
|
198
|
-
return {
|
|
199
|
-
code: result.code,
|
|
200
|
-
map: result.map || null
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
};
|
|
111
|
+
async function loadConfig(filePath, defaults) {
|
|
112
|
+
const cwd$1 = defaults.cwd;
|
|
113
|
+
const result = await build({
|
|
114
|
+
input: resolve(cwd$1, filePath),
|
|
115
|
+
write: false,
|
|
116
|
+
external: (id) => !(isAbsolute(id) || /^(\.|@\/|~\/)/.test(id)),
|
|
117
|
+
resolve: { tsconfigFilename: defaults.tsconfig },
|
|
118
|
+
output: { format: "esm" }
|
|
119
|
+
});
|
|
120
|
+
const tempConfig = resolve(cwd$1, "node_modules/.hypernym/bundler/config.mjs");
|
|
121
|
+
await write(tempConfig, result.output[0].code);
|
|
122
|
+
const config = (await import(tempConfig)).default;
|
|
123
|
+
const options = {
|
|
124
|
+
...defaults,
|
|
125
|
+
...config
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
options,
|
|
129
|
+
path: filePath
|
|
130
|
+
};
|
|
204
131
|
}
|
|
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
|
-
async function build(cwd, options) {
|
|
242
|
-
const { outDir = "dist", hooks } = options;
|
|
243
|
-
let start = 0;
|
|
244
|
-
const buildStats = {
|
|
245
|
-
cwd,
|
|
246
|
-
size: 0,
|
|
247
|
-
buildTime: 0,
|
|
248
|
-
files: []
|
|
249
|
-
};
|
|
250
|
-
await hooks?.["build:start"]?.(options, buildStats);
|
|
251
|
-
if (options.entries) {
|
|
252
|
-
const longestOutput = getLongestOutput(outDir, options.entries);
|
|
253
|
-
start = Date.now();
|
|
254
|
-
const aliasDir = resolve(cwd, "./src");
|
|
255
|
-
let aliasOptions = {
|
|
256
|
-
entries: options.alias || [
|
|
257
|
-
{ find: "@", replacement: aliasDir },
|
|
258
|
-
{ find: "~", replacement: aliasDir }
|
|
259
|
-
]
|
|
260
|
-
};
|
|
261
|
-
for (const entry of options.entries) {
|
|
262
|
-
const entryStart = Date.now();
|
|
263
|
-
if (entry.copy) {
|
|
264
|
-
const _entry = {
|
|
265
|
-
input: isString(entry.copy.input) ? [entry.copy.input] : entry.copy.input,
|
|
266
|
-
output: entry.copy.output,
|
|
267
|
-
recursive: entry.copy.recursive || true,
|
|
268
|
-
filter: entry.copy.filter
|
|
269
|
-
};
|
|
270
|
-
const buildLogs = [];
|
|
271
|
-
for (const copyInput of _entry.input) {
|
|
272
|
-
const fileSrc = resolve(cwd, copyInput);
|
|
273
|
-
const fileDist = resolve(cwd, _entry.output, copyInput);
|
|
274
|
-
await copy(fileSrc, fileDist, {
|
|
275
|
-
recursive: _entry.recursive,
|
|
276
|
-
filter: _entry.filter
|
|
277
|
-
}).catch(error);
|
|
278
|
-
const stats = await stat(fileDist);
|
|
279
|
-
let totalSize = 0;
|
|
280
|
-
if (!stats.isDirectory()) totalSize = stats.size;
|
|
281
|
-
else {
|
|
282
|
-
const files = await readdir(fileDist);
|
|
283
|
-
for (const file of files) {
|
|
284
|
-
const filePath = resolve(fileDist, file);
|
|
285
|
-
const fileStat = await stat(filePath);
|
|
286
|
-
totalSize = totalSize + fileStat.size;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
const parseInput = (path) => {
|
|
290
|
-
if (path.startsWith("./")) return path.slice(2);
|
|
291
|
-
else return path;
|
|
292
|
-
};
|
|
293
|
-
const parseOutput = (path) => {
|
|
294
|
-
if (path.startsWith("./")) return path;
|
|
295
|
-
else return `./${path}`;
|
|
296
|
-
};
|
|
297
|
-
const fileStats = {
|
|
298
|
-
cwd,
|
|
299
|
-
path: `${parseOutput(_entry.output)}/${parseInput(copyInput)}`,
|
|
300
|
-
size: totalSize,
|
|
301
|
-
buildTime: Date.now() - entryStart,
|
|
302
|
-
format: "copy",
|
|
303
|
-
logs: buildLogs
|
|
304
|
-
};
|
|
305
|
-
buildStats.files.push(fileStats);
|
|
306
|
-
buildStats.size = buildStats.size + stats.size;
|
|
307
|
-
logModuleStats(fileStats, longestOutput);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
if (entry.input) {
|
|
311
|
-
const logFilter = getLogFilter(entry.logFilter || []);
|
|
312
|
-
const _output = entry.output || getOutputPath(outDir, entry.input);
|
|
313
|
-
let _format = "esm";
|
|
314
|
-
if (_output.endsWith(".cjs")) _format = "cjs";
|
|
315
|
-
const buildLogs = [];
|
|
316
|
-
const _entry = {
|
|
317
|
-
input: entry.input,
|
|
318
|
-
output: _output,
|
|
319
|
-
externals: entry.externals || options.externals,
|
|
320
|
-
format: entry.format || _format,
|
|
321
|
-
...entry,
|
|
322
|
-
defaultPlugins: [
|
|
323
|
-
esbuild({
|
|
324
|
-
minify: !isUndefined(entry.minify) ? entry.minify : options.minify,
|
|
325
|
-
...entry.transformers?.esbuild
|
|
326
|
-
})
|
|
327
|
-
]
|
|
328
|
-
};
|
|
329
|
-
if (!entry.plugins) {
|
|
330
|
-
if (_entry.transformers?.json) {
|
|
331
|
-
const jsonOptions = isObject(_entry.transformers.json) ? _entry.transformers.json : void 0;
|
|
332
|
-
_entry.defaultPlugins.push(jsonPlugin(jsonOptions));
|
|
333
|
-
}
|
|
334
|
-
if (_entry.transformers?.replace) {
|
|
335
|
-
_entry.defaultPlugins.unshift(
|
|
336
|
-
replacePlugin({
|
|
337
|
-
preventAssignment: true,
|
|
338
|
-
..._entry.transformers.replace
|
|
339
|
-
})
|
|
340
|
-
);
|
|
341
|
-
}
|
|
342
|
-
if (_entry.transformers?.resolve) {
|
|
343
|
-
const resolveOptions = isObject(_entry.transformers.resolve) ? _entry.transformers.resolve : void 0;
|
|
344
|
-
_entry.defaultPlugins.unshift(resolvePlugin(resolveOptions));
|
|
345
|
-
}
|
|
346
|
-
_entry.defaultPlugins.unshift(
|
|
347
|
-
aliasPlugin(_entry.transformers?.alias || aliasOptions)
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
const fileStats = {
|
|
351
|
-
cwd,
|
|
352
|
-
path: _entry.output,
|
|
353
|
-
size: 0,
|
|
354
|
-
buildTime: entryStart,
|
|
355
|
-
format: _entry.format,
|
|
356
|
-
logs: buildLogs
|
|
357
|
-
};
|
|
358
|
-
await hooks?.["build:entry:start"]?.(_entry, fileStats);
|
|
359
|
-
const _build = await rollup({
|
|
360
|
-
input: resolve(cwd, _entry.input),
|
|
361
|
-
external: _entry.externals,
|
|
362
|
-
plugins: _entry.plugins || _entry.defaultPlugins,
|
|
363
|
-
onLog: (level, log) => {
|
|
364
|
-
if (logFilter(log)) buildLogs.push({ level, log });
|
|
365
|
-
}
|
|
366
|
-
});
|
|
367
|
-
await _build.write({
|
|
368
|
-
file: resolve(cwd, _entry.output),
|
|
369
|
-
format: _entry.format,
|
|
370
|
-
banner: _entry.banner,
|
|
371
|
-
footer: _entry.footer,
|
|
372
|
-
intro: _entry.intro,
|
|
373
|
-
outro: _entry.outro,
|
|
374
|
-
paths: _entry.paths,
|
|
375
|
-
name: _entry.name,
|
|
376
|
-
globals: _entry.globals,
|
|
377
|
-
extend: _entry.extend
|
|
378
|
-
});
|
|
379
|
-
const stats = await stat(resolve(cwd, _entry.output));
|
|
380
|
-
fileStats.size = stats.size;
|
|
381
|
-
fileStats.buildTime = Date.now() - entryStart;
|
|
382
|
-
fileStats.logs = buildLogs;
|
|
383
|
-
buildStats.files.push(fileStats);
|
|
384
|
-
buildStats.size = buildStats.size + stats.size;
|
|
385
|
-
logModuleStats(fileStats, longestOutput);
|
|
386
|
-
await hooks?.["build:entry:end"]?.(_entry, fileStats);
|
|
387
|
-
}
|
|
388
|
-
if (entry.dts || entry.declaration) {
|
|
389
|
-
const logFilter = getLogFilter(entry.logFilter || []);
|
|
390
|
-
const buildLogs = [];
|
|
391
|
-
const dts$1 = entry.dts || entry.declaration;
|
|
392
|
-
const _entry = {
|
|
393
|
-
dts: dts$1,
|
|
394
|
-
output: entry.output || getOutputPath(outDir, dts$1, true),
|
|
395
|
-
externals: entry.externals || options.externals,
|
|
396
|
-
format: entry.format || "esm",
|
|
397
|
-
...entry,
|
|
398
|
-
defaultPlugins: [dts(entry.transformers?.dts)]
|
|
399
|
-
};
|
|
400
|
-
if (!entry.plugins) {
|
|
401
|
-
_entry.defaultPlugins.unshift(
|
|
402
|
-
aliasPlugin(_entry.transformers?.alias || aliasOptions)
|
|
403
|
-
);
|
|
404
|
-
}
|
|
405
|
-
const fileStats = {
|
|
406
|
-
cwd,
|
|
407
|
-
path: _entry.output,
|
|
408
|
-
size: 0,
|
|
409
|
-
buildTime: entryStart,
|
|
410
|
-
format: "dts",
|
|
411
|
-
logs: buildLogs
|
|
412
|
-
};
|
|
413
|
-
await hooks?.["build:entry:start"]?.(_entry, fileStats);
|
|
414
|
-
const _build = await rollup({
|
|
415
|
-
input: resolve(cwd, _entry.dts),
|
|
416
|
-
external: _entry.externals,
|
|
417
|
-
plugins: _entry.plugins || _entry.defaultPlugins,
|
|
418
|
-
onLog: (level, log) => {
|
|
419
|
-
if (logFilter(log)) buildLogs.push({ level, log });
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
await _build.write({
|
|
423
|
-
file: resolve(cwd, _entry.output),
|
|
424
|
-
format: _entry.format,
|
|
425
|
-
banner: _entry.banner,
|
|
426
|
-
footer: _entry.footer,
|
|
427
|
-
intro: _entry.intro,
|
|
428
|
-
outro: _entry.outro,
|
|
429
|
-
paths: _entry.paths
|
|
430
|
-
});
|
|
431
|
-
const stats = await stat(resolve(cwd, _entry.output));
|
|
432
|
-
fileStats.size = stats.size;
|
|
433
|
-
fileStats.buildTime = Date.now() - entryStart;
|
|
434
|
-
fileStats.logs = buildLogs;
|
|
435
|
-
buildStats.files.push(fileStats);
|
|
436
|
-
buildStats.size = buildStats.size + stats.size;
|
|
437
|
-
logModuleStats(fileStats, longestOutput);
|
|
438
|
-
await hooks?.["build:entry:end"]?.(_entry, fileStats);
|
|
439
|
-
}
|
|
440
|
-
if (entry.template && entry.output) {
|
|
441
|
-
const buildLogs = [];
|
|
442
|
-
await write(entry.output, entry.template);
|
|
443
|
-
const stats = await stat(resolve(cwd, entry.output));
|
|
444
|
-
const fileStats = {
|
|
445
|
-
cwd,
|
|
446
|
-
path: entry.output,
|
|
447
|
-
size: stats.size,
|
|
448
|
-
buildTime: Date.now() - entryStart,
|
|
449
|
-
format: "tmp",
|
|
450
|
-
logs: buildLogs
|
|
451
|
-
};
|
|
452
|
-
buildStats.files.push(fileStats);
|
|
453
|
-
buildStats.size = buildStats.size + stats.size;
|
|
454
|
-
logModuleStats(fileStats, longestOutput);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
buildStats.buildTime = Date.now() - start;
|
|
458
|
-
}
|
|
459
|
-
await hooks?.["build:end"]?.(options, buildStats);
|
|
460
|
-
return buildStats;
|
|
132
|
+
async function createConfigLoader(args) {
|
|
133
|
+
const cwdir = args.cwd && args.cwd.trim() !== "" ? resolve(args.cwd) : cwd();
|
|
134
|
+
const tsconfig = await getTSConfigPath(cwdir, args.tsconfig);
|
|
135
|
+
const pkgPath = resolve(cwdir, "package.json");
|
|
136
|
+
const pkgFile = await read(pkgPath).catch(error);
|
|
137
|
+
const { dependencies } = JSON.parse(pkgFile);
|
|
138
|
+
const defaults = {
|
|
139
|
+
cwd: cwdir,
|
|
140
|
+
tsconfig,
|
|
141
|
+
externals: [...Object.keys(dependencies || {}), ...externals],
|
|
142
|
+
entries: []
|
|
143
|
+
};
|
|
144
|
+
const warnMessage = `Missing required configuration. To start bundling, add the ${cyan(`'bundler.config.{js,mjs,ts,mts}'`)} file to the project's root.`;
|
|
145
|
+
if (args.config) {
|
|
146
|
+
const path = resolve(args.config);
|
|
147
|
+
const isConfig = await exists(path);
|
|
148
|
+
if (isConfig) return await loadConfig(path, defaults);
|
|
149
|
+
else return logger.exit(warnMessage);
|
|
150
|
+
}
|
|
151
|
+
const configName = "bundler.config";
|
|
152
|
+
const configExts = [
|
|
153
|
+
".ts",
|
|
154
|
+
".mts",
|
|
155
|
+
".mjs",
|
|
156
|
+
".js"
|
|
157
|
+
];
|
|
158
|
+
for (const ext of configExts) {
|
|
159
|
+
const path = resolve(cwdir, `${configName}${ext}`);
|
|
160
|
+
const isConfig = await exists(path);
|
|
161
|
+
if (isConfig) return await loadConfig(path, defaults);
|
|
162
|
+
}
|
|
163
|
+
return logger.exit(warnMessage);
|
|
461
164
|
}
|
|
462
165
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
cl(`${modules} transformed. Total size is ${buildSize}`);
|
|
492
|
-
cl(`Bundle is generated and ready for production`);
|
|
493
|
-
cl();
|
|
494
|
-
}).catch(error);
|
|
495
|
-
await hooks?.["bundle:end"]?.(options);
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/bin/builder.ts
|
|
168
|
+
async function createBuilder(config) {
|
|
169
|
+
const { options, path: configPath } = config;
|
|
170
|
+
const { hooks } = options;
|
|
171
|
+
const cl$1 = console.log;
|
|
172
|
+
await hooks?.["bundle:start"]?.(options);
|
|
173
|
+
cl$1();
|
|
174
|
+
logger.info(dim(`v${version}`));
|
|
175
|
+
cl$1("Config", dim(configPath));
|
|
176
|
+
cl$1();
|
|
177
|
+
cl$1("Processing specified entries...");
|
|
178
|
+
cl$1();
|
|
179
|
+
await build$1(options).then((stats) => {
|
|
180
|
+
const entriesLength = options.entries.length;
|
|
181
|
+
const totalEntries = `${entriesLength} ${entriesLength > 1 ? "entries" : "entry"}`;
|
|
182
|
+
const filesLength = stats.files.length;
|
|
183
|
+
const totalFiles = `${stats.files.length} file${filesLength > 1 ? "s" : ""}`;
|
|
184
|
+
const buildTime = formatMs(stats.buildTime);
|
|
185
|
+
const buildSize = formatBytes(stats.size);
|
|
186
|
+
cl$1();
|
|
187
|
+
cl$1("Stats:", dim(`${totalEntries}, ${totalFiles}, ${buildSize}, ${buildTime}`));
|
|
188
|
+
cl$1();
|
|
189
|
+
cl$1("All entries successfully processed.");
|
|
190
|
+
cl$1("Bundle is optimized and ready for production.");
|
|
191
|
+
cl$1();
|
|
192
|
+
}).catch(error);
|
|
193
|
+
await hooks?.["bundle:end"]?.(options);
|
|
496
194
|
}
|
|
497
195
|
|
|
196
|
+
//#endregion
|
|
197
|
+
//#region src/bin/index.ts
|
|
498
198
|
async function main() {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
});
|
|
503
|
-
const config = await createConfigLoader(cwd$1, args);
|
|
504
|
-
await createBuilder(cwd$1, config);
|
|
199
|
+
const args = createArgs({ alias: { config: "c" } });
|
|
200
|
+
const config = await createConfigLoader(args);
|
|
201
|
+
await createBuilder(config);
|
|
505
202
|
}
|
|
506
203
|
main().catch(error);
|
|
204
|
+
|
|
205
|
+
//#endregion
|