From 5e93a0e4cc28ea8f080a08e3a8530b6bfdf25f42 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 6 Oct 2021 09:59:44 -0400 Subject: [PATCH] Fix exports walk (#852) (#853) * Fix exports walk (#852) Fixes #852 * Create eight-toes-pump.md * traverse default exports in type:module packages --- .changeset/eight-toes-pump.md | 5 +++++ src/index.js | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 .changeset/eight-toes-pump.md diff --git a/.changeset/eight-toes-pump.md b/.changeset/eight-toes-pump.md new file mode 100644 index 00000000..bd89f985 --- /dev/null +++ b/.changeset/eight-toes-pump.md @@ -0,0 +1,5 @@ +--- +"microbundle": patch +--- + +Fix crash when traversing `"exports"` objects (#852) diff --git a/src/index.js b/src/index.js index ed1e3f68..7ad55a0f 100644 --- a/src/index.js +++ b/src/index.js @@ -260,9 +260,12 @@ function replaceName(filename, name) { ); } -function walk(exports) { +function walk(exports, includeDefault) { + if (!exports) return null; if (typeof exports === 'string') return exports; - return walk(exports['.'] || exports.import || exports.module); + let p = exports['.'] || exports.import || exports.module; + if (!p && includeDefault) p = exports.default; + return walk(p, includeDefault); } function getMain({ options, entry, format }) { @@ -296,7 +299,7 @@ function getMain({ options, entry, format }) { mainNoExtension, ); mainsByFormat.modern = replaceName( - (pkg.exports && walk(pkg.exports)) || + (pkg.exports && walk(pkg.exports, pkg.type === 'module')) || (pkg.syntax && pkg.syntax.esmodules) || pkg.esmodule || 'x.modern.js',