bunchee 5.0.0-beta.4 → 5.0.0-beta.5

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/dist/bin/cli.js CHANGED
@@ -456,7 +456,7 @@ function lint$1(pkg) {
456
456
  }
457
457
  }
458
458
 
459
- var version = "5.0.0-beta.4";
459
+ var version = "5.0.0-beta.5";
460
460
 
461
461
  function relativify(path) {
462
462
  return path.startsWith('.') ? path : `./${path}`;
package/dist/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  Object.defineProperty(exports, '__esModule', { value: true });
2
2
 
3
- var rollup = require('rollup');
4
3
  var fsp = require('fs/promises');
5
4
  var fs = require('fs');
6
5
  var path = require('path');
@@ -19,6 +18,7 @@ var MagicString = require('magic-string');
19
18
  var CleanCSS = require('clean-css');
20
19
  var pluginutils = require('@rollup/pluginutils');
21
20
  var prettyBytes = require('pretty-bytes');
21
+ var rollup = require('rollup');
22
22
 
23
23
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
24
24
 
@@ -187,18 +187,6 @@ function isTypescriptFile(filename) {
187
187
  function fileExists(filePath) {
188
188
  return fs__default.default.existsSync(filePath);
189
189
  }
190
- async function removeDir(dirPath) {
191
- try {
192
- const dirStat = await fsp__default.default.stat(dirPath);
193
- if (dirStat.isDirectory()) {
194
- await rimraf.rimraf(dirPath);
195
- }
196
- } catch (err) {
197
- if (err.code !== 'ENOENT') {
198
- throw err;
199
- }
200
- }
201
- }
202
190
  const isNotNull = (n)=>Boolean(n);
203
191
  function resolveSourceFile(cwd, filename) {
204
192
  return path__default.default.resolve(cwd, SRC, filename);
@@ -287,9 +275,29 @@ function joinRelativePath(...segments) {
287
275
  function isESModulePackage(packageType) {
288
276
  return packageType === 'module';
289
277
  }
278
+ async function removeDir(dirPath) {
279
+ try {
280
+ const dirStat = await fsp__default.default.stat(dirPath);
281
+ if (dirStat.isDirectory()) {
282
+ await rimraf.rimraf(dirPath);
283
+ }
284
+ } catch (err) {
285
+ if (err.code !== 'ENOENT') {
286
+ throw err;
287
+ }
288
+ }
289
+ }
290
+ const removedDirs = new Set();
291
+ async function removeOutputDir(output, cwd) {
292
+ const dir = output.dir;
293
+ if (dir && dir !== cwd && !removedDirs.has(dir)) {
294
+ await removeDir(dir);
295
+ removedDirs.add(dir);
296
+ }
297
+ }
290
298
 
291
299
  let hasLoggedTsWarning = false;
292
- function resolveTypescriptHandler(cwd) {
300
+ function resolveTypescript(cwd) {
293
301
  let ts;
294
302
  const m = new module$1.Module('', undefined);
295
303
  m.paths = module$1.Module._nodeModulePaths(cwd);
@@ -304,12 +312,12 @@ function resolveTypescriptHandler(cwd) {
304
312
  }
305
313
  return ts;
306
314
  }
307
- const resolveTypescript = memoize(resolveTypescriptHandler);
308
315
  function resolveTsConfigHandler(cwd, tsconfig = 'tsconfig.json') {
309
316
  let tsCompilerOptions = {};
310
317
  let tsConfigPath;
311
318
  tsConfigPath = path.resolve(cwd, tsconfig);
312
319
  if (fileExists(tsConfigPath)) {
320
+ // Use the original ts handler to avoid memory leak
313
321
  const ts = resolveTypescript(cwd);
314
322
  const basePath = tsConfigPath ? path.dirname(tsConfigPath) : cwd;
315
323
  const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config;
@@ -324,6 +332,7 @@ function resolveTsConfigHandler(cwd, tsconfig = 'tsconfig.json') {
324
332
  }
325
333
  const resolveTsConfig = memoize(resolveTsConfigHandler);
326
334
  async function convertCompilerOptions(cwd, json) {
335
+ // Use the original ts handler to avoid memory leak
327
336
  const ts = resolveTypescript(cwd);
328
337
  return ts.convertCompilerOptionsFromJson(json, './');
329
338
  }
@@ -1472,6 +1481,9 @@ function createOutputState({ entries }) {
1472
1481
  if (chunk.type !== 'chunk') {
1473
1482
  return;
1474
1483
  }
1484
+ if (!chunk.isEntry) {
1485
+ return;
1486
+ }
1475
1487
  const size = chunk.code.length;
1476
1488
  const sourceFileName = chunk.facadeModuleId || '';
1477
1489
  const exportPath = removeScope(reversedMapping.get(sourceFileName) || '.');
@@ -1552,6 +1564,53 @@ function logOutputState(sizeCollector) {
1552
1564
  });
1553
1565
  }
1554
1566
 
1567
+ async function createAssetRollupJobs(options, buildContext, { isFromCli, generateTypes }) {
1568
+ const assetsConfigs = await buildEntryConfig(options, buildContext, false);
1569
+ const typesConfigs = generateTypes ? await buildEntryConfig(options, buildContext, true) : [];
1570
+ const allConfigs = assetsConfigs.concat(typesConfigs);
1571
+ for (const config of allConfigs){
1572
+ if (options.clean && !isFromCli) {
1573
+ await removeOutputDir(config.output, buildContext.cwd);
1574
+ }
1575
+ }
1576
+ const rollupJobs = allConfigs.map((rollupConfig)=>bundleOrWatch(options, rollupConfig));
1577
+ return await Promise.all(rollupJobs);
1578
+ }
1579
+ async function bundleOrWatch(options, rollupConfig) {
1580
+ if (options.watch) {
1581
+ return runWatch(rollupConfig);
1582
+ }
1583
+ return runBundle(rollupConfig);
1584
+ }
1585
+ function runBundle({ input, output }) {
1586
+ return rollup.rollup(input).then((bundle)=>{
1587
+ return bundle.write(output);
1588
+ }, catchErrorHandler);
1589
+ }
1590
+ function runWatch({ input, output }) {
1591
+ const watchOptions = [
1592
+ {
1593
+ ...input,
1594
+ output: output,
1595
+ watch: {
1596
+ exclude: [
1597
+ 'node_modules/**'
1598
+ ]
1599
+ }
1600
+ }
1601
+ ];
1602
+ const watcher = rollup.watch(watchOptions);
1603
+ return watcher;
1604
+ }
1605
+ function catchErrorHandler(error) {
1606
+ if (!error) return;
1607
+ logger.error(error);
1608
+ // filter out the rollup plugin error information such as loc/frame/code...
1609
+ const err = new Error(error.message);
1610
+ err.stack = error.stack;
1611
+ throw err;
1612
+ }
1613
+
1555
1614
  function assignDefault(options, name, defaultValue) {
1556
1615
  if (!(name in options) || options[name] == null) {
1557
1616
  options[name] = defaultValue;
@@ -1609,17 +1668,6 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1609
1668
  ].filter(Boolean));
1610
1669
  }
1611
1670
  }
1612
- const bundleOrWatch = async (rollupConfig)=>{
1613
- if (options.clean) {
1614
- if (!isFromCli) {
1615
- await removeOutputDir(rollupConfig.output, cwd);
1616
- }
1617
- }
1618
- if (options.watch) {
1619
- return runWatch(rollupConfig);
1620
- }
1621
- return runBundle(rollupConfig);
1622
- };
1623
1671
  const hasSpecifiedEntryFile = cliEntryPath ? fs__default.default.existsSync(cliEntryPath) && (await fsp__default.default.stat(cliEntryPath)).isFile() : false;
1624
1672
  const hasNoEntry = !hasSpecifiedEntryFile && !isMultiEntries && !hasBin;
1625
1673
  if (hasNoEntry) {
@@ -1663,34 +1711,21 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1663
1711
  entriesAlias
1664
1712
  }
1665
1713
  };
1666
- const assetsJobs = (await buildEntryConfig(options, buildContext, false)).map((rollupConfig)=>bundleOrWatch(rollupConfig));
1667
- const typesJobs = hasTsConfig && options.dts !== false ? (await buildEntryConfig(options, buildContext, true)).map((rollupConfig)=>bundleOrWatch(rollupConfig)) : [];
1668
- const totalJobs = assetsJobs.concat(typesJobs);
1669
- const result = await Promise.all(totalJobs);
1670
- if (result.length === 0) {
1714
+ const generateTypes = hasTsConfig && options.dts !== false;
1715
+ const rollupJobsOptions = {
1716
+ isFromCli,
1717
+ generateTypes
1718
+ };
1719
+ const assetJobs = await createAssetRollupJobs(options, buildContext, rollupJobsOptions);
1720
+ if (assetJobs.length === 0) {
1671
1721
  logger.warn('The "src" directory does not contain any entry files. ' + 'For proper usage, please refer to the following link: ' + 'https://github.com/huozhi/bunchee#usage');
1672
1722
  }
1673
- if (!options.watch) {
1674
- logOutputState(sizeCollector);
1723
+ if (options.watch) {
1724
+ logWatcherBuildTime(assetJobs);
1675
1725
  } else {
1676
- logWatcherBuildTime(result);
1726
+ logOutputState(sizeCollector);
1677
1727
  }
1678
- return result;
1679
- }
1680
- function runWatch({ input, output }) {
1681
- const watchOptions = [
1682
- {
1683
- ...input,
1684
- output: output,
1685
- watch: {
1686
- exclude: [
1687
- 'node_modules/**'
1688
- ]
1689
- }
1690
- }
1691
- ];
1692
- const watcher = rollup.watch(watchOptions);
1693
- return watcher;
1728
+ return;
1694
1729
  }
1695
1730
  function logWatcherBuildTime(result) {
1696
1731
  let watcherCounter = 0;
@@ -1727,15 +1762,6 @@ function logWatcherBuildTime(result) {
1727
1762
  });
1728
1763
  });
1729
1764
  }
1730
- async function removeOutputDir(output, cwd) {
1731
- const dir = output.dir;
1732
- if (dir && dir !== cwd) await removeDir(dir);
1733
- }
1734
- function runBundle({ input, output }) {
1735
- return rollup.rollup(input).then((bundle)=>{
1736
- return bundle.write(output);
1737
- }, catchErrorHandler);
1738
- }
1739
1765
  function logError(error) {
1740
1766
  if (!error) return;
1741
1767
  // logging source code in format
@@ -1743,13 +1769,5 @@ function logError(error) {
1743
1769
  process.stderr.write(error.frame + '\n');
1744
1770
  }
1745
1771
  }
1746
- function catchErrorHandler(error) {
1747
- if (!error) return;
1748
- logError(error);
1749
- // filter out the rollup plugin error information such as loc/frame/code...
1750
- const err = new Error(error.message);
1751
- err.stack = error.stack;
1752
- throw err;
1753
- }
1754
1772
 
1755
1773
  exports.bundle = bundle;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "5.0.0-beta.4",
3
+ "version": "5.0.0-beta.5",
4
4
  "description": "zero config bundler for js/ts/jsx libraries",
5
5
  "bin": "./dist/bin/cli.js",
6
6
  "main": "./dist/index.js",
@@ -12,7 +12,8 @@
12
12
  "clean": "rm -rf ./dist",
13
13
  "typecheck": "tsc --noEmit && tsc -p test/tsconfig.json --noEmit",
14
14
  "prepublishOnly": "pnpm clean && pnpm build && chmod +x ./dist/bin/cli.js && pnpm test",
15
- "build": "tsx ./src/bin/index.ts --runtime node",
15
+ "tsx": "node -r @swc-node/register",
16
+ "build": "node -r @swc-node/register ./src/bin/index.ts --runtime node",
16
17
  "format": "prettier --write .",
17
18
  "prepare": "husky install"
18
19
  },
