gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.
MIT License
12.5k stars 135 forks source link

fix: Make sure regeneratorRuntime isn't included in the cjs build #224

Closed gvergnaud closed 9 months ago

gvergnaud commented 9 months ago

The CJS build of TS-Pattern generates es5 javascript code, and turns the generator syntax into regeneratorRuntime calls, a function that's part of Babel's standard library:

(((t = {})[Symbol.iterator] = function () {
          /*#__PURE__*/ return regeneratorRuntime.mark(function t() {
            var r;
            return regeneratorRuntime.wrap(function (t) {
              // ...
            }, t);
          })();
        }),

The issue is that there is no guarantee that this function is in scope where TS-Pattern's CJS build is executed. This is a microbundle bug that has a closed issue here: https://github.com/developit/microbundle/issues/708 it's supposedly fixed, but I still see this behavior with the latest version of micro bundle, so this PR just turns the only generator of TS-Pattern into an iterator object.