bunchee 5.0.0-beta.2 → 5.0.0-beta.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.
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
  </a>
15
15
  </p>
16
16
 
17
- Bunchee makes bundling your library into one file effortless, with zero configuration required. It is built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple module types (CommonJS, ESModules) simultaneously.
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
@@ -252,6 +252,63 @@ Then when the library is integrated to an app such as Next.js, app bundler can t
252
252
  If you're using `"use client"` or `"use server"` in entry file, then it will be preserved on top and the dist file of that entry will become a client component.
253
253
  If you're using `"use client"` or `"use server"` in a file that used as a dependency for an entry, then that file containing directives be split into a separate chunk and hoist the directives to the top of the chunk.
254
254
 
255
+ ### Shared Modules
256
+
257
+ There're always cases that you need to share code among bundles but they don't have to be a separate entry or exports. You want to have them bundled into a shared chunk and then use them in different bundles. You can use shared module convention `[name].[layer]-runtime.[ext]` to create shared modules bundles.
258
+
259
+ <details>
260
+ <summary>Shared Utils Example</summary>
261
+
262
+ ```js
263
+ // src/util.shared-runtime.js
264
+ export function sharedUtil() {
265
+ /* ... */
266
+ }
267
+ ```
268
+
269
+ Then you can use them in different entry files:
270
+
271
+ ```js
272
+ // src/index.js
273
+ import { sharedUtil } from './util.shared-runtime'
274
+ ```
275
+
276
+ ```js
277
+ // src/lite.js
278
+ import { sharedUtil } from './util.shared-runtime'
279
+ ```
280
+
281
+ `bunchee` will bundle the shared module into a separate **layer** which matches the file name convention, in the above case it's "shared", and that bundle will be referenced by the different entry bundles.
282
+
283
+ </details>
284
+
285
+ With multiple runtime bundles, such as having `default` and `react-server` together. They could have the modules that need to be shared and kept as only one instance among different runtime bundles. You can use the shared module convention to create shared modules bundles for different runtime bundles.
286
+
287
+ <details>
288
+ <summary>Shared Runtime Module Example</summary>
289
+
290
+ ```js
291
+ 'use client'
292
+ // src/app-context.shared-runtime.js
293
+ export const AppContext = React.createContext(null)
294
+ ```
295
+
296
+ Then you can use them in different entry files:
297
+
298
+ ```js
299
+ // src/index.js
300
+ import { AppContext } from './app-context.shared-runtime'
301
+ ```
302
+
303
+ ```js
304
+ // src/index.react-server.js
305
+ import { AppContext } from './app-context.shared-runtime'
306
+ ```
307
+
308
+ `app-context.shared-runtime` will be bundled into a separate chunk that only has one instance and be shared among different runtime bundles.
309
+
310
+ </details>
311
+
255
312
  ### CLI
256
313
 
257
314
  #### CLI Options
package/dist/bin/cli.js CHANGED
@@ -350,8 +350,7 @@ function lint$1(pkg) {
350
350
  if (hasCjsExtension(exports)) {
351
351
  state.badMainExport = true;
352
352
  }
353
- }
354
- if (typeof exports !== 'object') {
353
+ } else if (typeof exports !== 'object') {
355
354
  state.invalidExportsFieldType = true;
356
355
  } else {
357
356
  parsedExports.forEach((outputPairs)=>{
@@ -389,8 +388,7 @@ function lint$1(pkg) {
389
388
  if (path__default.default.extname(exports) === '.mjs') {
390
389
  state.badMainExport = true;
391
390
  }
392
- }
393
- if (typeof exports !== 'object') {
391
+ } else if (typeof exports !== 'object') {
394
392
  state.invalidExportsFieldType = true;
395
393
  } else {
396
394
  parsedExports.forEach((outputPairs)=>{
@@ -458,7 +456,7 @@ function lint$1(pkg) {
458
456
  }
459
457
  }
460
458
 
461
- var version = "5.0.0-beta.2";
459
+ var version = "5.0.0-beta.3";
462
460
 
463
461
  function relativify(path) {
464
462
  return path.startsWith('.') ? path : `./${path}`;
package/dist/index.js CHANGED
@@ -1223,10 +1223,11 @@ function getCustomModuleLayer(moduleId) {
1223
1223
  const segments = path__default.default.basename(moduleId).split('.');
1224
1224
  if (segments.length >= 2) {
1225
1225
  const [layerSegment, ext] = segments.slice(-2);
1226
+ const baseName = segments[0];
1226
1227
  const match = layerSegment.match(/^(\w+)-runtime$/);
1227
1228
  const layer = match && match[1];
1228
1229
  if (availableExtensions.has(ext) && layer && layer.length > 0) {
1229
- return layer;
1230
+ return baseName + '-' + layer;
1230
1231
  }
1231
1232
  }
1232
1233
  return undefined;
@@ -1243,6 +1244,15 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
1243
1244
  const { isEntry } = moduleInfo;
1244
1245
  const moduleMeta = moduleInfo.meta;
1245
1246
  const moduleLayer = getModuleLater(moduleMeta);
1247
+ if (!isEntry) {
1248
+ const cachedCustomModuleLayer = splitChunksGroupMap.get(id);
1249
+ if (cachedCustomModuleLayer) return cachedCustomModuleLayer;
1250
+ const customModuleLayer = getCustomModuleLayer(id);
1251
+ if (customModuleLayer) {
1252
+ splitChunksGroupMap.set(id, customModuleLayer);
1253
+ return customModuleLayer;
1254
+ }
1255
+ }
1246
1256
  // Collect the sub modules of the entry, if they're having layer, and the same layer with the entry, push them to the dependencyGraphMap.
1247
1257
  if (isEntry) {
1248
1258
  const subModuleIds = ctx.getModuleIds();
@@ -1263,15 +1273,6 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
1263
1273
  }
1264
1274
  }
1265
1275
  }
1266
- if (!isEntry) {
1267
- const cachedCustomModuleLayer = splitChunksGroupMap.get(id);
1268
- if (cachedCustomModuleLayer) return cachedCustomModuleLayer;
1269
- const customModuleLayer = getCustomModuleLayer(id);
1270
- if (customModuleLayer) {
1271
- splitChunksGroupMap.set(id, customModuleLayer);
1272
- return customModuleLayer;
1273
- }
1274
- }
1275
1276
  // If current module has a layer, and it's not an entry
1276
1277
  if (moduleLayer && !isEntry) {
1277
1278
  // If the module is imported by the entry:
@@ -1662,8 +1663,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1662
1663
  entriesAlias
1663
1664
  }
1664
1665
  };
1665
- const buildConfigs = await buildEntryConfig(options, buildContext, false);
1666
- const assetsJobs = buildConfigs.map((rollupConfig)=>bundleOrWatch(rollupConfig));
1666
+ const assetsJobs = (await buildEntryConfig(options, buildContext, false)).map((rollupConfig)=>bundleOrWatch(rollupConfig));
1667
1667
  const typesJobs = hasTsConfig && options.dts !== false ? (await buildEntryConfig(options, buildContext, true)).map((rollupConfig)=>bundleOrWatch(rollupConfig)) : [];
1668
1668
  const totalJobs = assetsJobs.concat(typesJobs);
1669
1669
  const result = await Promise.all(totalJobs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "5.0.0-beta.2",
3
+ "version": "5.0.0-beta.3",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": "./dist/bin/cli.js",
6
6
  "main": "./dist/index.js",