@@ -53,14 +54,14 @@
53
54
  "@rollup/plugin-replace": "^5.0.5",
54
55
  "@rollup/plugin-wasm": "^6.2.2",
55
56
  "@rollup/pluginutils": "^5.1.0",
56
- "@swc/core": "^1.3.106",
57
- "@swc/helpers": "^0.5.3",
57
+ "@swc/core": "^1.4.8",
58
+ "@swc/helpers": "^0.5.6",
58
59
  "arg": "^5.0.2",
59
60
  "clean-css": "^5.3.3",
60
- "magic-string": "^0.30.6",
61
+ "magic-string": "^0.30.8",
61
62
  "pretty-bytes": "^5.6.0",
62
63
  "rimraf": "^5.0.5",
63
- "rollup": "^4.12.0",
64
+ "rollup": "^4.13.0",
64
65
  "rollup-plugin-dts": "^6.1.0",
65
66
  "rollup-plugin-swc3": "^0.11.0",
66
67
  "rollup-preserve-directives": "^1.1.1",
@@ -79,6 +80,7 @@
79
80
  },
80
81
  "devDependencies": {
81
82
  "@huozhi/testing-package": "1.0.0",
83
+ "@swc-node/register": "^1.9.0",
82
84
  "@swc/jest": "^0.2.31",
83
85
  "@swc/types": "^0.1.5",
84
86
  "@types/clean-css": "^4.2.11",
@@ -91,7 +93,6 @@
91
93
  "picocolors": "^1.0.0",
92
94
  "prettier": "^3.0.0",
93
95
  "react": "^18.2.0",
94
- "tsx": "^4.6.2",
95
96
  "typescript": "^5.3.2"
96
97
  },
97
98
  "lint-staged": {