bunchee 4.4.4 → 4.4.6

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
@@ -481,13 +481,13 @@ function lint$1(pkg) {
481
481
  }
482
482
  }
483
483
  } else {
484
+ // Validate CJS package
484
485
  if (main && path__default.default.extname(main) === '.mjs') {
485
486
  state.badMainExtension = true;
486
487
  }
487
- // Validate CJS package
488
488
  if (exports) {
489
489
  if (typeof exports === 'string') {
490
- if (!hasCjsExtension(exports)) {
490
+ if (path__default.default.extname(exports) === '.mjs') {
491
491
  state.badMainExport = true;
492
492
  }
493
493
  }
@@ -523,7 +523,11 @@ function lint$1(pkg) {
523
523
  logger.warn('Cannot export `main` field with .mjs extension in CJS package, only .js extension is allowed');
524
524
  }
525
525
  if (state.badMainExport) {
526
- logger.warn('Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
526
+ if (isESM) {
527
+ logger.warn('Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
528
+ } else {
529
+ logger.warn('Cannot export `exports` field with .mjs extension in CJS package, only .js and .cjs extensions are allowed');
530
+ }
527
531
  }
528
532
  if (state.invalidExportsFieldType) {
529
533
  logger.warn('Invalid exports field type, only object or string is allowed');
@@ -554,7 +558,7 @@ function lint$1(pkg) {
554
558
  }
555
559
  }
556
560
 
557
- var version = "4.4.4";
561
+ var version = "4.4.6";
558
562
 
559
563
  function relativify(path) {
560
564
  return path.startsWith('.') ? path : `./${path}`;
package/dist/index.js CHANGED
@@ -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
 
@@ -304,6 +304,92 @@ async function writeDefaultTsconfig(tsConfigPath) {
304
304
  logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
305
305
  }
306
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
+ }
392
+
307
393
  const helpers = {
308
394
  cssImport: {
309
395
  // have to assign r.type = 'text/css' to make it work in Safari
@@ -931,6 +1017,9 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
931
1017
  checkJs: false,
932
1018
  declarationMap: false,
933
1019
  skipLibCheck: true,
1020
+ // preserveSymlinks should always be set to false to avoid issues with
1021
+ // resolving types from <reference> from node_modules
1022
+ preserveSymlinks: false,
934
1023
  target: 'ESNext',
935
1024
  ...!tsCompilerOptions.jsx ? {
936
1025
  jsx: 'react-jsx'
@@ -955,7 +1044,6 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
955
1044
  rawContent({
956
1045
  exclude: /node_modules/
957
1046
  }),
958
- esmShim__default.default(),
959
1047
  preserveDirectives__default.default(),
960
1048
  prependDirectives(),
961
1049
  replace__default.default({
@@ -966,6 +1054,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
966
1054
  preferBuiltins: runtime === 'node',
967
1055
  extensions: nodeResolveExtensions
968
1056
  }),
1057
+ bundleConfig.format === 'esm' && esmShim(),
969
1058
  pluginWasm.wasm(),
970
1059
  rollupPluginSwc3.swc({
971
1060
  include: availableESExtensionsRegex,
@@ -1612,11 +1701,12 @@ function logWatcherBuildTime(result) {
1612
1701
  let startTime = 0;
1613
1702
  result.map((watcher)=>{
1614
1703
  function start() {
1615
- if (startTime === 0) startTime = perf_hooks.performance.now();
1704
+ if (watcherCounter === 0) startTime = perf_hooks.performance.now();
1705
+ watcherCounter++;
1616
1706
  }
1617
1707
  function end() {
1618
- watcherCounter++;
1619
- if (watcherCounter === result.length) {
1708
+ watcherCounter--;
1709
+ if (watcherCounter === 0) {
1620
1710
  logger.info(`Build in ${(perf_hooks.performance.now() - startTime).toFixed(2)}ms`);
1621
1711
  }
1622
1712
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "4.4.4",
3
+ "version": "4.4.6",
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",