Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compat babel interop helper code. #1326

Closed
underfin opened this issue May 27, 2021 · 4 comments
Closed

Compat babel interop helper code. #1326

underfin opened this issue May 27, 2021 · 4 comments

Comments

@underfin
Copy link

Description

Babel compiled esm code will add interop module code. This is common usaged at react ecosystem because create-react-app use bable compiled code.
But this interop code will throw error after esbuild bundle. Here is error trace.

Uncaught TypeError: Cannot set property default of #<Object> which has only a getter
    at _interopRequireWildcard (Button.js:24)
    at node_modules/primereact/components/button/Button.js (Button.js:8)
    at __require (chunk-GZDVDKT7.js?v=b1e101bf:6)
    at node_modules/primereact/button.js (button.js:3)
    at __require (chunk-GZDVDKT7.js?v=b1e101bf:6)
    at dep:primereact_button:1

I tracked this issue found this caused by __export, it only add getter for export namespace, here is code.

var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {get: all[name], enumerable: true});
};

Same issue also happend at ts, but microsoft/TypeScript#37236 is fixed this. So i think it should be issue at esbuild.

Original issue: vitejs/vite#3437

Babel Input

import React, { Component } from 'react';

Babel Output

var _react = _interopRequireWildcard(require("react"));
function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }
      if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return {default: obj};
      }
      var cache = _getRequireWildcardCache();
      if (cache && cache.has(obj)) {
        return cache.get(obj);
      }
      var newObj = {};
      var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
      for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
          if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }
     // !! throw error at this line.
      newObj.default = obj;
      if (cache) {
        cache.set(obj, newObj);
      }
      return newObj;
    }
@evanw
Copy link
Owner

evanw commented May 28, 2021

After an initial reading of this code, this seems like a bug in Babel's _interopRequireWildcard implementation to me. It seems like the loop should skip copying over the default key if it's going to be assigned to after the loop. Otherwise the setter could throw as is the case here.

@jridgewell
Copy link
Contributor

jridgewell commented May 28, 2021

This was fixed in Babel with babel/babel#12838. Please upgrade to v7.14.0

@evanw
Copy link
Owner

evanw commented May 28, 2021

Thanks for the link! Ok, I'm going to close this issue as "working as intended" then. This is a Babel bug that has already been fixed, so you need to update your version of Babel.

@evanw evanw closed this as completed May 28, 2021
@jacobodeharo
Copy link

jacobodeharo commented May 28, 2021

Thanks, @jridgewell, @evanw do we know since which version esbuild stopped to work with such Babel scrips? It was working for me using ViteJS and stopped to work I think bumping ViteJS version and within that upgrade, the version of esbuild from v0.9 to v0.11 too.

The problem I am facing is that the error is in the roof of a 3rd party dependency, it is happening when I import components published using an earlier version of Babel. Is there any workaround until they upgrade the Babel version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants