bunchee 6.3.2 → 6.3.4

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
@@ -41,12 +41,12 @@ Add the exports in `package.json`.
41
41
 
42
42
  ```json5
43
43
  {
44
- name: 'coffee',
45
- type: 'module',
46
- main: './dist/index.js',
47
- scripts: {
48
- build: 'bunchee',
49
- },
44
+ "name": "coffee",
45
+ "type": "module",
46
+ "main": "./dist/index.js",
47
+ "scripts": {
48
+ "build": "bunchee"
49
+ }
50
50
  }
51
51
  ```
52
52
 
@@ -73,23 +73,23 @@ Here's a example of entry files and exports configuration:
73
73
 
74
74
  ```json5
75
75
  {
76
- name: 'coffee',
77
- scripts: {
78
- build: 'bunchee',
76
+ "name": "coffee",
77
+ "scripts": {
78
+ "build: "bunchee",
79
79
  },
80
- type: 'module',
81
- exports: {
80
+ "type": "module",
81
+ "exports": {
82
82
  // entry: ./src/index.ts
83
- '.': {
84
- import: './dist/index.js',
85
- require: './dist/index.cjs',
83
+ ".": {
84
+ "import": "./dist/index.js",
85
+ "require": "./dist/index.cjs",
86
86
  },
87
87
 
88
88
  // entry: ./src/lite.ts
89
- './lite': './dist/lite.js',
89
+ "./lite": "./dist/lite.js",
90
90
 
91
91
  // entry: ./src/react/index.ts
92
- './react': './dist/react.js',
92
+ "./react": "./dist/react.js",
93
93
  },
94
94
  }
95
95
  ```
@@ -124,15 +124,15 @@ The `dependencies` and `peerDependencies` will be marked as externalized and won
124
124
  ```json5
125
125
  {
126
126
  // Externalized
127
- dependencies: {
127
+ "dependencies": {
128
128
  /* ... */
129
129
  },
130
- peerDependencies: {
130
+ "peerDependencies": {
131
131
  /* ... */
132
132
  },
133
133
 
134
134
  // Bundled
135
- devDependencies: {
135
+ "devDependencies": {
136
136
  /* ... */
137
137
  },
138
138
  }
@@ -146,10 +146,10 @@ For instance:
146
146
 
147
147
  ```json5
