bunchee 5.2.0-beta.0 → 5.2.0-beta.1

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/bin/cli.js CHANGED
@@ -1,11 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  var path = require('path');
3
3
  var arg = require('arg');
4
+ var perf_hooks = require('perf_hooks');
4
5
  var fs = require('fs');
5
6
  var fsp = require('fs/promises');
6
7
  var require$$0 = require('tty');
7
- var bunchee = require('bunchee');
8
+ var index_js = require('../index.js');
8
9
  require('module');
10
+ var prettyBytes = require('pretty-bytes');
9
11
 
10
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
13
 
@@ -14,6 +16,7 @@ var arg__default = /*#__PURE__*/_interopDefault(arg);
14
16
  var fs__default = /*#__PURE__*/_interopDefault(fs);
15
17
  var fsp__default = /*#__PURE__*/_interopDefault(fsp);
16
18
  var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
19
+ var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
17
20
 
18
21
  const availableExtensions = new Set([
19
22
  'js',
@@ -474,7 +477,7 @@ function lint$1(pkg) {
474
477
  }
475
478
  }
476
479
 
477
- var version = "5.2.0-beta.0";
480
+ var version = "5.2.0-beta.1";
478
481
 
479
482
  function relativify(path) {
480
483
  return path.startsWith('.') ? path : `./${path}`;
@@ -847,6 +850,64 @@ async function prepare(cwd) {
847
850
  logger.info('Configured `exports` in package.json');
848
851
  }
849
852
 
853
+ function isTypeFile(filename) {
854
+ return filename.endsWith('.d.ts') || filename.endsWith('.d.mts') || filename.endsWith('.d.cts');
855
+ }
856
+ function normalizeExportName(exportName) {
857
+ const isBinary = isBinExportPath(exportName);
858
+ let result = exportName;
859
+ if (isBinary) {
860
+ result = (exportName.replace(new RegExp(`^\\${BINARY_TAG}\\/?`), '') || '.') + ' (bin)';
861
+ } else {
862
+ const normalizedExportPath = normalizeExportPath(exportName);
863
+ const specialConditionName = getSpecialExportTypeFromComposedExportPath(exportName);
864
+ result = normalizedExportPath + (specialConditionName !== 'default' ? ` (${specialConditionName})` : '');
865
+ }
866
+ return result;
867
+ }
868
+ function logOutputState(stats) {
869
+ if (stats.size === 0) {
870
+ logger.warn('No build info can be logged');
871
+ return;
872
+ }
873
+ const allFileNameLengths = Array.from(stats.values()).flat(1).map(([filename])=>filename.length);
874
+ const maxFilenameLength = Math.max(...allFileNameLengths);
875
+ const statsArray = [
876
+ ...stats.entries()
877
+ ].sort(([a], [b])=>{
878
+ const comp = normalizeExportPath(a).length - normalizeExportPath(b).length;
879
+ return comp === 0 ? a.localeCompare(b) : comp;
880
+ });
881
+ const maxLengthOfExportName = Math.max(...statsArray.map(([exportName])=>normalizeExportName(exportName).length));
882
+ console.log(pc.underline('Exports'), ' '.repeat(Math.max(maxLengthOfExportName - 'Exports'.length, 0)), pc.underline('File'), ' '.repeat(Math.max(maxFilenameLength - 'File'.length, 0)), pc.underline('Size'));
883
+ statsArray.forEach(([exportName, filesList])=>{
884
+ // sort by file type, first js files then types, js/mjs/cjs are prioritized than .d.ts/.d.mts/.d.cts
885
+ filesList.sort(([a], [b])=>{
886
+ const aIsType = isTypeFile(a);
887
+ const bIsType = isTypeFile(b);
888
+ if (aIsType && bIsType) {
889
+ return 0;
890
+ }
891
+ if (aIsType) {
892
+ return 1;
893
+ }
894
+ if (bIsType) {
895
+ return -1;
896
+ }
897
+ return 0;
898
+ }).forEach((item, index)=>{
899
+ const [filename, , size] = item;
900
+ const normalizedExportName = normalizeExportName(exportName);
901
+ const prefix = index === 0 ? normalizedExportName : ' '.repeat(normalizedExportName.length);
902
+ const filenamePadding = ' '.repeat(Math.max(maxLengthOfExportName, 'Exports'.length) - normalizedExportName.length);
903
+ const isType = isTypeFile(filename);
904
+ const sizePadding = ' '.repeat(Math.max(maxFilenameLength, 'File'.length) - filename.length);
905
+ const prettiedSize = prettyBytes__default.default(size);
906
+ console.log(prefix, filenamePadding, `${pc[isType ? 'dim' : 'bold'](filename)}`, sizePadding, prettiedSize);
907
+ });
908
+ });
909
+ }
910
+
850
911
  const helpMessage = `
851
912
  Usage: bunchee [options]
852
913
 
@@ -963,14 +1024,60 @@ async function run(args) {
963
1024
  const cliEntry = source ? path__default.default.resolve(cwd, source) : '';
964
1025
  // lint package
965
1026
  await lint(cwd);
1027
+ const { default: ora } = await import('ora');
1028
+ const spinner = ora({
1029
+ text: 'Building...\n',
1030
+ color: 'green'
1031
+ });
1032
+ function stopSpinner() {
1033
+ if (spinner.isSpinning) {
1034
+ spinner.stop();
1035
+ }
1036
+ }
1037
+ let initialBuildContext;
1038
+ function onBuildStart(buildContext) {
1039
+ initialBuildContext = buildContext;
1040
+ }
1041
+ function onBuildEnd(assetJobs) {
1042
+ if (watch) {
1043
+ logWatcherBuildTime(assetJobs);
1044
+ } else {
1045
+ // Stop spinner before logging output files and sizes
1046
+ stopSpinner();
1047
+ if (assetJobs.length === 0) {
1048
+ logger.warn('The "src" directory does not contain any entry files. ' + 'For proper usage, please refer to the following link: ' + 'https://github.com/huozhi/bunchee#usage');
1049
+ }
1050
+ const outputState = initialBuildContext == null ? void 0 : initialBuildContext.pluginContext.outputState;
1051
+ if (outputState) {
1052
+ logOutputState(outputState.getSizeStats());
1053
+ }
1054
+ }
1055
+ }
1056
+ let buildError;
1057
+ bundleConfig._callbacks = {
1058
+ onBuildStart,
1059
+ onBuildEnd
1060
+ };
1061
+ spinner.start();
966
1062
  try {
967
- await bunchee.bundle(cliEntry, bundleConfig);
1063
+ await index_js.bundle(cliEntry, bundleConfig);
968
1064
  } catch (err) {
1065
+ stopSpinner();
969
1066
  if (err.name === 'NOT_EXISTED') {
1067
+ buildError = {
1068
+ digest: 'bunchee:not-existed',
1069
+ error: err
1070
+ };
1071
+ }
1072
+ if ((buildError == null ? void 0 : buildError.digest) === 'bunchee:not-existed') {
970
1073
  help();
971
- return exit(err);
1074
+ } else {
1075
+ if (watch) {
1076
+ logError(err);
1077
+ } else {
1078
+ throw err;
1079
+ }
972
1080
  }
973
- throw err;
974
1081
  }
975
1082
  // watching mode
976
1083
  if (watch) {
@@ -994,4 +1101,46 @@ async function main() {
994
1101
  }
995
1102
  await run(params);
996
1103
  }
1104
+ function logWatcherBuildTime(result) {
1105
+ let watcherCounter = 0;
1106
+ let startTime = 0;
1107
+ result.map((watcher)=>{
1108
+ function start() {
1109
+ if (watcherCounter === 0) startTime = perf_hooks.performance.now();
1110
+ watcherCounter++;
1111
+ }
1112
+ function end() {
1113
+ watcherCounter--;
1114
+ if (watcherCounter === 0) {
1115
+ logger.info(`Build in ${(perf_hooks.performance.now() - startTime).toFixed(2)}ms`);
1116
+ }
1117
+ }
1118
+ watcher.on('event', (event)=>{
1119
+ switch(event.code){
1120
+ case 'ERROR':
1121
+ {
1122
+ logError(event.error);
1123
+ break;
1124
+ }
1125
+ case 'START':
1126
+ {
1127
+ start();
1128
+ break;
1129
+ }
1130
+ case 'END':
1131
+ {
1132
+ end();
1133
+ break;
1134
+ }
1135
+ }
1136
+ });
1137
+ });
1138
+ }
1139
+ function logError(error) {
1140
+ if (!error) return;
1141
+ // logging source code in format
1142
+ if (error.frame) {
1143
+ process.stderr.write(error.frame + '\n');
1144
+ }
1145
+ }
997
1146
  main().catch(exit);
package/dist/index.d.ts CHANGED
@@ -19,6 +19,10 @@ type BundleConfig = {
19
19
  pkg?: PackageMetadata;
20
20
  clean?: boolean;
21
21
  tsconfig?: string;
22
+ _callbacks?: {
23
+ onBuildStart?: (state: any) => void;
24
+ onBuildEnd?: (assetJobs: any) => void;
25
+ };
22
26
  };
23
27
  type PackageMetadata = {
24
28
  name?: string;
@@ -34,6 +38,6 @@ type PackageMetadata = {
34
38
  typings?: string;
35
39
  };
36
40
 
37
- declare function bundle(cliEntryPath: string, { cwd: _cwd, ...options }?: BundleConfig): Promise<any>;
41
+ declare function bundle(cliEntryPath: string, { cwd: _cwd, ...options }?: BundleConfig): Promise<void>;
38
42
 
39
43
  export { type BundleConfig, bundle };