@hypernym/bundler 0.1.2 → 0.2.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.
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
- import process, { stdout, cwd } from 'node:process';
2
+ import process, { cwd } from 'node:process';
3
3
  import { createArgs } from '@hypernym/args';
4
- import { readFile, mkdir, writeFile, stat } from 'node:fs/promises';
4
+ import { readFile, stat } from 'node:fs/promises';
5
5
  import { resolve, parse } from 'node:path';
6
- import { exists } from '@hypernym/utils/node';
7
- import { dim, magenta, red, cyan, green } from '@hypernym/colors';
6
+ import { exists, writeFile } from '@hypernym/utils/fs';
7
+ import { cyan, dim, red, magenta, green } from '@hypernym/colors';
8
8
  import { build as build$1, transform } from 'esbuild';
9
9
  import { createSpinner } from '@hypernym/spinner';
10
10
  import { isObject } from '@hypernym/utils';
@@ -25,31 +25,28 @@ const externals = [
25
25
  ];
26
26
 
27
27
  const name = "bundler";
28
- const version = `0.1.2`;
28
+ const version = `0.2.0`;
29
29
 
30
30
  const cl = console.log;
31
- const log = (...args) => {
32
- let length = args.length + 2;
33
- const cols = stdout.columns || 80;
34
- const time = (/* @__PURE__ */ new Date()).toLocaleTimeString();
35
- for (const arg of args) {
36
- length = length + arg.replace(/\u001b\[.*?m/g, "").length;
37
- }
38
- const repeatLength = cols <= length + time.length ? 0 : cols - (length + time.length);
39
- return cl(...args, " ".repeat(repeatLength), dim(time));
40
- };
41
31
  const logger = {
32
+ info: (...args) => {
33
+ const time = `[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}]`;
34
+ return cl(cyan(name), dim(time), ...args);
35
+ },
36
+ error: (...args) => {
37
+ const time = `[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}]`;
38
+ return cl(red(name), dim(time), ...args);
39
+ },
42
40
  exit: (message) => {
43
- cl();
44
- log(dim(name), dim(version));
45
- log(magenta(name), message);
46
- cl();
41
+ const time = `[${(/* @__PURE__ */ new Date()).toLocaleTimeString()}]`;
42
+ cl(magenta(name), dim(time), version);
43
+ cl(magenta(name), dim(time), message);
47
44
  return process.exit(1);
48
45
  }
49
46
  };
50
47
 
51
48
  function error(err) {
52
- log(red(name), "Something went wrong...");
49
+ logger.error("Something went wrong...");
53
50
  console.error(err);
54
51
  return process.exit(1);
55
52
  }
@@ -106,9 +103,7 @@ async function loadConfig(cwd, filePath, defaults) {
106
103
  packages: "external"
107
104
  });
108
105
  const code = result.outputFiles[0].text;
109
- const tempDir = resolve(cwd, "node_modules", ".hypernym", "bundler");
110
- const tempConfig = resolve(tempDir, "config.mjs");
111
- await mkdir(tempDir, { recursive: true });
106
+ const tempConfig = resolve(cwd, "node_modules/.hypernym/bundler/config.mjs");
112
107
  await writeFile(tempConfig, code, "utf-8");
113
108
  const content = await import(tempConfig);
114
109
  const config = {
@@ -201,8 +196,8 @@ async function build(cwd, options) {
201
196
  buildTime: 0,
202
197
  files: []
203
198
  };
204
- if (hooks?.["build:before"])
205
- await hooks["build:before"](options);
199
+ if (hooks?.["build:start"])
200
+ await hooks["build:start"](options, buildStats);
206
201
  if (options.entries) {
207
202
  start = Date.now();
208
203
  for (const entry of options.entries) {
@@ -296,33 +291,24 @@ async function build(cwd, options) {
296
291
  }
297
292
  buildStats.buildTime = Date.now() - start;
298
293
  }
299
- if (hooks?.["build:done"])
300
- await hooks["build:done"](options);
294
+ if (hooks?.["build:end"])
295
+ await hooks["build:end"](options, buildStats);
301
296
  return buildStats;
302
297
  }
303
298
 
304
299
  async function createBuilder(cwd, args, options) {
300
+ const { hooks } = options;
301
+ if (hooks?.["bundle:start"])
302
+ await hooks["bundle:start"](options);
303
+ logger.info(version);
304
+ logger.info(`Bundling started...`);
305
305
  const spinner = createSpinner();
306
- cl();
307
- log(dim(name), dim(version));
308
- log(cyan(name), `Bundling started...`);
309
306
  spinner.start({
310
307
  message: `Transforming modules ${green("for production...")}`
311
308
  });
312
- return await build(cwd, options).then((stats) => {
309
+ await build(cwd, options).then((stats) => {
313
310
  spinner.stop({
314
- message: `Modules transformation is ${green("done!")}`,
315
- template: (mark, message) => {
316
- const temp = `${mark}${message}`;
317
- const cols = stdout.columns || 80;
318
- const time = (/* @__PURE__ */ new Date()).toLocaleTimeString();
319
- const length = (
320
- // eslint-disable-next-line no-control-regex
321
- temp.replace(/\u001b\[.*?m/g, "").length + time.length + 1
322
- );
323
- const repeatLength = cols <= length ? 0 : cols - length;
324
- return temp + " ".repeat(repeatLength) + dim(time);
325
- }
311
+ message: `Modules transformation is ${green("done!")}`
326
312
  });
327
313
  const check = green("\u2714");
328
314
  const info = cyan("i");
@@ -336,9 +322,9 @@ async function createBuilder(cwd, args, options) {
336
322
  const longestSize = Math.max(
337
323
  ...stats.files.map((v) => formatBytes(v.size).length)
338
324
  );
339
- log(check, `Bundling completed in ${buildTime}`);
340
- log(check, `${modules} modules transformed. Total size is ${buildSize}`);
341
- log(info, "Individual stats per module");
325
+ cl(check, `Bundling completed in ${buildTime}`);
326
+ cl(check, `${modules} modules transformed. Total size is ${buildSize}`);
327
+ cl(info, "Individual stats per module");
342
328
  for (const file of stats.files) {
343
329
  let format = file.format;
344
330
  const base = parse(file.path).base;
@@ -352,11 +338,11 @@ async function createBuilder(cwd, args, options) {
352
338
  if (base.includes(".d."))
353
339
  format = "dts";
354
340
  if (file.logs) {
355
- for (const log2 of file.logs) {
356
- cl(magenta("!"), magenta(log2.log.message));
341
+ for (const log of file.logs) {
342
+ cl(magenta("!"), magenta(log.log.message));
357
343
  }
358
344
  }
359
- log(
345
+ cl(
360
346
  info,
361
347
  dim("\u251C\u2500"),
362
348
  dim(path) + cyan(base),
@@ -368,7 +354,7 @@ async function createBuilder(cwd, args, options) {
368
354
  dim(formatBytes(file.size).padStart(longestSize))
369
355
  );
370
356
  }
371
- log(
357
+ logger.info(
372
358
  check,
373
359
  `Bundle is now fully generated and ready ${green("for production!")}`
374
360
  );
@@ -377,10 +363,12 @@ async function createBuilder(cwd, args, options) {
377
363
  mark: red("\u2716"),
378
364
  message: `Modules transformation is ${red("interupted!")}`
379
365
  });
380
- log(red(name), "Something went wrong...");
366
+ logger.error("Something went wrong...");
381
367
  console.error(err);
382
368
  return process.exit(1);
383
369
  });
370
+ if (hooks?.["bundle:end"])
371
+ await hooks["bundle:end"](options);
384
372
  }
385
373
 
386
374
  async function main() {
@@ -1,4 +1,4 @@
1
- import { OutputOptions } from 'rollup';
1
+ import { OutputOptions, LogLevel, RollupLog } from 'rollup';
2
2
  import { RollupReplaceOptions } from '@rollup/plugin-replace';
3
3
  import { RollupJsonOptions } from '@rollup/plugin-json';
4
4
  import { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
@@ -82,15 +82,40 @@ interface EntryTypes extends Entry {
82
82
  }
83
83
  type EntriesOptions = EntryInput | EntryTypes;
84
84
 
85
- interface BuildHooks {
85
+ interface BuildLogs {
86
+ level: LogLevel;
87
+ log: RollupLog;
88
+ }
89
+ interface BuildStats {
90
+ cwd: string;
91
+ size: number;
92
+ buildTime: number;
93
+ files: {
94
+ path: string;
95
+ size: number;
96
+ buildTime: number;
97
+ format: string;
98
+ logs: BuildLogs[];
99
+ }[];
100
+ }
101
+
102
+ interface HooksOptions {
86
103
  /**
87
104
  * Called just before bundling started.
88
105
  */
89
- 'build:before'?: (options?: Options) => void | Promise<void>;
106
+ 'bundle:start'?: (options?: Options) => void | Promise<void>;
107
+ /**
108
+ * Called just before building started.
109
+ */
110
+ 'build:start'?: (options?: Options, buildStats?: BuildStats) => void | Promise<void>;
111
+ /**
112
+ * Called right after building is complete.
113
+ */
114
+ 'build:end'?: (options?: Options, buildStats?: BuildStats) => void | Promise<void>;
90
115
  /**
91
116
  * Called right after bundling is complete.
92
117
  */
93
- 'build:done'?: (options?: Options) => void | Promise<void>;
118
+ 'bundle:end'?: (options?: Options) => void | Promise<void>;
94
119
  }
95
120
 
96
121
  interface Options {
@@ -118,11 +143,11 @@ interface Options {
118
143
  */
119
144
  externals?: (string | RegExp)[];
120
145
  /**
121
- * Provides a powerful hooking system to further expand build mode.
146
+ * Provides a powerful hooking system to further expand bundling mode.
122
147
  *
123
148
  * @default undefined
124
149
  */
125
- hooks?: BuildHooks;
150
+ hooks?: HooksOptions;
126
151
  }
127
152
 
128
153
  declare const externals: RegExp[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",
@@ -8,6 +8,7 @@
8
8
  "homepage": "https://github.com/hypernym-studio/bundler",
9
9
  "funding": "https://github.com/sponsors/ivodolenc",
10
10
  "type": "module",
11
+ "types": "./dist/types/index.d.ts",
11
12
  "exports": {
12
13
  ".": {
13
14
  "types": "./dist/types/index.d.ts",
@@ -55,22 +56,22 @@
55
56
  }
56
57
  },
57
58
  "dependencies": {
58
- "@hypernym/args": "^0.2.0",
59
- "@hypernym/colors": "^1.0.0",
60
- "@hypernym/spinner": "^0.1.0",
61
- "@hypernym/utils": "^2.0.2",
59
+ "@hypernym/args": "^0.2.1",
60
+ "@hypernym/colors": "^1.0.1",
61
+ "@hypernym/spinner": "^0.2.0",
62
+ "@hypernym/utils": "^2.1.0",
62
63
  "@rollup/plugin-json": "^6.0.1",
63
64
  "@rollup/plugin-node-resolve": "^15.2.3",
64
65
  "@rollup/plugin-replace": "^5.0.3",
65
66
  "esbuild": "^0.19.4",
66
- "rollup": "^4.0.2",
67
+ "rollup": "^4.1.1",
67
68
  "rollup-plugin-dts": "^6.1.0"
68
69
  },
69
70
  "devDependencies": {
70
- "@hypernym/eslint-config": "^2.0.1",
71
- "@hypernym/prettier-config": "^2.0.1",
71
+ "@hypernym/eslint-config": "^2.0.2",
72
+ "@hypernym/prettier-config": "^2.0.2",
72
73
  "@hypernym/tsconfig": "^1.1.0",
73
- "@types/node": "^20.8.4",
74
+ "@types/node": "^20.8.6",
74
75
  "eslint": "^8.51.0",
75
76
  "prettier": "^3.0.3",
76
77
  "tsx": "^3.13.0",