bunchee 4.4.2 → 4.4.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/dist/bin/cli.js CHANGED
@@ -6,6 +6,7 @@ var fsp = require('fs/promises');
6
6
  require('rimraf');
7
7
  var require$$0 = require('tty');
8
8
  var bunchee = require('bunchee');
9
+ require('module');
9
10
 
10
11
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
12
 
@@ -161,7 +162,7 @@ const hasCjsExtension = (filename)=>path__default.default.extname(filename) ===
161
162
  const baseNameWithoutExtension = (filename)=>path__default.default.basename(filename, path__default.default.extname(filename));
162
163
  const isTestFile = (filename)=>/\.(test|spec)$/.test(baseNameWithoutExtension(filename));
163
164
 
164
- function getTypings(pkg) {
165
+ function getPackageTypings(pkg) {
165
166
  return pkg.types || pkg.typings;
166
167
  }
167
168
  // Reached the end of the export path
@@ -382,7 +383,7 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
382
383
  const defaultMainExport = constructFullExportCondition({
383
384
  ...mainExportCondition,
384
385
  module: pkg.module,
385
- types: getTypings(pkg)
386
+ types: getPackageTypings(pkg)
386
387
  }, packageType);
387
388
  if (!isEsmPackage && ((_pathsMap_ = pathsMap['.']) == null ? void 0 : _pathsMap_['require'])) {
388
389
  // pathsMap's exports.require are prioritized.
@@ -553,12 +554,17 @@ function lint$1(pkg) {
553
554
  }
554
555
  }
555
556
 
556
- var version = "4.4.2";
557
+ var version = "4.4.4";
557
558
 
558
559
  function relativify(path) {
559
560
  return path.startsWith('.') ? path : `./${path}`;
560
561
  }
561
562
 
563
+ async function writeDefaultTsconfig(tsConfigPath) {
564
+ await fs.promises.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
565
+ logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
566
+ }
567
+
562
568
  // Output with posix style in package.json
563
569
  function getDistPath(...subPaths) {
564
570
  return `./${DIST}/${subPaths.join('/')}`;
@@ -687,7 +693,7 @@ async function prepare(cwd) {
687
693
  let isUsingTs = false;
688
694
  // Collect bins and exports entries
689
695
  const { bins, exportsEntries } = await collectSourceEntries(sourceFolder);
690
- const tsconfigPath = path__default.default.join(cwd, 'tsconfig.json');
696
+ const tsConfigPath = path__default.default.join(cwd, 'tsconfig.json');
691
697
  const sourceFiles = [
692
698
  ...exportsEntries.values()
693
699
  ].concat([
@@ -696,9 +702,8 @@ async function prepare(cwd) {
696
702
  const hasTypeScriptFiles = sourceFiles.some((filename)=>isTypescriptFile(filename));
697
703
  if (hasTypeScriptFiles) {
698
704
  isUsingTs = true;
699
- if (!fs__default.default.existsSync(tsconfigPath)) {
700
- await fsp__default.default.writeFile(tsconfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
701
- logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
705
+ if (!fs__default.default.existsSync(tsConfigPath)) {
706
+ await writeDefaultTsconfig(tsConfigPath);
702
707
  }
703
708
  }
704
709
  // Configure as ESM package by default if there's no package.json
@@ -888,7 +893,7 @@ async function run(args) {
888
893
  }
889
894
  // watching mode
890
895
  if (watch) {
891
- logger.log(`Watching assets in ${cwd}...`);
896
+ logger.log(`Watching project ${cwd}...`);
892
897
  return;
893
898
  }
894
899
  // build mode
package/dist/index.js CHANGED
@@ -4,8 +4,9 @@ var rollup = require('rollup');
4
4
  var fsp = require('fs/promises');
5
5
  var fs = require('fs');
6
6
  var path = require('path');
7
- var require$$0 = require('tty');
7
+ var perf_hooks = require('perf_hooks');
8
8
  var module$1 = require('module');
9
+ var require$$0 = require('tty');
9
10
  var rimraf = require('rimraf');
10
11
  var pluginWasm = require('@rollup/plugin-wasm');
11
12
  var rollupPluginSwc3 = require('rollup-plugin-swc3');
@@ -243,9 +244,24 @@ const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.
243
244
  const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
244
245
  // TODO: add unit test
245
246
  const baseNameWithoutExtension = (filename)=>path__default.default.basename(filename, path__default.default.extname(filename));
247
+ const memoize = (fn, resolver)=>{
248
+ const cache = new Map();
249
+ return (...args)=>{
250
+ const key = resolver ? resolver(...args) : JSON.stringify({
251
+ args
252
+ });
253
+ const existing = cache.get(key);
254
+ if (existing !== undefined) {
255
+ return existing;
256
+ }
257
+ const result = fn(...args);
258
+ cache.set(key, result);
259
+ return result;
260
+ };
261
+ };
246
262
 
247
263
  let hasLoggedTsWarning = false;
248
- function resolveTypescript(cwd) {
264
+ function resolveTypescriptHandler(cwd) {
249
265
  let ts;
250
266
  const m = new module$1.Module('', undefined);
251
267
  m.paths = module$1.Module._nodeModulePaths(cwd);
@@ -260,7 +276,8 @@ function resolveTypescript(cwd) {
260
276
  }
261
277
  return ts;
262
278
  }
263
- function resolveTsConfig(cwd) {
279
+ const resolveTypescript = memoize(resolveTypescriptHandler);
280
+ function resolveTsConfigHandler(cwd) {
264
281
  let tsCompilerOptions = {};
265
282
  let tsConfigPath;
266
283
  tsConfigPath = path.resolve(cwd, 'tsconfig.json');
@@ -277,10 +294,15 @@ function resolveTsConfig(cwd) {
277
294
  tsConfigPath
278
295
  };
279
296
  }
297
+ const resolveTsConfig = memoize(resolveTsConfigHandler);
280
298
  async function convertCompilerOptions(cwd, json) {
281
299
  const ts = resolveTypescript(cwd);
282
300
  return ts.convertCompilerOptionsFromJson(json, './');
283
301
  }
302
+ async function writeDefaultTsconfig(tsConfigPath) {
303
+ await fs.promises.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
304
+ logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
305
+ }
284
306
 
285
307
  const helpers = {
286
308
  cssImport: {
@@ -461,7 +483,7 @@ function prependDirectives() {
461
483
  };
462
484
  }
463
485
 
464
- function getTypings(pkg) {
486
+ function getPackageTypings(pkg) {
465
487
  return pkg.types || pkg.typings;
466
488
  }
467
489
  // Reached the end of the export path
@@ -682,7 +704,7 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
682
704
  const defaultMainExport = constructFullExportCondition({
683
705
  ...mainExportCondition,
684
706
  module: pkg.module,
685
- types: getTypings(pkg)
707
+ types: getPackageTypings(pkg)
686
708
  }, packageType);
687
709
  if (!isEsmPackage && ((_pathsMap_ = pathsMap['.']) == null ? void 0 : _pathsMap_['require'])) {
688
710
  // pathsMap's exports.require are prioritized.
@@ -712,7 +734,7 @@ function constructDefaultExportCondition(value, packageType) {
712
734
  const isEsmPackage = isESModulePackage(packageType);
713
735
  let exportCondition;
714
736
  if (typeof value === 'string') {
715
- const types = getTypings(value);
737
+ const types = getPackageTypings(value);
716
738
  exportCondition = {
717
739
  [isEsmPackage ? 'import' : 'require']: value,
718
740
  ...types && {
@@ -788,7 +810,7 @@ const swcMinifyOptions = {
788
810
  }
789
811
  };
790
812
  // return { 'process.env.<key>': '<value>' }
791
- function getBuildEnv(envs, parsedExportCondition) {
813
+ function getDefinedInlineVariables(envs, parsedExportCondition) {
792
814
  if (!envs.includes('NODE_ENV')) {
793
815
  envs.push('NODE_ENV');
794
816
  }
@@ -808,6 +830,9 @@ function getBuildEnv(envs, parsedExportCondition) {
808
830
  } else if (exportConditionNames.has('production')) {
809
831
  envVars['process.env.NODE_ENV'] = JSON.stringify('production');
810
832
  }
833
+ if (exportConditionNames.has('edge-light')) {
834
+ envVars['EdgeRuntime'] = JSON.stringify('edge-runtime');
835
+ }
811
836
  return envVars;
812
837
  }
813
838
  /**
@@ -842,7 +867,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
842
867
  externals.push(entryFilePath);
843
868
  }
844
869
  }
845
- const envValues = getBuildEnv(bundleConfig.env || [], exportCondition);
870
+ const inlineDefinedValues = getDefinedInlineVariables(bundleConfig.env || [], exportCondition);
846
871
  const { useTypeScript } = buildContext;
847
872
  const { runtime, target: jscTarget, minify: shouldMinify } = bundleConfig;
848
873
  const hasSpecifiedTsTarget = Boolean(tsCompilerOptions.target && tsConfigPath);
@@ -874,6 +899,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
874
899
  // common plugins for both dts and ts assets that need to be processed
875
900
  const aliasFormat = dts ? ((_bundleConfig_file = bundleConfig.file) == null ? void 0 : _bundleConfig_file.endsWith('.d.cts')) ? 'cjs' : 'esm' : bundleConfig.format;
876
901
  const commonPlugins = [
902
+ json__default.default(),
877
903
  sizePlugin,
878
904
  aliasEntries({
879
905
  entry,
@@ -894,7 +920,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
894
920
  const incrementalOptions = enableIncrementalWithoutBuildInfo ? {
895
921
  incremental: false
896
922
  } : undefined;
897
- const compositeOptions = tsCompilerOptions.composite && enableIncrementalWithoutBuildInfo ? {
923
+ const compositeOptions = tsCompilerOptions.composite ? {
898
924
  composite: false
899
925
  } : undefined;
900
926
  const { options: overrideResolvedTsOptions } = await convertCompilerOptions(cwd, {
@@ -933,7 +959,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
933
959
  preserveDirectives__default.default(),
934
960
  prependDirectives(),
935
961
  replace__default.default({
936
- values: envValues,
962
+ values: inlineDefinedValues,
937
963
  preventAssignment: true
938
964
  }),
939
965
  pluginNodeResolve.nodeResolve({
@@ -950,8 +976,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
950
976
  }),
951
977
  commonjs__default.default({
952
978
  exclude: bundleConfig.external || null
953
- }),
954
- json__default.default()
979
+ })
955
980
  ]).filter(isNotNull);
956
981
  return {
957
982
  input: entry,
@@ -1470,7 +1495,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1470
1495
  ;
1471
1496
  const hasBin = Boolean(pkg.bin);
1472
1497
  const isFromCli = Boolean(cliEntryPath);
1473
- let tsConfig = await resolveTsConfig(cwd);
1498
+ let tsConfig = resolveTsConfig(cwd);
1474
1499
  let hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath);
1475
1500
  const defaultTsOptions = {
1476
1501
  tsConfigPath: tsConfig == null ? void 0 : tsConfig.tsConfigPath,
@@ -1503,11 +1528,11 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1503
1528
  const bundleOrWatch = async (rollupConfig)=>{
1504
1529
  if (options.clean) {
1505
1530
  if (!isFromCli) {
1506
- await removeOutputDir(rollupConfig.output);
1531
+ await removeOutputDir(rollupConfig.output, cwd);
1507
1532
  }
1508
1533
  }
1509
1534
  if (options.watch) {
1510
- return Promise.resolve(runWatch(rollupConfig));
1535
+ return runWatch(rollupConfig);
1511
1536
  }
1512
1537
  return runBundle(rollupConfig);
1513
1538
  };
@@ -1532,8 +1557,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1532
1557
  if (hasTypeScriptFiles && !hasTsConfig) {
1533
1558
  const tsConfigPath = path.resolve(cwd, 'tsconfig.json');
1534
1559
  defaultTsOptions.tsConfigPath = tsConfigPath;
1535
- await fsp__default.default.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
1536
- logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
1560
+ await writeDefaultTsconfig(tsConfigPath);
1537
1561
  hasTsConfig = true;
1538
1562
  }
1539
1563
  const sizeCollector = createOutputState({
@@ -1556,12 +1580,15 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1556
1580
  const buildConfigs = await buildEntryConfig(options, buildContext, false);
1557
1581
  const assetsJobs = buildConfigs.map((rollupConfig)=>bundleOrWatch(rollupConfig));
1558
1582
  const typesJobs = hasTsConfig ? (await buildEntryConfig(options, buildContext, true)).map((rollupConfig)=>bundleOrWatch(rollupConfig)) : [];
1559
- const result = await Promise.all(assetsJobs.concat(typesJobs));
1583
+ const totalJobs = assetsJobs.concat(typesJobs);
1584
+ const result = await Promise.all(totalJobs);
1560
1585
  if (result.length === 0) {
1561
1586
  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');
1562
1587
  }
1563
1588
  if (!options.watch) {
1564
1589
  logOutputState(sizeCollector);
1590
+ } else {
1591
+ logWatcherBuildTime(result);
1565
1592
  }
1566
1593
  return result;
1567
1594
  }
@@ -1578,29 +1605,45 @@ function runWatch({ input, output }) {
1578
1605
  }
1579
1606
  ];
1580
1607
  const watcher = rollup.watch(watchOptions);
1581
- watcher.on('event', (event)=>{
1582
- switch(event.code){
1583
- case 'ERROR':
1584
- {
1585
- logError(event.error);
1586
- break;
1587
- }
1588
- case 'START':
1589
- {
1590
- break;
1591
- }
1592
- case 'END':
1593
- {
1594
- break;
1595
- }
1596
- default:
1597
- return;
1608
+ return watcher;
1609
+ }
1610
+ function logWatcherBuildTime(result) {
1611
+ let watcherCounter = 0;
1612
+ let startTime = 0;
1613
+ result.map((watcher)=>{
1614
+ function start() {
1615
+ if (startTime === 0) startTime = perf_hooks.performance.now();
1616
+ }
1617
+ function end() {
1618
+ watcherCounter++;
1619
+ if (watcherCounter === result.length) {
1620
+ logger.info(`Build in ${(perf_hooks.performance.now() - startTime).toFixed(2)}ms`);
1621
+ }
1598
1622
  }
1623
+ watcher.on('event', (event)=>{
1624
+ switch(event.code){
1625
+ case 'ERROR':
1626
+ {
1627
+ logError(event.error);
1628
+ break;
1629
+ }
1630
+ case 'START':
1631
+ {
1632
+ start();
1633
+ break;
1634
+ }
1635
+ case 'END':
1636
+ {
1637
+ end();
1638
+ break;
1639
+ }
1640
+ }
1641
+ });
1599
1642
  });
1600
- return watcher;
1601
1643
  }
1602
- async function removeOutputDir(output) {
1603
- if (output.dir) await removeDir(output.dir);
1644
+ async function removeOutputDir(output, cwd) {
1645
+ const dir = output.dir;
1646
+ if (dir && dir !== cwd) await removeDir(dir);
1604
1647
  }
1605
1648
  function runBundle({ input, output }) {
1606
1649
  return rollup.rollup(input).then((bundle)=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "4.4.2",
3
+ "version": "4.4.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",
@@ -54,7 +54,7 @@
54
54
  "@rollup/plugin-replace": "^5.0.5",
55
55
  "@rollup/plugin-wasm": "^6.2.2",
56
56
  "@rollup/pluginutils": "^5.1.0",
57
- "@swc/core": "^1.3.102",
57
+ "@swc/core": "^1.3.106",
58
58
  "@swc/helpers": "^0.5.3",
59
59
  "arg": "^5.0.2",
60
60
  "clean-css": "^5.3.3",
@@ -79,7 +79,7 @@
79
79
  },
80
80
  "devDependencies": {
81
81
  "@huozhi/testing-package": "1.0.0",
82
- "@swc/jest": "^0.2.29",
82
+ "@swc/jest": "^0.2.31",
83
83
  "@swc/types": "^0.1.5",
84
84
  "@types/clean-css": "^4.2.11",
85
85
  "@types/jest": "29.0.0",
@@ -112,7 +112,8 @@
112
112
  "testPathIgnorePatterns": [
113
113
  "/node_modules/",
114
114
  "<rootDir>/test/integration/.*/*src"
115
- ]
115
+ ],
116
+ "testTimeout": 8000
116
117
  },
117
118
  "packageManager": "pnpm@8.8.0"
118
119
  }