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

swc uses _interopRequireDefault when it should use _interopRequireWildcard #1786

Closed
cspotcode opened this issue Jun 4, 2021 · 2 comments · Fixed by #1829
Closed

swc uses _interopRequireDefault when it should use _interopRequireWildcard #1786

cspotcode opened this issue Jun 4, 2021 · 2 comments · Fixed by #1829
Labels
Milestone

Comments

@cspotcode
Copy link
Contributor

cspotcode commented Jun 4, 2021

EDIT this description is wrong; see #1786 (comment)

Describe the bug

swc does not inline _interopRequireDefault when it is needed and when externalHelpers is false. Instead it inlines _interopRequireWildcard, although that function is not used.

Input code

I can reproduce the bug using SWC's JS API

swc.transformSync(`
  import Foo from "bar";
  export {Foo} from "bar";
`, {
  module: {type: 'commonjs'}
})

The output includes the _interopRequireWildcard helper, but omits _interopRequireDefault.

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
var _bar = _interopRequireDefault(require("bar"));
function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj;
    } else {
        var newObj = {
        };
        if (obj != null) {
            for(var key in obj){
                if (Object.prototype.hasOwnProperty.call(obj, key)) {
                    var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {
                    };
                    if (desc.get || desc.set) {
                        Object.defineProperty(newObj, key, desc);
                    } else {
                        newObj[key] = obj[key];
                    }
                }
            }
        }
        newObj.default = obj;
        return newObj;
    }
}
Object.defineProperty(exports, "Foo", {
    enumerable: true,
    get: function() {
        return _bar.Foo;
    }
});

Config

n/a

Expected behavior

swc inlines the _interopRequireDefault helper function when it is used.

Version
The version of @swc/wasm: 1.2.58

Additional context

Possibly related to #138

@cspotcode
Copy link
Contributor Author

I realize now that the problem is slightly different than I described. _interopRequireWildcard is the correct helper. The bug is that swc emits var _bar = _interopRequireDefault(require("bar")); when it should emit var _bar = _interopRequireWildcard(require("bar"));

@cspotcode cspotcode changed the title swc inlines _interopRequireWildcard when it needs _interopRequireDefault swc uses _interopRequireDefault when it should use _interopRequireWildcard Jun 17, 2021
@kdy1 kdy1 added this to the v1.2.62 milestone Jun 24, 2021
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 23, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants