bunchee 4.4.3 → 4.4.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
@@ -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
 
@@ -553,12 +554,17 @@ function lint$1(pkg) {
553
554
  }
554
555
  }
555
556
 
556
- var version = "4.4.3";
557
+ var version = "4.4.5";
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
package/dist/index.js CHANGED
@@ -5,8 +5,8 @@ var fsp = require('fs/promises');
5
5
  var fs = require('fs');
6
6
  var path = require('path');
7
7
  var perf_hooks = require('perf_hooks');
8
- var require$$0 = require('tty');
9
8
  var module$1 = require('module');
9
+ var require$$0 = require('tty');
10
10
  var rimraf = require('rimraf');
11
11
  var pluginWasm = require('@rollup/plugin-wasm');
12
12
  var rollupPluginSwc3 = require('rollup-plugin-swc3');
@@ -14,8 +14,8 @@ var commonjs = require('@rollup/plugin-commonjs');
14
14
  var json = require('@rollup/plugin-json');
15
15
  var pluginNodeResolve = require('@rollup/plugin-node-resolve');
16
16
  var replace = require('@rollup/plugin-replace');
17
- var esmShim = require('@rollup/plugin-esm-shim');
18
17
  var preserveDirectives = require('rollup-preserve-directives');
18
+ var MagicString = require('magic-string');
19
19
  var CleanCSS = require('clean-css');
20
20
  var pluginutils = require('@rollup/pluginutils');
21
21
  var prettyBytes = require('pretty-bytes');
@@ -29,8 +29,8 @@ var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
29
29
  var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
30
30
  var json__default = /*#__PURE__*/_interopDefault(json);
31
31
  var replace__default = /*#__PURE__*/_interopDefault(replace);
32
- var esmShim__default = /*#__PURE__*/_interopDefault(esmShim);
33
32
  var preserveDirectives__default = /*#__PURE__*/_interopDefault(preserveDirectives);
33
+ var MagicString__default = /*#__PURE__*/_interopDefault(MagicString);
34
34
  var CleanCSS__default = /*#__PURE__*/_interopDefault(CleanCSS);
35
35
  var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
36
36
 
@@ -244,9 +244,24 @@ const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.
244
244
  const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
245
245
  // TODO: add unit test
246
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
+ };
247
262
 
248
263
  let hasLoggedTsWarning = false;
