bunchee 3.3.1 → 3.3.3

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 +170 -144
  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.3";
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
  }
@@ -144,10 +268,14 @@ function findExport(name, value, paths, packageType) {
144
268
  if (typeof exportsCondition === 'string') {
145
269
  paths['.'] = constructFullExportCondition(exportsCondition, packageType);
146
270
  } else if (typeof exportsCondition === 'object') {
147
- Object.keys(exportsCondition).forEach((key)=>{
148
- const value = exportsCondition[key];
149
- findExport(key, value, paths, packageType);
150
- });
271
+ if (isExportLike(exportsCondition)) {
272
+ paths['.'] = constructFullExportCondition(exportsCondition, packageType);
273
+ } else {
274
+ Object.keys(exportsCondition).forEach((key)=>{
275
+ const value = exportsCondition[key];
276
+ findExport(key, value, paths, packageType);
277
+ });
278
+ }
151
279
  }
152
280
  return paths;
153
281
  }
@@ -276,125 +404,11 @@ function getExportConditionDist(pkg, parsedExportCondition, cwd) {
276
404
  }
277
405
  return dist;
278
406
  }
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);
407
+ function getTypeFilePath(entryFilePath, exportCondition, cwd) {
408
+ const name = filenameWithoutExtension(entryFilePath);
409
+ const firstDistPath = exportCondition ? Object.values(exportCondition.export)[0] : undefined;
410
+ const exportName = (exportCondition == null ? void 0 : exportCondition.name) || 'index';
411
+ return entryFilePath ? name + '.d.ts' : path.resolve(firstDistPath ? path.dirname(firstDistPath) : path.join(cwd, 'dist'), (exportName === '.' ? 'index' : exportName) + '.d.ts');
398
412
  }
399
413
 
400
414
  function asyncGeneratorStep$2(gen, resolve, reject, _next, _throw, key, arg) {
@@ -466,7 +480,7 @@ function getBuildEnv(envs) {
466
480
  }, {});
467
481
  return envVars;
468
482
  }
469
- function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerOptions }, dtsOnly) {
483
+ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerOptions }, dts) {
470
484
  var _options_external;
471
485
  const externals = options.noExternal ? [] : [
472
486
  pkg.peerDependencies,
@@ -482,7 +496,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
482
496
  shebang__default.default(),
483
497
  sizePlugin
484
498
  ];
485
- const plugins = (dtsOnly ? [
499
+ const plugins = (dts ? [
486
500
  ...commonPlugins,
487
501
  useTypescript && require('rollup-plugin-dts').default({
488
502
  compilerOptions: _extends$1({}, tsCompilerOptions, {
@@ -559,7 +573,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
559
573
  onwarn (warning, warn) {
560
574
  const code = warning.code || '';
561
575
  // Some may not have types, like CLI binary
562
- if (dtsOnly && code === 'EMPTY_BUNDLE') return;
576
+ if (dts && code === 'EMPTY_BUNDLE') return;
563
577
  if ([
564
578
  'MIXED_EXPORTS',
565
579
  'PREFER_NAMED_EXPORTS',
@@ -586,17 +600,16 @@ function hasEsmExport(exportPaths, tsCompilerOptions) {
586
600
  }
587
601
  return Boolean(hasEsm || (tsCompilerOptions == null ? void 0 : tsCompilerOptions.esModuleInterop));
588
602
  }
589
- function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { tsCompilerOptions }, dtsOnly) {
603
+ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { tsCompilerOptions }, dts) {
590
604
  const { format } = options;
591
605
  // Add esm mark and interop helper if esm export is detected
592
606
  const useEsModuleMark = hasEsmExport(exportPaths, tsCompilerOptions);
593
607
  const typings = getTypings(pkg);
594
608
  const file = options.file && path.resolve(cwd, options.file);
595
609
  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;
610
+ const name = filenameWithoutExtension(file);
598
611
  // 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);
612
+ 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
613
  // If there's dts file, use `output.file`
601
614
  const dtsPathConfig = dtsFile ? {
602
615
  file: dtsFile
@@ -605,7 +618,7 @@ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { t
605
618
  };
606
619
  return _extends$1({
607
620
  name: pkg.name || name
608
- }, dtsOnly ? dtsPathConfig : {
621
+ }, dts ? dtsPathConfig : {
609
622
  file: file
610
623
  }, {
611
624
  format,
@@ -618,44 +631,46 @@ function buildOutputConfigs(pkg, exportPaths, options, exportCondition, cwd, { t
618
631
  });
619
632
  }
620
633
  // build configs for each entry from package exports
621
- function buildEntryConfig(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dtsOnly) {
634
+ function buildEntryConfig(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dts) {
622
635
  return _buildEntryConfig.apply(this, arguments);
623
636
  }
624
637
  function _buildEntryConfig() {
625
- _buildEntryConfig = _async_to_generator$2(function*(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dtsOnly) {
638
+ _buildEntryConfig = _async_to_generator$2(function*(pkg, entryPath, exportPaths, bundleConfig, cwd, tsOptions, dts) {
626
639
  const configs = Object.keys(exportPaths).map(/*#__PURE__*/ _async_to_generator$2(function*(entryExport) {
627
640
  // TODO: improve the source detection
628
641
  const source = entryPath || (yield getSourcePathFromExportPath(cwd, entryExport));
629
642
  if (!source) return undefined;
643
+ // For dts, only build types filed
644
+ if (dts && !exportPaths[entryExport]['types']) return undefined;
630
645
  const exportCondition = {
631
646
  source,
632
647
  name: entryExport,
633
648
  export: exportPaths[entryExport]
634
649
  };
635
650
  const entry = resolveSourceFile(cwd, source);
636
- const rollupConfig = buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dtsOnly);
651
+ const rollupConfig = buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dts);
637
652
  return rollupConfig;
638
653
  }));
639
654
  return (yield Promise.all(configs)).filter((n)=>Boolean(n));
640
655
  });
641
656
  return _buildEntryConfig.apply(this, arguments);
642
657
  }
643
- function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dtsOnly) {
658
+ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd, tsOptions, dts) {
644
659
  const { file } = bundleConfig;
645
660
  const useTypescript = Boolean(tsOptions.tsConfigPath);
646
661
  const options = _extends$1({}, bundleConfig, {
647
662
  useTypescript
648
663
  });
649
- const inputOptions = buildInputConfig(entry, pkg, options, cwd, tsOptions, dtsOnly);
664
+ const inputOptions = buildInputConfig(entry, pkg, options, cwd, tsOptions, dts);
650
665
  const outputExports = getExportConditionDist(pkg, exportCondition, cwd);
651
666
  let outputConfigs = [];
652
667
  // Generate dts job - single config
653
- if (dtsOnly) {
668
+ if (dts) {
654
669
  outputConfigs = [
655
670
  buildOutputConfigs(pkg, exportPaths, _extends$1({}, bundleConfig, {
656
671
  format: 'es',
657
672
  useTypescript
658
- }), exportCondition, cwd, tsOptions, dtsOnly)
673
+ }), exportCondition, cwd, tsOptions, dts)
659
674
  ];
660
675
  } else {
661
676
  // multi outputs with specified format
@@ -664,7 +679,7 @@ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd
664
679
  file: exportDist.file,
665
680
  format: exportDist.format,
666
681
  useTypescript
667
- }), exportCondition, cwd, tsOptions, dtsOnly);
682
+ }), exportCondition, cwd, tsOptions, dts);
668
683
  });
669
684
  // CLI output option is always prioritized
670
685
  if (file) {
@@ -675,7 +690,7 @@ function buildConfig(entry, pkg, exportPaths, bundleConfig, exportCondition, cwd
675
690
  file,
676
691
  format: bundleConfig.format || fallbackFormat,
677
692
  useTypescript
678
- }), exportCondition, cwd, tsOptions, dtsOnly)
693
+ }), exportCondition, cwd, tsOptions, dts)
679
694
  ];
680
695
  }
681
696
  }
@@ -849,6 +864,8 @@ function _bundle() {
849
864
  // const exportPathsLength = Object.keys(exportPaths).length
850
865
  const isMultiEntries = hasMultiEntryExport(exportPaths) // exportPathsLength > 1
851
866
  ;
867
+ // has `types` field in package.json or has `types` exports in any export condition for multi-entries
868
+ const hasTypings = Object.values(exportPaths).some((condition)=>condition.hasOwnProperty('types'));
852
869
  const tsConfig = yield resolveTsConfig(cwd);
853
870
  const hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath);
854
871
  const defaultTsOptions = {
@@ -862,11 +879,22 @@ function _bundle() {
862
879
  entryPath = entryPath || (yield getSourcePathFromExportPath(cwd, '.')) || '';
863
880
  }
864
881
  if (entryPath) {
882
+ let mainEntryPath;
883
+ let typesEntryPath;
865
884
  // with -o option
866
885
  if (options.file) {
867
- exportPaths['.'] = constructDefaultExportCondition(options.file, packageType);
886
+ mainEntryPath = options.file;
868
887
  } else if (exportKeys.length === 0) {
869
- exportPaths['.'] = constructDefaultExportCondition('dist/index.js', packageType);
888
+ mainEntryPath = path.resolve(cwd, 'dist/index.js');
889
+ }
890
+ if (mainEntryPath) {
891
+ if (options.dts) {
892
+ typesEntryPath = getTypeFilePath(mainEntryPath, undefined, cwd);
893
+ }
894
+ exportPaths['.'] = constructDefaultExportCondition({
895
+ main: mainEntryPath,
896
+ types: typesEntryPath
897
+ }, packageType);
870
898
  }
871
899
  }
872
900
  const bundleOrWatch = (rollupConfig)=>{
@@ -888,8 +916,6 @@ function _bundle() {
888
916
  err.name = 'NOT_EXISTED';
889
917
  return Promise.reject(err);
890
918
  }
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
919
  // Enable types generation if it's types field specified in package.json
894
920
  if (hasTypings) {
895
921
  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.3",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": {
6
6
  "bunchee": "./dist/cli.js"