bunchee 3.3.1 → 3.3.2

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.
Files changed (3) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/index.js +162 -140
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -97,7 +97,7 @@ function _fileExists() {
97
97
  return _fileExists.apply(this, arguments);
98
98
  }
99
99
 
100
- var version = "3.3.1";
100
+ var version = "3.3.2";
101
101
 
102
102
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
103
103
  try {
package/dist/index.js CHANGED
@@ -61,6 +61,130 @@ function chunkSizeCollector() {
61
61
  };
62
62
  }
63
63
 
64
+ function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
65
+ try {
66
+ var info = gen[key](arg);
67
+ var value = info.value;
68
+ } catch (error) {
69
+ reject(error);
70
+ return;
71
+ }
72
+ if (info.done) {
73
+ resolve(value);
74
+ } else {
75
+ Promise.resolve(value).then(_next, _throw);
76
+ }
77
+ }
78
+ function _async_to_generator$3(fn) {
79
+ return function() {
80
+ var self = this, args = arguments;
81
+ return new Promise(function(resolve, reject) {
82
+ var gen = fn.apply(self, args);
83
+ function _next(value) {
84
+ asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
85
+ }
86
+ function _throw(err) {
87
+ asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
88
+ }
89
+ _next(undefined);
90
+ });
91
+ };
92
+ }
93
+ function exit(err) {
94
+ logger.error(err);
95
+ process.exit(1);
96
+ }
97
+ function getPackageMeta(cwd) {
98
+ return _getPackageMeta.apply(this, arguments);
99
+ }
100
+ function _getPackageMeta() {
101
+ _getPackageMeta = _async_to_generator$3(function*(cwd) {
102
+ const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
103
+ let targetPackageJson = {};
104
+ try {
105
+ targetPackageJson = JSON.parse((yield fs__default.default.readFile(pkgFilePath, {
106
+ encoding: 'utf-8'
107
+ })));
108
+ } catch (_) {}
109
+ return targetPackageJson;
110
+ });
111
+ return _getPackageMeta.apply(this, arguments);
112
+ }
113
+ const logger = {
114
+ log (arg) {
115
+ console.log(arg);
116
+ },
117
+ warn (arg) {
118
+ console.log('\x1b[33m' + arg + '\x1b[0m');
119
+ },
120
+ error (arg) {
121
+ console.error('\x1b[31m' + arg + '\x1b[0m');
122
+ }
123
+ };
124
+ function fileExists(filePath) {
125
+ return _fileExists.apply(this, arguments);
126
+ }
127
+ function _fileExists() {
128
+ _fileExists = _async_to_generator$3(function*(filePath) {
129
+ try {
130
+ yield fs__default.default.access(filePath);
131
+ return true;
132
+ } catch (err) {
133
+ if (err.code === 'ENOENT') {
134
+ return false;
135
+ }
136
+ throw err;
137
+ }
138
+ });
139
+ return _fileExists.apply(this, arguments);
140
+ }
141
+ // . -> pkg name
142
+ // ./lite -> <pkg name>/lite
143
+ function getExportPath(pkg, cwd, exportName) {
144
+ const name = pkg.name || path__default.default.basename(cwd);
145
+ if (exportName === '.' || !exportName) return name;
146
+ return path__default.default.join(name, exportName);
147
+ }
148
+ const isNotNull = (n)=>Boolean(n);
149
+ const SRC = 'src' // resolve from src/ directory
150
+ ;
151
+ function resolveSourceFile(cwd, filename) {
152
+ return path__default.default.resolve(cwd, SRC, filename);
153
+ }
154
+ // Map '.' -> './index.[ext]'
155
+ // Map './lite' -> './lite.[ext]'
156
+ // Return undefined if no match or if it's package.json exports
157
+ function getSourcePathFromExportPath(cwd, exportPath) {
158
+ return _getSourcePathFromExportPath.apply(this, arguments);
159
+ }
160
+ function _getSourcePathFromExportPath() {
161
+ _getSourcePathFromExportPath = _async_to_generator$3(function*(cwd, exportPath) {
162
+ const exts = [
163
+ 'js',
164
+ 'cjs',
165
+ 'mjs',
166
+ 'jsx',
167
+ 'ts',
168
+ 'tsx'
169
+ ];
170
+ for (const ext of exts){
171
+ // ignore package.json
172
+ if (exportPath.endsWith('package.json')) return;
173
+ if (exportPath === '.') exportPath = './index';
174
+ const filename = resolveSourceFile(cwd, `${exportPath}.${ext}`);
175
+ if (yield fileExists(filename)) {
176
+ return filename;
177
+ }
178
+ }
179
+ return;
180
+ });
181
+ return _getSourcePathFromExportPath.apply(this, arguments);
182
+ }
183
+ // Unlike path.basename, forcedly removing extension
184
+ function filenameWithoutExtension(file) {
185
+ return file ? file.replace(new RegExp(`${path__default.default.extname(file)}$`), '') : undefined;
186
+ }
187
+
64
188
  function getTypings(pkg) {
65
189
  return pkg.types || pkg.typings;
66
190
  }
