bunchee 4.4.4 → 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
@@ -554,7 +554,7 @@ function lint$1(pkg) {
554
554
  }
555
555
  }
556
556
 
557
- var version = "4.4.4";
557
+ var version = "4.4.5";
558
558
 
559
559
  function relativify(path) {
560
560
  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
@@ -955,7 +1041,6 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
955
1041
  rawContent({
956
1042
  exclude: /node_modules/
957
1043
  }),
958
- esmShim__default.default(),
959
1044
  preserveDirectives__default.default(),
960
1045
  prependDirectives(),
961
1046
  replace__default.default({
@@ -966,6 +1051,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
966
1051
  preferBuiltins: runtime === 'node',
967
1052
  extensions: nodeResolveExtensions
968
1053
  }),
1054
+ bundleConfig.format === 'esm' && esmShim(),
969
1055
  pluginWasm.wasm(),
970
1056
  rollupPluginSwc3.swc({
971
1057
  include: availableESExtensionsRegex,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bunchee",
3
- "version": "4.4.4",
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",