bunchee 5.2.0-beta.0 → 5.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.
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',
@@ -130,13 +133,6 @@ const logger = {
130
133
  console.log(color('green')('✓'), ...arg);
131
134
  }
132
135
  };
133
- function paint(prefix, prefixColor, ...arg) {
134
- if (pc.isColorSupported) {
135
- console.log(pc[prefixColor](prefix), ...arg);
136
- } else {
137
- console.log(prefix, ...arg);
138
- }
139
- }
140
136
 
141
137
  function exit(err) {
142
138
  logger.error(err);
@@ -474,7 +470,7 @@ function lint$1(pkg) {
474
470
  }
475
471
  }
476
472
 
477
- var version = "5.2.0-beta.0";
473
+ var version = "5.2.0";
478
474
 
479
475
  function relativify(path) {
480
476
  return path.startsWith('.') ? path : `./${path}`;
@@ -847,6 +843,64 @@ async function prepare(cwd) {
847
843
  logger.info('Configured `exports` in package.json');
848
844
  }
849
845
 
846
+ function isTypeFile(filename) {
847
+ return filename.endsWith('.d.ts') || filename.endsWith('.d.mts') || filename.endsWith('.d.cts');
848
+ }
849
+ function normalizeExportName(exportName) {
850
+ const isBinary = isBinExportPath(exportName);
851
+ let result = exportName;
852
+ if (isBinary) {
853
+ result = (exportName.replace(new RegExp(`^\\${BINARY_TAG}\\/?`), '') || '.') + ' (bin)';
854
+ } else {
855
+ const normalizedExportPath = normalizeExportPath(exportName);
856
+ const specialConditionName = getSpecialExportTypeFromComposedExportPath(exportName);
857
+ result = normalizedExportPath + (specialConditionName !== 'default' ? ` (${specialConditionName})` : '');
858
+ }
859
+ return result;
860
+ }
861
+ function logOutputState(stats) {
862
+ if (stats.size === 0) {
863
+ logger.warn('No build info can be logged');
864
+ return;
865
+ }
866
+ const allFileNameLengths = Array.from(stats.values()).flat(1).map(([filename])=>filename.length);
867
+ const maxFilenameLength = Math.max(...allFileNameLengths);
868
+ const statsArray = [
869
+ ...stats.entries()
870
+ ].sort(([a], [b])=>{
871
+ const comp = normalizeExportPath(a).length - normalizeExportPath(b).length;
872
+ return comp === 0 ? a.localeCompare(b) : comp;
873
+ });
874
+ const maxLengthOfExportName = Math.max(...statsArray.map(([exportName])=>normalizeExportName(exportName).length));
875
+ 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'));
876
+ statsArray.forEach(([exportName, filesList])=>{
877
+ // sort by file type, first js files then types, js/mjs/cjs are prioritized than .d.ts/.d.mts/.d.cts
878
+ filesList.sort(([a], [b])=>{
879
+ const aIsType = isTypeFile(a);
880
+ const bIsType = isTypeFile(b);
881
+ if (aIsType && bIsType) {
882
+ return 0;
883
+ }
884
+ if (aIsType) {
885
+ return 1;
886
+ }
887
+ if (bIsType) {
888
+ return -1;
889
+ }
890
+ return 0;
891
+ }).forEach((item, index)=>{
892
+ const [filename, , size] = item;
893
+ const normalizedExportName = normalizeExportName(exportName);
894
+ const prefix = index === 0 ? normalizedExportName : ' '.repeat(normalizedExportName.length);
895
+ const filenamePadding = ' '.repeat(Math.max(maxLengthOfExportName, 'Exports'.length) - normalizedExportName.length);
896
+ const isType = isTypeFile(filename);
897
+ const sizePadding = ' '.repeat(Math.max(maxFilenameLength, 'File'.length) - filename.length);
898
+ const prettiedSize = prettyBytes__default.default(size);
899
+ console.log(prefix, filenamePadding, `${pc[isType ? 'dim' : 'bold'](filename)}`, sizePadding, prettiedSize);
900
+ });
901
+ });
902
+ }
903
+
850
904
  const helpMessage = `
851
905
  Usage: bunchee [options]
852
906
 
@@ -963,23 +1017,76 @@ async function run(args) {
963
1017
  const cliEntry = source ? path__default.default.resolve(cwd, source) : '';
964
1018
  // lint package
965
1019
  await lint(cwd);
1020
+ const { default: ora } = await import('ora');
1021
+ const spinner = ora({
1022
+ text: 'Building...\n\n',
1023
+ color: 'green'
1024
+ });
1025
+ function stopSpinner(text) {
1026
+ if (spinner.isSpinning) {
1027
+ spinner.clear();
1028
+ console.log();
1029
+ if (text) {
1030
+ spinner.stopAndPersist({
1031
+ symbol: '✔',
1032
+ text
1033
+ });
1034
+ } else {
1035
+ spinner.stop();
1036
+ }
1037
+ }
1038
+ }
1039
+ let initialBuildContext;
1040
+ function onBuildStart(buildContext) {
1041
+ initialBuildContext = buildContext;
1042
+ spinner.start();
1043
+ }
1044
+ function onBuildEnd(assetJobs) {
1045
+ // Stop spinner before logging output files and sizes on build end
1046
+ if (watch) {
1047
+ stopSpinner('');
1048
+ logWatcherBuildTime(assetJobs);
1049
+ } else {
1050
+ if (assetJobs.length === 0) {
1051
+ 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');
1052
+ }
1053
+ const outputState = initialBuildContext == null ? void 0 : initialBuildContext.pluginContext.outputState;
1054
+ if (outputState) {
1055
+ logOutputState(outputState.getSizeStats());
1056
+ }
1057
+ }
1058
+ }
1059
+ let buildError;
1060
+ bundleConfig._callbacks = {
1061
+ onBuildStart,
1062
+ onBuildEnd
1063
+ };
1064
+ if (watch) {
1065
+ logger.log(`Watching project ${cwd}...`);
1066
+ }
966
1067
  try {
967
- await bunchee.bundle(cliEntry, bundleConfig);
1068
+ await index_js.bundle(cliEntry, bundleConfig);
968
1069
  } catch (err) {
969
1070
  if (err.name === 'NOT_EXISTED') {
1071
+ buildError = {
1072
+ digest: 'bunchee:not-existed',
1073
+ error: err
1074
+ };
1075
+ }
1076
+ if ((buildError == null ? void 0 : buildError.digest) === 'bunchee:not-existed') {
970
1077
  help();
971
- return exit(err);
1078
+ } else {
1079
+ if (watch) {
1080
+ logError(err);
1081
+ } else {
1082
+ throw err;
1083
+ }
972
1084
  }
973
- throw err;
974
1085
  }
975
1086
  // watching mode
976
- if (watch) {
977
- logger.log(`Watching project ${cwd}...`);
978
- return;
1087
+ if (!watch) {
1088
+ stopSpinner(`bunchee ${version} build completed`);
979
1089
  }
980
- // build mode
981
- logger.log();
982
- paint('✓', 'green', `bunchee ${version} build completed`);
983
1090
  }
984
1091
  async function main() {
985
1092
  let params, error;
@@ -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(`Built 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 };