148
148
  {
149
- exports: {
150
- 'react-server': './dist/react-server.mjs',
151
- 'edge-light': './dist/edge-light.mjs',
152
- import: './dist/index.mjs',
149
+ "exports": {
150
+ "react-server": "./dist/react-server.mjs",
151
+ "edge-light": "./dist/edge-light.mjs",
152
+ "import": "./dist/index.mjs",
153
153
  },
154
154
  }
155
155
  ```
@@ -161,8 +161,8 @@ For instance:
161
161
  ```json5
162
162
  // package.json
163
163
  {
164
- imports: {
165
- '#util': './src/utils.ts',
164
+ "imports": {
165
+ "#util": "./src/utils.ts",
166
166
  },
167
167
  }
168
168
  ```
@@ -183,7 +183,7 @@ This will match the `bin` field in package.json as:
183
183
 
184
184
  ```json5
185
185
  {
186
- bin: './dist/bin.js',
186
+ "bin": "./dist/bin.js",
187
187
  }
188
188
  ```
189
189
 
@@ -204,9 +204,9 @@ This will match the `bin` field in package.json as:
204
204
 
205
205
  ```json5
206
206
  {
207
- bin: {
208
- foo: './dist/bin/a.js',
209
- bar: './dist/bin/b.js',
207
+ "bin": {
208
+ "foo": "./dist/bin/a.js",
209
+ "bar": "./dist/bin/b.js",
210
210
  },
211
211
  }
212
212
  ```
@@ -338,14 +338,14 @@ Then use use the [exports field in package.json](https://nodejs.org/api/packages
338
338
 
339
339
  ```json5
340
340
  {
341
- files: ['dist'],
342
- type: 'module',
343
- exports: {
344
- '.': './dist/es/index.js',
345
- './react': './dist/es/react.js',
341
+ "files": ["dist"],
342
+ "type": "module",
343
+ "exports": {
344
+ ".": "./dist/es/index.js",
345
+ "./react": "./dist/es/react.js",
346
346
  },
347
- scripts: {
348
- build: 'bunchee',
347
+ "scripts": {
348
+ "build": "bunchee",
349
349
  },
350
350
  }
351
351
  ```
@@ -359,21 +359,21 @@ If you're building a TypeScript library, separate the types from the main entry
359
359
 
360
360
  ```json5
361
361
  {
362
- files: ['dist'],
363
- type: 'module',
364
- main: './dist/index.js',
365
- exports: {
366
- '.': {
367
- types: './dist/index.d.ts',
368
- default: './dist/index.js',
362
+ "files": ["dist"],
363
+ "type": "module",
364
+ "main": "./dist/index.js",
365
+ "exports": {
366
+ ".": {
367
+ "types": "./dist/index.d.ts",
368
+ "default": "./dist/index.js",
369
369
  },
370
- './react': {
371
- types: './dist/react/index.d.ts',
372
- default: './dist/react/index.js',
370
+ "./react": {
371
+ "types": "./dist/react/index.d.ts",
372
+ "default": "./dist/react/index.js",
373
373
  },
374
374
  },
375
- scripts: {
376
- build: 'bunchee',
375
+ "scripts": {
376
+ "build": "bunchee",
377
377
  },
378
378
  }
379
379
  ```
@@ -388,23 +388,23 @@ _NOTE_: When you're using `.mjs` or `.cjs` extensions with TypeScript and modern
388
388
 
389
389
  ```json5
390
390
  {
391
- files: ['dist'],
392
- type: 'module',
393
- main: './dist/index.js',
394
- module: './dist/index.js',
395
- types: './dist/index.d.ts',
396
- exports: {
397
- import: {
398
- types: './dist/index.d.ts',
399
- default: './dist/index.js',
391
+ "files": ["dist"],
392
+ "type": "module",
393
+ "main": "./dist/index.js",
394
+ "module": "./dist/index.js",
395
+ "types": "./dist/index.d.ts",
396
+ "exports": {
397
+ "import": {
398
+ "types": "./dist/index.d.ts",
399
+ "default": "./dist/index.js",
400
400
  },
401
- require: {
402
- types: './dist/index.d.cts',
403
- default: './dist/index.cjs',
401
+ "require": {
402
+ "types": "./dist/index.d.cts",
403
+ "default": "./dist/index.cjs",
404
404
  },
405
405
  },
406
- scripts: {
407
- build: 'bunchee',
406
+ "scripts": {
407
+ "build": "bunchee",
408
408
  },
409
409
  }
410
410
  ```
@@ -450,9 +450,9 @@ This will match the export name `"react-server"` and `"edge-light"` then use the
450
450
 
451
451
  ```json5
452
452
  {
453
- exports: {
454
- development: './dist/index.development.js',
455
- production: './dist/index.production.js',
453
+ "exports": {
454
+ "development": './dist/index.development.js',
455
+ "production": './dist/index.production.js',
456
456
  },
457
457
  }
458
458
  ```
@@ -499,8 +499,8 @@ hello world
499
499
 
500
500
  output
501
501
 
502
- ```
503
- export default "hello world"
502
+ ```js
503
+ export default 'hello world'
504
504
  ```
505
505
 
506
506
  ### Node.js API
@@ -538,11 +538,11 @@ For example:
538
538
 
539
539
  ```json5
540
540
  {
541
- browserslist: [
542
- 'last 1 version',
543
- '> 1%',
544
- 'maintained node versions',
545
- 'not dead',
541
+ "browserslist": [
542
+ "last 1 version",
543
+ "> 1%",
544
+ "maintained node versions",
545
+ "not dead",
546
546
  ],
547
547
  }
548
548
  ```
package/dist/bin/cli.js CHANGED
@@ -176,9 +176,10 @@ function validateEntryFiles(entryFiles) {
176
176
  // Check if there are multiple files with the same base name
177
177
  const filePathWithoutExt = filePath.slice(0, -path__default.default.extname(filePath).length).replace(/\\/g, '/');
178
178
  const segments = filePathWithoutExt.split('/');
179
- const lastSegment = segments.pop() || '';
180
- if (lastSegment !== 'index' && lastSegment !== '') {
181
- segments.push(lastSegment);
179
+ let lastSegment = segments[segments.length - 1];
180
+ while(lastSegment && (lastSegment === 'index' || lastSegment === '')){
181
+ segments.pop();
182
+ lastSegment = segments[segments.length - 1];
182
183
  }
183
184
  const fileBasePath = segments.join('/');
184
185
  if (fileBasePaths.has(fileBasePath)) {
@@ -646,7 +647,7 @@ function lint$1(pkg) {
646
647
  }
647
648
  }
648
649
 
649
- var version = "6.3.2";
650
+ var version = "6.3.4";
650
651
 
651
652
  async function writeDefaultTsconfig(tsConfigPath) {
652
653
  await fs.promises.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
@@ -1049,6 +1050,16 @@ function logOutputState(stats) {
1049
1050
  });
1050
1051
  }
1051
1052
 
1053
+ function normalizeError(error) {
1054
+ // Remove the noise from rollup plugin error
1055
+ if (error.code === 'PLUGIN_ERROR') {
1056
+ const normalizedError = new Error(error.message);
1057
+ normalizedError.stack = error.stack;
1058
+ error = normalizedError;
1059
+ }
1060
+ return error;
1061
+ }
1062
+
1052
1063
  const helpMessage = `
1053
1064
  Usage: bunchee [options]
1054
1065
 
@@ -1265,10 +1276,14 @@ async function run(args) {
1265
1276
  }
1266
1277
  }
1267
1278
  }
1279
+ function onBuildError(error) {
1280
+ logError(error);
1281
+ }
1268
1282
  let buildError;
1269
1283
  bundleConfig._callbacks = {
1270
1284
  onBuildStart,
1271
- onBuildEnd
1285
+ onBuildEnd,
1286
+ onBuildError
1272
1287
  };
1273
1288
  if (watch) {
1274
1289
  logger.log(`Watching changes in the project directory...`);
@@ -1354,11 +1369,8 @@ function logWatcherBuildTime(result, spinner) {
1354
1369
  });
1355
1370
  });
1356
1371
  }
1357
- function logError(error) {
1358
- if (!error) return;
1359
- // logging source code in format
1360
- if (error.frame) {
1361
- process.stderr.write(error.frame + '\n');
1362
- }
1372
+ function logError(err) {
1373
+ const error = normalizeError(err);
1374
+ logger.error(error);
1363
1375
  }
1364
1376
  main().catch(exit);
package/dist/index.d.ts CHANGED
@@ -24,6 +24,7 @@ type BundleConfig = {
24
24
  _callbacks?: {
25
25
  onBuildStart?: (state: any) => void;
26
26
  onBuildEnd?: (assetJobs: any) => void;
27
+ onBuildError?: (assetJob: any) => void;
27
28
  };
28
29
  };
29
30
  type PackageMetadata = {
package/dist/index.js CHANGED
@@ -277,9 +277,10 @@ function validateEntryFiles(entryFiles) {
277
277
  // Check if there are multiple files with the same base name
278
278
  const filePathWithoutExt = filePath.slice(0, -path__default.default.extname(filePath).length).replace(/\\/g, '/');
279
279
  const segments = filePathWithoutExt.split('/');
280
- const lastSegment = segments.pop() || '';
281
- if (lastSegment !== 'index' && lastSegment !== '') {
282
- segments.push(lastSegment);
280
+ let lastSegment = segments[segments.length - 1];
281
+ while(lastSegment && (lastSegment === 'index' || lastSegment === '')){
282
+ segments.pop();
283
+ lastSegment = segments[segments.length - 1];
283
284
  }
284
285
  const fileBasePath = segments.join('/');
285
286
  if (fileBasePaths.has(fileBasePath)) {
@@ -1368,10 +1369,19 @@ function aliasEntries({ entry: sourceFilePath, conditionNames, entries, format,
1368
1369
  })) == null ? undefined : _exportMapEntries_find1.bundlePath;
1369
1370
  }
1370
1371
  } else {
1371
- var _exportMapEntries_find2;
1372
- matchedBundlePath = (_exportMapEntries_find2 = exportMapEntries.find((item)=>{
1372
+ var _exportMapEntries_sort_find;
1373
+ matchedBundlePath = (_exportMapEntries_sort_find = exportMapEntries.sort(// always put special condition after the general condition (default, cjs, esm)
1374
+ (a, b)=>{
1375
+ if (a.conditionNames.has(specialCondition)) {
1376
+ return -1;
1377
+ }
1378
+ if (b.conditionNames.has(specialCondition)) {
1379
+ return 1;
1380
+ }
1381
+ return 0;
1382
+ }).find((item)=>{
1373
1383
  return findJsBundlePathCallback(item, specialCondition);
1374
- })) == null ? undefined : _exportMapEntries_find2.bundlePath;
1384
+ })) == null ? undefined : _exportMapEntries_sort_find.bundlePath;
1375
1385
  }
1376
1386
  if (matchedBundlePath) {
1377
1387
  if (!sourceToRelativeBundleMap.has(exportCondition.source)) sourceToRelativeBundleMap.set(exportCondition.source, matchedBundlePath);
@@ -1499,6 +1509,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
1499
1509
  decorators: true
1500
1510
  };
1501
1511
  const hasBrowserslistConfig = !!(browserslistConfig && !hasSpecifiedTsTarget);
1512
+ const sourceMap = bundleConfig.sourcemap;
1502
1513
  const swcOptions = {
1503
1514
  jsc: {
1504
1515
  ...!hasSpecifiedTsTarget && !hasBrowserslistConfig && {
@@ -1513,11 +1524,11 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
1513
1524
  ...shouldMinify && {
1514
1525
  minify: {
1515
1526
  ...swcMinifyOptions,
1516
- sourceMap: bundleConfig.sourcemap
1527
+ sourceMap: sourceMap
1517
1528
  }
1518
1529
  }
1519
1530
  },
1520
- sourceMaps: bundleConfig.sourcemap,
1531
+ sourceMaps: sourceMap,
1521
1532
  inlineSourcesContent: false,
1522
1533
  isModule: true,
1523
1534
  ...hasBrowserslistConfig && {
@@ -1714,7 +1725,7 @@ async function buildOutputConfigs(bundleConfig, exportCondition, buildContext, d
1714
1725
  interop: 'auto',
1715
1726
  freeze: false,
1716
1727
  strict: false,
1717
- sourcemap: bundleConfig.sourcemap,
1728
+ sourcemap: bundleConfig.sourcemap && !dts,
1718
1729
  manualChunks: createSplitChunks(pluginContext.moduleDirectiveLayerMap, entryFiles),
1719
1730
  chunkFileNames () {
1720
1731
  const isCjsFormat = format === 'cjs';
@@ -1828,6 +1839,16 @@ async function buildConfig(bundleConfig, exportCondition, pluginContext, bundleE
1828
1839
  return Promise.all(outputConfigs);
1829
1840
  }
1830
1841
 
1842
+ function normalizeError(error) {
1843
+ // Remove the noise from rollup plugin error
1844
+ if (error.code === 'PLUGIN_ERROR') {
1845
+ const normalizedError = new Error(error.message);
1846
+ normalizedError.stack = error.stack;
1847
+ error = normalizedError;
1848
+ }
1849
+ return error;
1850
+ }
1851
+
1831
1852
  async function createAssetRollupJobs(options, buildContext, bundleJobOptions) {
1832
1853
  const { isFromCli, generateTypes } = bundleJobOptions;
1833
1854
  const assetsConfigs = await buildEntryConfig(options, buildContext, {
@@ -1845,7 +1866,12 @@ async function createAssetRollupJobs(options, buildContext, bundleJobOptions) {
1845
1866
  }
1846
1867
  }
1847
1868
  const rollupJobs = allConfigs.map((rollupConfig)=>bundleOrWatch(options, rollupConfig));
1848
- return await Promise.all(rollupJobs);
1869
+ try {
1870
+ return await Promise.all(rollupJobs);
1871
+ } catch (err) {
1872
+ const error = normalizeError(err);
1873
+ throw error;
1874
+ }
1849
1875
  }
1850
1876
  async function bundleOrWatch(options, rollupConfig) {
1851
1877
  if (options.watch) {
@@ -1891,7 +1917,7 @@ function hasMultiEntryExport(exportPaths) {
1891
1917
  return exportKeys.length > 0 && exportKeys.every((name)=>name.startsWith('.'));
1892
1918
  }
1893
1919
  async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1894
- var _options__callbacks_onBuildStart, _options__callbacks, _options__callbacks_onBuildEnd, _options__callbacks1;
1920
+ var _options__callbacks_onBuildStart, _options__callbacks;
1895
1921
  const cwd = path.resolve(process.cwd(), _cwd || '');
1896
1922
  assignDefault(options, 'format', 'esm');
1897
1923
  assignDefault(options, 'minify', false);
@@ -1995,8 +2021,15 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1995
2021
  isFromCli,
1996
2022
  generateTypes
1997
2023
  };
1998
- const assetJobs = await createAssetRollupJobs(options, buildContext, rollupJobsOptions);
1999
- (_options__callbacks1 = options._callbacks) == null ? undefined : (_options__callbacks_onBuildEnd = _options__callbacks1.onBuildEnd) == null ? undefined : _options__callbacks_onBuildEnd.call(_options__callbacks1, assetJobs);
2024
+ try {
2025
+ var _options__callbacks_onBuildEnd, _options__callbacks1;
2026
+ const assetJobs = await createAssetRollupJobs(options, buildContext, rollupJobsOptions);
2027
+ (_options__callbacks1 = options._callbacks) == null ? void 0 : (_options__callbacks_onBuildEnd = _options__callbacks1.onBuildEnd) == null ? void 0 : _options__callbacks_onBuildEnd.call(_options__callbacks1, assetJobs);
2028
+ } catch (error) {
2029
+ var _options__callbacks_onBuildError, _options__callbacks2;
2030
+ (_options__callbacks2 = options._callbacks) == null ? undefined : (_options__callbacks_onBuildError = _options__callbacks2.onBuildError) == null ? undefined : _options__callbacks_onBuildError.call(_options__callbacks2, error);
2031
+ return Promise.reject(error);
2032
+ }
2000
2033
  }
2001
2034
 
2002
2035
  exports.bundle = bundle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "6.3.2",
3
+ "version": "6.3.4",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": "./dist/bin/cli.js",
6
6
  "main": "./dist/index.js",
@@ -72,10 +72,8 @@
72
72
  "devDependencies": {
73
73
  "@huozhi/testing-package": "1.0.0",
74
74
  "@swc-node/register": "^1.10.9",
75
- "@swc/jest": "^0.2.37",
76
75
  "@swc/types": "^0.1.17",
77
76
  "@types/clean-css": "^4.2.11",
78
- "@types/jest": "29.0.0",
79
77
  "@types/node": "^22.9.3",
80
78
  "@types/picomatch": "^3.0.1",
81
79
  "@types/react": "19.0.1",
@@ -83,42 +81,22 @@
83
81
  "bunchee": "link:./",
84
82
  "cross-env": "^7.0.3",
85
83
  "husky": "^9.0.11",
86
- "jest": "29.7.0",
87
84
  "lint-staged": "^15.2.2",
88
85
  "next": "^15.0.4",
89
86
  "picocolors": "^1.0.0",
90
- "prettier": "2.8.8",
87
+ "prettier": "3.4.2",
91
88
  "react": "^19.0.0",
92
89
  "react-dom": "^19.0.0",
93
- "typescript": "^5.7.2"
90
+ "typescript": "^5.7.2",
91
+ "vitest": "^3.0.4"
94
92
  },
95
93
  "lint-staged": {
96
94
  "*.{js,jsx,ts,tsx,md,json,yml,yaml}": "prettier --write"
97
95
  },
98
- "jest": {
99
- "moduleDirectories": [
100
- "node_modules"
101
- ],
102
- "moduleNameMapper": {
103
- "^bunchee$": "<rootDir>/src/index.ts"
104
- },
105
- "transform": {
106
- "^.+\\.(t|j)sx?$": [
107
- "@swc/jest"
108
- ]
109
- },
110
- "testPathIgnorePatterns": [
111
- "/node_modules/",
112
- "<rootDir>/test/integration/.*/*src",
113
- "<rootDir>/test/fixtures"
114
- ],
115
- "testTimeout": 60000
116
- },
117
96
  "scripts": {
118
- "test": "jest --env node",
119
- "test:ci": "pnpm test -- --ci",
97
+ "test": "vitest run",
120
98
  "test:update": "TEST_UPDATE_SNAPSHOT=1 pnpm test",
121
- "test:post": "cross-env POST_BUILD=1 pnpm jest test/compile.test.ts test/integration.test.ts",
99
+ "test:post": "cross-env POST_BUILD=1 pnpm test test/compile.test.ts test/integration.test.ts",
122
100
  "docs:dev": "next dev docs",
123
101
  "docs:build": "next build docs",
124
102
  "clean": "rm -rf ./dist",