developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.03k stars 362 forks source link

modern output produces different behaviour to source #971

Closed JamesLMilner closed 2 years ago

JamesLMilner commented 2 years ago

Hello!

I have a piece of code in a Class that looks like this:

  private _map: Map<string, [number, number]>;

  /** The entries() method returns a new Iterator object that contains an array of [value, value] */
  entries() {
    const getEntries = () => {
      return this._map.entries();
    };

    // Have also tried const getEntries = this._map.entries

    const iterator = function* () {
      for (let entry of getEntries()) {
        yield [entry[1], entry[1]];
      }
    };

    return {
      [Symbol.iterator]: iterator,
    };
  }

I have a Jest unit test in the module that ensures that this behaves as intended:

 it("entries - spread", () => {
     const input = [
        [0, 0],
        [1, 1],
        [2, 2],
      ] as [number, number][];

      const set = new CoordinateSet(input);
      const iterator = set.entries();

      expect([...iterator]).toStrictEqual([
        [
          [0, 0],
          [0, 0],
        ],
        [
          [1, 1],
          [1, 1],
        ],
        [
          [2, 2],
          [2, 2],
        ],
      ]);
    });
    // This passes!

However when I use the same code and use the module from npm on CodeSandbox (where it selects the modern output), entries is always an empty array - is there something going on here in the transpilation process?

JamesLMilner commented 2 years ago

Turns out this is an issue with CodeSandbox itself and not microbundle - this helped me resolve it https://github.com/codesandbox/codesandbox-client/issues/2166#issuecomment-513210419