bunchee 5.0.1 → 5.1.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/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
  </a>
15
15
  </p>
16
16
 
17
- **bunchee** is a zero configuration bundler makes bundling JS/tS library effortless. It's built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple bundles (CommonJS or ESModule) at the same time.
17
+ **bunchee** is a zero configuration bundler makes bundling JS/TS library effortless. It's built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple bundles (CommonJS or ESModule) at the same time.
18
18
  It uses the standard exports configuration in `package.json` as the only source of truth, and uses entry file conventions to match your exports and build them into bundles.
19
19
 
20
20
  ## Quick Start
package/dist/bin/cli.js CHANGED
@@ -474,7 +474,7 @@ function lint$1(pkg) {
474
474
  }
475
475
  }
476
476
 
477
- var version = "5.0.1";
477
+ var version = "5.1.0";
478
478
 
479
479
  function relativify(path) {
480
480
  return path.startsWith('.') ? path : `./${path}`;
package/dist/index.js CHANGED
@@ -535,27 +535,25 @@ function relativify(path) {
535
535
  // e.g.
536
536
  // For a resolved file, if it's one of the entries,
537
537
  // aliases it as export path, such as <absolute file> -> <pkg>/<export path>
538
- function aliasEntries({ entry, entries, entriesAlias, format, dts }) {
539
- let currentDistPath = '';
538
+ function aliasEntries({ entry, entries, entriesAlias, format, dts, cwd }) {
540
539
  const entryAliasWithoutSelf = {
541
540
  ...entriesAlias,
542
541
  [entry]: null
543
542
  };
544
543
  const pathToRelativeDistMap = new Map();
545
544
  for (const [, exportCondition] of Object.entries(entries)){
546
- var _Object_entries_find;
547
- const { import: importCond, require: requireCond, default: defaultCond } = exportCondition.export;
548
- const firstCond = (_Object_entries_find = Object.entries(exportCondition.export).find(([key, cond])=>key !== 'types' && cond != null)) == null ? void 0 : _Object_entries_find[1];
545
+ const exportDistMaps = exportCondition.export;
549
546
  if (dts) {
550
- const fallbackCond = defaultCond || firstCond;
551
- // For cjs, use require() instead of import
552
- const firstDistPath = (format === 'cjs' ? requireCond : importCond) || fallbackCond;
553
- if (firstDistPath) {
554
- if (entry !== exportCondition.source) {
555
- pathToRelativeDistMap.set(exportCondition.source, firstDistPath);
556
- } else {
557
- currentDistPath = firstDistPath;
558
- }
547
+ var _Object_entries_find;
548
+ // Search types + [format] condition from entries map
549
+ // e.g. import.types, require.types
550
+ const typeCond = (_Object_entries_find = Object.entries(exportDistMaps).find(([composedKey, cond])=>{
551
+ const typesSet = new Set(composedKey.split('.'));
552
+ const formatCond = format === 'cjs' ? 'require' : 'import';
553
+ return typesSet.has('types') && typesSet.has(formatCond) && cond != null;
554
+ })) == null ? void 0 : _Object_entries_find[1];
555
+ if (typeCond) {
556
+ pathToRelativeDistMap.set(exportCondition.source, typeCond);
559
557
  }
560
558
  }
561
559
  }
@@ -563,23 +561,25 @@ function aliasEntries({ entry, entries, entriesAlias, format, dts }) {
563
561
  name: 'alias',
564
562
  resolveId: {
565
563
  async handler (source, importer, options) {
566
- const resolvedId = await this.resolve(source, importer, options);
567
- if (resolvedId != null) {
564
+ const resolved = await this.resolve(source, importer, options);
565
+ if (resolved != null) {
568
566
  if (dts) {
569
567
  // For types, generate relative path to the other type files,
570
568
  // this will be compatible for the node10 ts module resolution.
571
- const aliasedId = pathToRelativeDistMap.get(resolvedId.id);
572
- if (aliasedId != null && aliasedId !== currentDistPath) {
573
- const ext = path__default.default.extname(aliasedId);
574
- const filePathBase = filePathWithoutExtension(path__default.default.relative(path__default.default.dirname(currentDistPath), aliasedId));
575
- const relativePath = relativify(filePathBase + ext);
569
+ const resolvedDist = pathToRelativeDistMap.get(resolved.id);
570
+ const entryDist = pathToRelativeDistMap.get(entry);
571
+ if (resolved.id !== entry && entryDist && resolvedDist) {
572
+ const absoluteEntryDist = path__default.default.resolve(cwd, entryDist);
573
+ const absoluteResolvedDist = path__default.default.resolve(cwd, resolvedDist);
574
+ const filePathBase = path__default.default.relative(path__default.default.dirname(absoluteEntryDist), absoluteResolvedDist);
575
+ const relativePath = relativify(filePathBase);
576
576
  return {
577
577
  id: relativePath,
578
578
  external: true
579
579
  };
580
580
  }
581
581
  } else {
582
- const aliasedId = entryAliasWithoutSelf[resolvedId.id];
582
+ const aliasedId = entryAliasWithoutSelf[resolved.id];
583
583
  if (aliasedId != null) {
584
584
  return {
585
585
  id: aliasedId
@@ -615,6 +615,23 @@ function prependDirectives() {
615
615
  };
616
616
  }
617
617
 
618
+ const prependShebang = (entry)=>({
619
+ name: 'prependShebang',
620
+ transform: (code, id)=>{
621
+ if (id !== entry) return;
622
+ const shebang = '#!/usr/bin/env node\n';
623
+ if (code.startsWith(shebang)) return;
624
+ const magicString = new MagicString__default.default(code);
625
+ magicString.prepend(shebang);
626
+ return {
627
+ code: magicString.toString(),
628
+ map: magicString.generateMap({
629
+ hires: true
630
+ })
631
+ };
632
+ }
633
+ });
634
+
618
635
  function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exportToDist) {
619
636
  // End of searching, export value is file path.
620
637
  // <export key>: <export value> (string)
@@ -1202,6 +1219,7 @@ const memoizeDtsPluginByKey = memoizeByKey(createDtsPlugin);
1202
1219
  async function buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts) {
1203
1220
  var _bundleConfig_file, _bundleConfig_file1;
1204
1221
  const { entries, pkg, cwd, tsOptions: { tsConfigPath, tsCompilerOptions }, pluginContext } = buildContext;
1222
+ const isBinEntry = isBinExportPath(exportCondition.name);
1205
1223
  const hasNoExternal = bundleConfig.external === null;
1206
1224
  var _bundleConfig_external;
1207
1225
  const externals = hasNoExternal ? [] : [
@@ -1253,13 +1271,15 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
1253
1271
  entries,
1254
1272
  entriesAlias: pluginContext.entriesAlias,
1255
1273
  format: aliasFormat,
1256
- dts
1274
+ dts,
1275
+ cwd
1257
1276
  });
1258
1277
  const commonPlugins = [
1259
1278
  json__default.default(),
1260
1279
  sizePlugin
1261
1280
  ];
1262
1281
  const typesPlugins = [
1282
+ aliasPlugin,
1263
1283
  ...commonPlugins,
1264
1284
  inlineCss({
1265
1285
  skip: true
@@ -1273,10 +1293,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
1273
1293
  const dtsPlugin = await memoizeDtsPluginByKey(uniqueProcessId)(tsCompilerOptions, tsConfigPath, cwd);
1274
1294
  typesPlugins.push(dtsPlugin);
1275
1295
  }
1276
- const plugins = (dts ? [
1277
- ...typesPlugins,
1278
- aliasPlugin
1279
- ] : [
1296
+ const plugins = (dts ? typesPlugins : [
1280
1297
  ...commonPlugins,
1281
1298
  preserveDirectives__default.default(),
1282
1299
  aliasPlugin,
@@ -1286,6 +1303,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
1286
1303
  rawContent({
1287
1304
  exclude: /node_modules/
1288
1305
  }),
1306
+ isBinEntry && prependShebang(entry),
1289
1307
  replace__default.default({
1290
1308
  values: inlineDefinedValues,
1291
1309
  preventAssignment: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "5.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": "./dist/bin/cli.js",
6
6
  "main": "./dist/index.js",