249
- function resolveTypescript(cwd) {
264
+ function resolveTypescriptHandler(cwd) {
250
265
  let ts;
251
266
  const m = new module$1.Module('', undefined);
252
267
  m.paths = module$1.Module._nodeModulePaths(cwd);
@@ -261,7 +276,8 @@ function resolveTypescript(cwd) {
261
276
  }
262
277
  return ts;
263
278
  }
264
- function resolveTsConfig(cwd) {
279
+ const resolveTypescript = memoize(resolveTypescriptHandler);
280
+ function resolveTsConfigHandler(cwd) {
265
281
  let tsCompilerOptions = {};
266
282
  let tsConfigPath;
267
283
  tsConfigPath = path.resolve(cwd, 'tsconfig.json');
@@ -278,10 +294,101 @@ function resolveTsConfig(cwd) {
278
294
  tsConfigPath
279
295
  };
280
296
  }
297
+ const resolveTsConfig = memoize(resolveTsConfigHandler);
281
298
  async function convertCompilerOptions(cwd, json) {
282
299
  const ts = resolveTypescript(cwd);
283
300
  return ts.convertCompilerOptionsFromJson(json, './');
284
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
+ }
306
+
307
+ const FILENAME_REGEX = /__filename/;
308
+ const DIRNAME_REGEX = /__dirname/;
309
+ const GLOBAL_REQUIRE_REGEX = /require(\.resolve)?\(/;
310
+ const PolyfillComment = '/** rollup-private-do-not-use-esm-shim-polyfill */';
311
+ const createESMShim = ({ filename, dirname, globalRequire })=>{
312
+ const useNodeUrl = filename || dirname;
313
+ const useNodePath = dirname;
314
+ const useNodeModule = globalRequire;
315
+ return `\
316
+ ${PolyfillComment}
317
+ ${useNodeUrl ? `import __node_cjsUrl from 'node:url'` : ''};
318
+ ${useNodePath ? `import __node_cjsPath from 'node:path';` : ''}
319
+ ${useNodeModule ? `import __node_cjsModule from 'node:module';` : ''}
320
+ ${useNodeUrl ? 'const __filename = __node_cjsUrl.fileURLToPath(import.meta.url);' : ''}
321
+ ${useNodePath ? 'const __dirname = __node_cjsPath.dirname(__filename);' : ''}
322
+ ${useNodeModule ? 'const require = __node_cjsModule.createRequire(import.meta.url);' : ''}
323
+ `.trim() + '\n';
324
+ };
325
+ function esmShim() {
326
+ return {
327
+ name: 'esm-shim',
328
+ transform: {
329
+ order: 'post',
330
+ handler (code, id) {
331
+ const ext = path.extname(id);
332
+ if (!availableESExtensionsRegex.test(ext) || code.includes(PolyfillComment)) {
333
+ return null;
334
+ }
335
+ let hasFilename = false;
336
+ let hasDirname = false;
337
+ let hasGlobalRequire = false;
338
+ if (FILENAME_REGEX.test(code)) {
339
+ hasFilename = true;
340
+ }
341
+ if (DIRNAME_REGEX.test(code)) {
342
+ hasDirname = true;
343
+ }
344
+ if (GLOBAL_REQUIRE_REGEX.test(code)) {
345
+ hasGlobalRequire = true;
346
+ }
347
+ if (!hasFilename && !hasDirname && !hasGlobalRequire) {
348
+ return null;
349
+ }
350
+ const magicString = new MagicString__default.default(code);
351
+ let ast = null;
352
+ try {
353
+ // rollup 2 built-in parser doesn't have `allowShebang`, we need to use the sliced code here. Hence the `magicString.toString()`
354
+ ast = this.parse(magicString.toString(), {
355
+ allowReturnOutsideFunction: true
356
+ });
357
+ } catch (e) {
358
+ console.warn(e);
359
+ return null;
360
+ }
361
+ if (ast.type !== 'Program') {
362
+ return null;
363
+ }
364
+ let lastImportNode = null;
365
+ for (const node of ast.body){
366
+ if (node.type === 'ImportDeclaration') {
367
+ lastImportNode = node;
368
+ continue;
369
+ }
370
+ }
371
+ let end = 0;
372
+ if (lastImportNode) {
373
+ end = lastImportNode.end;
374
+ } else {
375
+ end = ast.body.length > 0 ? ast.body[0].end : 0;
376
+ }
377
+ magicString.appendRight(end, '\n' + createESMShim({
378
+ filename: hasFilename,
379
+ dirname: hasDirname,
380
+ globalRequire: hasGlobalRequire
381
+ }));
382
+ return {
383
+ code: magicString.toString(),
384
+ map: magicString.generateMap({
385
+ hires: true
386
+ })
387
+ };
388
+ }
389
+ }
390
+ };
391
+ }
285
392
 
286
393
  const helpers = {
287
394
  cssImport: {
@@ -878,6 +985,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
878
985
  // common plugins for both dts and ts assets that need to be processed
879
986
  const aliasFormat = dts ? ((_bundleConfig_file = bundleConfig.file) == null ? void 0 : _bundleConfig_file.endsWith('.d.cts')) ? 'cjs' : 'esm' : bundleConfig.format;
880
987
  const commonPlugins = [
988
+ json__default.default(),
881
989
  sizePlugin,
882
990
  aliasEntries({
883
991
  entry,
@@ -933,7 +1041,6 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
933
1041
  rawContent({
934
1042
  exclude: /node_modules/
935
1043
  }),
936
- esmShim__default.default(),
937
1044
  preserveDirectives__default.default(),
938
1045
  prependDirectives(),
939
1046
  replace__default.default({
@@ -944,6 +1051,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
944
1051
  preferBuiltins: runtime === 'node',
945
1052
  extensions: nodeResolveExtensions
946
1053
  }),
1054
+ bundleConfig.format === 'esm' && esmShim(),
947
1055
  pluginWasm.wasm(),
948
1056
  rollupPluginSwc3.swc({
949
1057
  include: availableESExtensionsRegex,
@@ -954,8 +1062,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
954
1062
  }),
955
1063
  commonjs__default.default({
956
1064
  exclude: bundleConfig.external || null
957
- }),
958
- json__default.default()
1065
+ })
959
1066
  ]).filter(isNotNull);
960
1067
  return {
961
1068
  input: entry,
@@ -1474,7 +1581,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1474
1581
  ;
1475
1582
  const hasBin = Boolean(pkg.bin);
1476
1583
  const isFromCli = Boolean(cliEntryPath);
1477
- let tsConfig = await resolveTsConfig(cwd);
1584
+ let tsConfig = resolveTsConfig(cwd);
1478
1585
  let hasTsConfig = Boolean(tsConfig == null ? void 0 : tsConfig.tsConfigPath);
1479
1586
  const defaultTsOptions = {
1480
1587
  tsConfigPath: tsConfig == null ? void 0 : tsConfig.tsConfigPath,
@@ -1536,8 +1643,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
1536
1643
  if (hasTypeScriptFiles && !hasTsConfig) {
1537
1644
  const tsConfigPath = path.resolve(cwd, 'tsconfig.json');
1538
1645
  defaultTsOptions.tsConfigPath = tsConfigPath;
1539
- await fsp__default.default.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
1540
- logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
1646
+ await writeDefaultTsconfig(tsConfigPath);
1541
1647
  hasTsConfig = true;
1542
1648
  }
1543
1649
  const sizeCollector = createOutputState({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "4.4.3",
3
+ "version": "4.4.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",
@@ -48,7 +48,6 @@
48
48
  "license": "MIT",
49
49
  "dependencies": {
50
50
  "@rollup/plugin-commonjs": "^25.0.7",
51
- "@rollup/plugin-esm-shim": "^0.1.5",
52
51
  "@rollup/plugin-json": "^6.1.0",
53
52
  "@rollup/plugin-node-resolve": "^15.2.3",
54
53
  "@rollup/plugin-replace": "^5.0.5",
@@ -58,6 +57,7 @@
58
57
  "@swc/helpers": "^0.5.3",
59
58
  "arg": "^5.0.2",
60
59
  "clean-css": "^5.3.3",
60
+ "magic-string": "^0.30.6",
61
61
  "pretty-bytes": "^5.6.0",
62
62
  "rimraf": "^5.0.5",
63
63
  "rollup": "^4.9.4",
@@ -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
  }