@@ -276,125 +400,11 @@ function getExportConditionDist(pkg, parsedExportCondition, cwd) {
276
400
  }
277
401
  return dist;
278
402
  }
279
-
280
- function asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, key, arg) {
281
- try {
282
- var info = gen[key](arg);
283
- var value = info.value;
284
- } catch (error) {
285
- reject(error);
286
- return;
287
- }
288
- if (info.done) {
289
- resolve(value);
290
- } else {
291
- Promise.resolve(value).then(_next, _throw);
292
- }
293
- }
294
- function _async_to_generator$3(fn) {
295
- return function() {
296
- var self = this, args = arguments;
297
- return new Promise(function(resolve, reject) {
298
- var gen = fn.apply(self, args);
299
- function _next(value) {
300
- asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "next", value);
301
- }
302
- function _throw(err) {
303
- asyncGeneratorStep$3(gen, resolve, reject, _next, _throw, "throw", err);
304
- }
305
- _next(undefined);
306
- });
307
- };
308
- }
309
- function exit(err) {
310
- logger.error(err);
311
- process.exit(1);
312
- }
313
- function getPackageMeta(cwd) {
314
- return _getPackageMeta.apply(this, arguments);
315
- }
316
- function _getPackageMeta() {
317
- _getPackageMeta = _async_to_generator$3(function*(cwd) {
318
- const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
319
- let targetPackageJson = {};
320
- try {
321
- targetPackageJson = JSON.parse((yield fs__default.default.readFile(pkgFilePath, {
322
- encoding: 'utf-8'
323
- })));
324
- } catch (_) {}
325
- return targetPackageJson;
326
- });
327
- return _getPackageMeta.apply(this, arguments);
328
- }
329
- const logger = {
330
- log (arg) {
331
- console.log(arg);
332
- },
333
- warn (arg) {
334
- console.log('\x1b[33m' + arg + '\x1b[0m');
335
- },
336
- error (arg) {
337
- console.error('\x1b[31m' + arg + '\x1b[0m');
338
- }
339
- };
340
- function fileExists(filePath) {
341
- return _fileExists.apply(this, arguments);
342
- }
343
- function _fileExists() {
344
- _fileExists = _async_to_generator$3(function*(filePath) {
345
- try {
346
- yield fs__default.default.access(filePath);
347
- return true;
348
- } catch (err) {
349
- if (err.code === 'ENOENT') {
350
- return false;
351
- }
352
- throw err;
353
- }
354
- });
355
- return _fileExists.apply(this, arguments);
356
- }
357
- // . -> pkg name
358
- // ./lite -> <pkg name>/lite
359
- function getExportPath(pkg, cwd, exportName) {
360
- const name = pkg.name || path__default.default.basename(cwd);
361
- if (exportName === '.' || !exportName) return name;
362
- return path__default.default.join(name, exportName);
363
- }
364
- const isNotNull = (n)=>Boolean(n);
365
- const SRC = 'src' // resolve from src/ directory
366
- ;
367
- function resolveSourceFile(cwd, filename) {
368
- return path__default.default.resolve(cwd, SRC, filename);
369
- }
370
- // Map '.' -> './index.[ext]'
371
- // Map './lite' -> './lite.[ext]'
372
- // Return undefined if no match or if it's package.json exports
373
- function getSourcePathFromExportPath(cwd, exportPath) {
374
- return _getSourcePathFromExportPath.apply(this, arguments);
375
- }
376
- function _getSourcePathFromExportPath() {
377
- _getSourcePathFromExportPath = _async_to_generator$3(function*(cwd, exportPath) {
378
- const exts = [
379
- 'js',
380
- 'cjs',
381
- 'mjs',
382
- 'jsx',
383
- 'ts',
384
- 'tsx'
385
- ];
386
- for (const ext of exts){
387
- // ignore package.json
388
- if (exportPath.endsWith('package.json')) return;
389
- if (exportPath === '.') exportPath = './index';
390
- const filename = resolveSourceFile(cwd, `${exportPath}.${ext}`);
391
- if (yield fileExists(filename)) {
392
- return filename;
393
- }
394
- }
395
- return;
396
- });
397
- return _getSourcePathFromExportPath.apply(this, arguments);
403
+ function getTypeFilePath(entryFilePath, exportCondition, cwd) {
404
+ const name = filenameWithoutExtension(entryFilePath);
405
+ const firstDistPath = exportCondition ? Object.values(exportCondition.export)[0] : undefined;
406
+ const exportName = (exportCondition == null ? void 0 : exportCondition.name) || 'index';
407
+ return entryFilePath ? name + '.d.ts' : path.resolve(firstDistPath ? path.dirname(firstDistPath) : path.join(cwd, 'dist'), (exportName === '.' ? 'index' : exportName) + '.d.ts');
398
408
  }
399
409
 
400
410
  function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
@@ -466,7 +476,7 @@ function getBuildEnv(envs) {
466
476
  }, {});
467
477
  return envVars;
468
478
  }
469
- function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerOptions }, dtsOnly) {
479
+ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerOptions }, dts) {
470
480
  var _options_external;
471
481
  const externals = options.noExternal ? [] : [
472
482
  pkg.peerDependencies,
@@ -482,7 +492,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
482
492
  shebang__default.default(),
483
493
  sizePlugin
484
494
  ];
485
- const plugins = (dtsOnly ? [
495
+ const plugins = (dts ? [
486
496
  ...commonPlugins,
487
497
  useTypescript && require('rollup-plugin-dts').default({
488
498
  compilerOptions: _extends$1({}, tsCompilerOptions, {
@@ -559,7 +569,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
559
569
  onwarn (warning, warn) {
560
570
  const code = warning.code || '';
561
571
  // Some may not have types, like CLI binary
562
- if (dtsOnly && code === 'EMPTY_BUNDLE') return;
572
+ if (dts && code === 'EMPTY_BUNDLE') return;
563
573
  if ([
564
574
  'MIXED_EXPORTS',
565
575
  'PREFER_NAMED_EXPORTS',
@@ -586,17 +596,16 @@ function hasEsmExport(exportPaths, tsCompilerOptions) {
586
596
  }
587
597
  return Boolean(hasEsm || (tsCompilerOptions == null ? void 0 : tsCompilerOptions.esModuleInterop));
588
598
  }
589
- function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { tsCompilerOptions }, dtsOnly) {
599
+ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { tsCompilerOptions }, dts) {
590
600
  const { format } = options;
591
601
  // Add esm mark and interop helper if esm export is detected
592
602
  const useEsModuleMark = hasEsmExport(exportPaths, tsCompilerOptions);
593
603
  const typings = getTypings(pkg);
594
604
  const file = options.file && path.resolve(cwd, options.file);
595
605
  const dtsDir = typings ? path.dirname(path.resolve(cwd, typings)) : path.resolve(cwd, 'dist');
596
- // file base name without extension
597
- const name = file ? file.replace(new RegExp(`${path.extname(file)}$`), '') : undefined;
606
+ const name = filenameWithoutExtension(file);
598
607
  // TODO: simplify dts file name detection
599
- const dtsFile = exportCondition.export['types'] ? path.resolve(cwd, exportCondition.export['types']) : file ? name + '.d.ts' : (exportCondition == null ? void 0 : exportCondition.name) ? path.resolve(dtsDir, (exportCondition.name === '.' ? 'index' : exportCondition.name) + '.d.ts') : typings && path.resolve(cwd, typings);
608
+ const dtsFile = exportCondition.export['types'] ? path.resolve(cwd, exportCondition.export['types']) : file ? name + '.d.ts' : path.resolve(dtsDir, (exportCondition.name === '.' ? 'index' : exportCondition.name) + '.d.ts');
600
609
  // If there's dts file, use `output.file`
601
610
  const dtsPathConfig = dtsFile ? {
602
611
  file: dtsFile
@@ -605,7 +614,7 @@ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { t
605
614
  };
606
615
  return _extends$1({
607
616
  name: pkg.name || name
608
- }, dtsOnly ? dtsPathConfig : {
617
+ }, dts ? dtsPathConfig : {
609
618
  file: file
610
619
  }, {
611
620
  format,
@@ -618,44 +627,46 @@ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { t
618
627
  });
619
628
  }
620
629
  // build configs for each entry from package exports
621
- function buildEntryConfig(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dtsOnly) {
630
+ function buildEntryConfig(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dts) {
622
631
  return _buildEntryConfig.apply(this, arguments);
623
632
  }
624
633
  function _buildEntryConfig() {
625
- _buildEntryConfig = _async_to_generator$2(function*(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dtsOnly) {
634
+ _buildEntryConfig = _async_to_generator$2(function*(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dts) {
626
635
  const configs = Object.keys(exportPaths).map(/*#__PURE__*/ _async_to_generator$2(function*(entryExport) {
627
636
  // TODO: improve the source detection
628
637
  const source = entryPath || (yield getSourcePathFromExportPath(cwd, entryExport));
629
638
  if (!source) return undefined;
639
+ // For dts, only build types filed
640
+ if (dts && !exportPaths[entryExport]['types']) return undefined;
630
641
  const exportCondition = {
631
642
  source,
632
643
  name: entryExport,
633
644
  export: exportPaths[entryExport]
634
645
  };
635
646
  const entry = resolveSourceFile(cwd, source);
636
- const rollupConfig = buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dtsOnly);
647
+ const rollupConfig = buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dts);
637
648
  return rollupConfig;
638
649
  }));
639
650
  return (yield Promise.all(configs)).filter((n)=>Boolean(n));
640
651
  });
641
652
  return _buildEntryConfig.apply(this, arguments);
642
653
  }
643
- function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dtsOnly) {
654
+ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dts) {
644
655
  const { file } = bundleConfig;
645
656
  const useTypescript = Boolean(tsOptions.tsConfigPath);
646
657
  const options = _extends$1({}, bundleConfig, {
647
658
  useTypescript
648
659
  });
649
- const inputOptions = buildInputConfig(entry, pkg, options, cwd, tsOptions, dtsOnly);
660
+ const inputOptions = buildInputConfig(entry, pkg, options, cwd, tsOptions, dts);
650
661
  const outputExports = getExportConditionDist(pkg, exportCondition, cwd);
651
662
  let outputConfigs = [];
652
663
  // Generate dts job - single config
653
- if (dtsOnly) {
664
+ if (dts) {
654
665
  outputConfigs = [
655
666
  buildOutputConfigs(pkg, exportPaths, _extends$1({}, bundleConfig, {
656
667
  format: 'es',
657
668
  useTypescript
658
- }), exportCondition, cwd, tsOptions, dtsOnly)
669
+ }), exportCondition, cwd, tsOptions, dts)
659
670
  ];
660
671
  } else {
661
672
  // multi outputs with specified format
@@ -664,7 +675,7 @@ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd
664
675
  file: exportDist.file,
665
676
  format: exportDist.format,
666
677
  useTypescript
667
- }), exportCondition, cwd, tsOptions, dtsOnly);
678
+ }), exportCondition, cwd, tsOptions, dts);
668
679
  });
669
680
  // CLI output option is always prioritized
670
681
  if (file) {
@@ -675,7 +686,7 @@ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd
675
686
  file,
676
687
  format: bundleConfig.format || fallbackFormat,
677
688
  useTypescript
678
- }), exportCondition, cwd, tsOptions, dtsOnly)
689
+ }), exportCondition, cwd, tsOptions, dts)
679
690
  ];
680
691
  }
681
692
  }
@@ -849,6 +860,8 @@ function _bundle() {
849
860
  // const exportPathsLength = Object.keys(exportPaths).length
850
861
  const isMultiEntries = hasMultiEntryExport(exportPaths) // exportPathsLength > 1
851
862
  ;
863
+ // has `types` field in package.json or has `types` exports in any export condition for multi-entries
864
+ const hasTypings = Object.values(exportPaths).some((condition)=>condition.hasOwnProperty('types'));
852
865
  const tsConfig = yield resolveTsConfig(cwd);
853
866
  const hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath);
854
867
  const defaultTsOptions = {
@@ -862,11 +875,22 @@ function _bundle() {
862
875
  entryPath = entryPath || (yield getSourcePathFromExportPath(cwd, '.')) || '';
863
876
  }
864
877
  if (entryPath) {
878
+ let mainEntryPath;
879
+ let typesEntryPath;
865
880
  // with -o option
866
881
  if (options.file) {
867
- exportPaths['.'] = constructDefaultExportCondition(options.file, packageType);
882
+ mainEntryPath = options.file;
868
883
  } else if (exportKeys.length === 0) {
869
- exportPaths['.'] = constructDefaultExportCondition('dist/index.js', packageType);
884
+ mainEntryPath = path.resolve(cwd, 'dist/index.js');
885
+ }
886
+ if (mainEntryPath) {
887
+ if (options.dts) {
888
+ typesEntryPath = getTypeFilePath(mainEntryPath, undefined, cwd);
889
+ }
890
+ exportPaths['.'] = constructDefaultExportCondition({
891
+ main: mainEntryPath,
892
+ types: typesEntryPath
893
+ }, packageType);
870
894
  }
871
895
  }
872
896
  const bundleOrWatch = (rollupConfig)=>{
@@ -888,8 +912,6 @@ function _bundle() {
888
912
  err.name = 'NOT_EXISTED';
889
913
  return Promise.reject(err);
890
914
  }
891
- // has `types` field in package.json or has `types` exports in any export condition for multi-entries
892
- const hasTypings = Object.values(exportPaths).some((condition)=>condition.hasOwnProperty('types'));
893
915
  // Enable types generation if it's types field specified in package.json
894
916
  if (hasTypings) {
895
917
  options.dts = hasTypings;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "3.3.1",
3
+ "version": "3.3.2",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": {
6
6
  "bunchee": "./dist/cli.js"