Skip to content

more than one export * from cause Cannot redefine property when re-imports have same name export #1780

@IronLu233

Description

@IronLu233

Hi, I'm working in Ringcentral Inc. I plan to use swc for reducing build time.

I'm willing to help fix it

Describe the bug

in this demo, I have these files

// constant.ts
export enum Foo {};
// File1.ts
export {Foo} from './constant';
// file2.ts
export {Foo} from './constant';

Input code

// index.ts
export const BIZ = 'biz';
export * from './File1';
export * from './File2';

and I compile it with swc, then run the index.js

it through an error
image

the compile result of index.ts is

"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
var _exportNames = {
    BIZ: true
};
exports.BIZ = void 0;
var _file1 = require("./File1");
var _file2 = require("./File2");
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;
    }
}
var BIZ = 'biz';
exports.BIZ = BIZ;
Object.keys(_file1).forEach(function(key) {
    if (key === "default" || key === "__esModule") return;
    if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
    Object.defineProperty(exports, key, {
        enumerable: true,
        get: function() {
            return _file1[key];
        }
    });
});
Object.keys(_file2).forEach(function(key) {
    if (key === "default" || key === "__esModule") return;
    if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
    Object.defineProperty(exports, key, {
        enumerable: true,
        get: function() {
            return _file2[key];
        }
    });
});

config

{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "transform": {
      "legacyDecorator": true
    }
  },
  "module": {
    "type": "commonjs",

    "strict": false,
    "strictMode": true,
    "lazy": false,
    "noInterop": false
  }
}

Expected behavior

the index.js run and exit without error

Version

The version of @swc/core: 1.2.59

Additional context

compare with the result via babel, it has a extra check before Object.defineProperty

if (key in exports && exports[key] === _utils[key]) return;

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions