bunchee 3.1.0 → 3.1.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/README.md CHANGED
@@ -39,9 +39,9 @@ You can use the `exports` field to support different conditions and leverage the
39
39
  ```json
40
40
  {
41
41
  "exports": {
42
- "require": "dist/index.cjs",
42
+ "module": "dist/index.esm.js",
43
43
  "import": "dist/index.mjs",
44
- "module": "dist/index.esm.js"
44
+ "require": "dist/index.cjs"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "bunchee"
package/dist/cli.js CHANGED
@@ -3,6 +3,12 @@ var path = require('path');
3
3
  var arg = require('arg');
4
4
  var fs = require('fs/promises');
5
5
 
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var path__default = /*#__PURE__*/_interopDefault(path);
9
+ var arg__default = /*#__PURE__*/_interopDefault(arg);
10
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
11
+
6
12
  function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
7
13
  try {
8
14
  var info = gen[key](arg);
@@ -42,7 +48,7 @@ function hasPackageJson(cwd) {
42
48
  }
43
49
  function _hasPackageJson() {
44
50
  _hasPackageJson = _async_to_generator$1(function*(cwd) {
45
- return yield fileExists(path.resolve(cwd, 'package.json'));
51
+ return yield fileExists(path__default.default.resolve(cwd, 'package.json'));
46
52
  });
47
53
  return _hasPackageJson.apply(this, arguments);
48
54
  }
@@ -51,10 +57,10 @@ function getPackageMeta(cwd) {
51
57
  }
52
58
  function _getPackageMeta() {
53
59
  _getPackageMeta = _async_to_generator$1(function*(cwd) {
54
- const pkgFilePath = path.resolve(cwd, 'package.json');
60
+ const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
55
61
  let targetPackageJson = {};
56
62
  try {
57
- targetPackageJson = JSON.parse((yield fs.readFile(pkgFilePath, {
63
+ targetPackageJson = JSON.parse((yield fs__default.default.readFile(pkgFilePath, {
58
64
  encoding: 'utf-8'
59
65
  })));
60
66
  } catch (_) {}
@@ -79,7 +85,7 @@ function fileExists(filePath) {
79
85
  function _fileExists() {
80
86
  _fileExists = _async_to_generator$1(function*(filePath) {
81
87
  try {
82
- yield fs.access(filePath);
88
+ yield fs__default.default.access(filePath);
83
89
  return true;
84
90
  } catch (err) {
85
91
  if (err.code === 'ENOENT') {
@@ -91,7 +97,7 @@ function _fileExists() {
91
97
  return _fileExists.apply(this, arguments);
92
98
  }
93
99
 
94
- var version = "3.1.0";
100
+ var version = "3.1.1";
95
101
 
96
102
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
97
103
  try {
@@ -168,7 +174,7 @@ function _lintPackage() {
168
174
  }
169
175
  function parseCliArgs(argv) {
170
176
  let args;
171
- args = arg({
177
+ args = arg__default.default({
172
178
  '--cwd': String,
173
179
  '--dts': Boolean,
174
180
  '--output': String,
@@ -220,7 +226,7 @@ function _run() {
220
226
  var _args_external;
221
227
  const { source , format , watch , minify , sourcemap , target , runtime , dts , env } = args;
222
228
  const cwd = args.cwd || process.cwd();
223
- const file = args.file ? path.resolve(cwd, args.file) : undefined;
229
+ const file = args.file ? path__default.default.resolve(cwd, args.file) : undefined;
224
230
  const bundleConfig = {
225
231
  dts,
226
232
  file,
@@ -240,7 +246,7 @@ function _run() {
240
246
  if (args.help) {
241
247
  return help();
242
248
  }
243
- const entry = source ? path.resolve(cwd, source) : '';
249
+ const entry = source ? path__default.default.resolve(cwd, source) : '';
244
250
  const bundle = require('./index').bundle;
245
251
  let timeStart = Date.now();
246
252
  let timeEnd;
package/dist/index.js CHANGED
@@ -12,6 +12,16 @@ var replace = require('@rollup/plugin-replace');
12
12
  var prettyBytes = require('pretty-bytes');
13
13
  var module$1 = require('module');
14
14
 
15
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
16
+
17
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
18
+ var path__default = /*#__PURE__*/_interopDefault(path);
19
+ var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
20
+ var shebang__default = /*#__PURE__*/_interopDefault(shebang);
21
+ var json__default = /*#__PURE__*/_interopDefault(json);
22
+ var replace__default = /*#__PURE__*/_interopDefault(replace);
23
+ var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
24
+
15
25
  function chunkSizeCollector() {
16
26
  const sizes = new Map();
17
27
  function addSize(name, size) {
@@ -25,10 +35,10 @@ function chunkSizeCollector() {
25
35
  // Do nothing, but use the hook to keep the plugin instance alive
26
36
  },
27
37
  renderChunk (code, chunk, options) {
28
- const dir = options.dir || options.file && path.dirname(options.file);
38
+ const dir = options.dir || options.file && path__default.default.dirname(options.file);
29
39
  let fileName = chunk.fileName;
30
40
  if (dir) {
31
- fileName = path.relative(cwd, path.join(dir, fileName));
41
+ fileName = path__default.default.relative(cwd, path__default.default.join(dir, fileName));
32
42
  }
33
43
  addSize(fileName, code.length);
34
44
  return null;
@@ -40,7 +50,7 @@ function chunkSizeCollector() {
40
50
  sizes.forEach((size, name)=>{
41
51
  sizeStats.push([
42
52
  name,
43
- prettyBytes(size),
53
+ prettyBytes__default.default(size),
44
54
  size
45
55
  ]);
46
56
  });
@@ -72,7 +82,46 @@ function parseExport(exportsCondition) {
72
82
  }
73
83
  return paths;
74
84
  }
75
- function getExportPaths(pkg) {
85
+ /**
86
+ * Get package exports paths
87
+ *
88
+ * Example:
89
+ *
90
+ * ```json
91
+ * {
92
+ * "exports": {
93
+ * ".": {
94
+ * "require": "./dist/index.cjs",
95
+ * "module": "./dist/index.esm.js",
96
+ * "default": "./dist/index.esm.js"
97
+ * },
98
+ * "./foo": {
99
+ * "require": "./dist/foo.cjs",
100
+ * "module": "./dist/foo.esm.js",
101
+ * "default": "./dist/foo.esm.js"
102
+ * }
103
+ * }
104
+ *
105
+ * ```
106
+ *
107
+ * will be parsed to:
108
+ *
109
+ * ```js
110
+ * {
111
+ * '.': {
112
+ * main: './dist/index.cjs',
113
+ * module: './dist/index.esm.js',
114
+ * export: './dist/index.esm.js'
115
+ * },
116
+ * './foo': {
117
+ * main: './dist/foo.cjs',
118
+ * module: './dist/foo.esm.js',
119
+ * export: './dist/foo.esm.js'
120
+ * }
121
+ *
122
+ *
123
+ * pkg.main and pkg.module will be added to ['.'] if exists
124
+ */ function getExportPaths(pkg) {
76
125
  const pathsMap = {};
77
126
  const mainExport = {};
78
127
  if (pkg.main) {
@@ -203,10 +252,10 @@ function getPackageMeta(cwd) {
203
252
  }
204
253
  function _getPackageMeta() {
205
254
  _getPackageMeta = _async_to_generator$3(function*(cwd) {
206
- const pkgFilePath = path.resolve(cwd, 'package.json');
255
+ const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
207
256
  let targetPackageJson = {};
208
257
  try {
209
- targetPackageJson = JSON.parse((yield fs.readFile(pkgFilePath, {
258
+ targetPackageJson = JSON.parse((yield fs__default.default.readFile(pkgFilePath, {
210
259
  encoding: 'utf-8'
211
260
  })));
212
261
  } catch (_) {}
@@ -231,7 +280,7 @@ function fileExists(filePath) {
231
280
  function _fileExists() {
232
281
  _fileExists = _async_to_generator$3(function*(filePath) {
233
282
  try {
234
- yield fs.access(filePath);
283
+ yield fs__default.default.access(filePath);
235
284
  return true;
236
285
  } catch (err) {
237
286
  if (err.code === 'ENOENT') {
@@ -245,15 +294,15 @@ function _fileExists() {
245
294
  // . -> pkg name
246
295
  // ./lite -> <pkg name>/lite
247
296
  function getExportPath(pkg, cwd, exportName) {
248
- const name = pkg.name || path.basename(cwd);
297
+ const name = pkg.name || path__default.default.basename(cwd);
249
298
  if (exportName === '.' || !exportName) return name;
250
- return path.join(name, exportName);
299
+ return path__default.default.join(name, exportName);
251
300
  }
252
301
  const isNotNull = (n)=>Boolean(n);
253
302
  const SRC = 'src' // resolve from src/ directory
254
303
  ;
255
304
  function resolveSourceFile(cwd, filename) {
256
- return path.resolve(cwd, SRC, filename);
305
+ return path__default.default.resolve(cwd, SRC, filename);
257
306
  }
258
307
  // Map '.' -> './index.[ext]'
259
308
  // Map './lite' -> './lite.[ext]'
@@ -367,7 +416,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
367
416
  const hasSpecifiedTsTarget = Boolean((tsCompilerOptions == null ? void 0 : tsCompilerOptions.target) && tsConfigPath);
368
417
  const sizePlugin = sizeCollector.plugin(cwd);
369
418
  const commonPlugins = [
370
- shebang(),
419
+ shebang__default.default(),
371
420
  sizePlugin
372
421
  ];
373
422
  const plugins = (dtsOnly ? [
@@ -390,7 +439,7 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
390
439
  })
391
440
  ] : [
392
441
  ...commonPlugins,
393
- replace({
442
+ replace__default.default({
394
443
  values: getBuildEnv(options.env || []),
395
444
  preventAssignment: true
396
445
  }),
@@ -404,10 +453,10 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
404
453
  '.jsx'
405
454
  ]
406
455
  }),
407
- commonjs({
456
+ commonjs__default.default({
408
457
  include: /node_modules\//
409
458
  }),
410
- json(),
459
+ json__default.default(),
411
460
  rollupPluginSwc3.swc({
412
461
  include: /\.(m|c)?[jt]sx?$/,
413
462
  exclude: 'node_modules',
@@ -460,13 +509,22 @@ function buildInputConfig(entry, pkg, options, cwd, { tsConfigPath , tsCompilerO
460
509
  }
461
510
  };
462
511
  }
512
+ function hasEsmExport(exportPaths, tsCompilerOptions) {
513
+ let hasEsm = false;
514
+ for(const key in exportPaths){
515
+ const exportInfo = exportPaths[key];
516
+ if (exportInfo.import || exportInfo.module) {
517
+ hasEsm = true;
518
+ break;
519
+ }
520
+ }
521
+ return Boolean(hasEsm || (tsCompilerOptions == null ? void 0 : tsCompilerOptions.esModuleInterop));
522
+ }
463
523
  function buildOutputConfigs(pkg, options, cwd, { tsCompilerOptions }, dtsOnly) {
464
524
  const { format , exportCondition } = options;
465
525
  const exportPaths = getExportPaths(pkg);
466
- // respect if tsconfig.json has `esModuleInterop` config;
467
- // add ESModule mark if cjs and ESModule are both generated;
468
- const mainExport = exportPaths['.'];
469
- const useEsModuleMark = Boolean(tsCompilerOptions.esModuleInterop || mainExport.main && mainExport.module);
526
+ // Add esm mark and interop helper if esm export is detected
527
+ const useEsModuleMark = hasEsmExport(exportPaths, tsCompilerOptions);
470
528
  const typings = getTypings(pkg);
471
529
  const file = options.file && path.resolve(cwd, options.file);
472
530
  const dtsDir = typings ? path.dirname(path.resolve(cwd, typings)) : path.resolve(cwd, 'dist');
@@ -486,7 +544,8 @@ function buildOutputConfigs(pkg, options, cwd, { tsCompilerOptions }, dtsOnly)
486
544
  }, {
487
545
  format,
488
546
  exports: 'named',
489
- esModule: useEsModuleMark,
547
+ esModule: useEsModuleMark || 'if-default-prop',
548
+ interop: 'auto',
490
549
  freeze: false,
491
550
  strict: false,
492
551
  sourcemap: options.sourcemap
@@ -749,7 +808,7 @@ function _bundle() {
749
808
  }
750
809
  return runBundle(rollupConfig);
751
810
  };
752
- const hasSpecifiedEntryFile = entryPath ? (yield fileExists(entryPath)) && (yield fs.stat(entryPath)).isFile() : false;
811
+ const hasSpecifiedEntryFile = entryPath ? (yield fileExists(entryPath)) && (yield fs__default.default.stat(entryPath)).isFile() : false;
753
812
  if (!hasSpecifiedEntryFile && !isMultiEntries) {
754
813
  const err = new Error(`Entry file \`${entryPath}\` is not existed`);
755
814
  err.name = 'NOT_EXISTED';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": {
6
6
  "bunchee": "./dist/cli.js"