Skip to content

Transpiling (non-conflicting) namespace reexports emits invalid code #11710

@mischnic

Description

@mischnic

Bug Report

  • I would like to work on a fix!

Current behavior

TypeError: Cannot redefine property: c

Namespace reexports are transpiled like this:

for(key ...){
  Object.defineProperty(exports, key, {
    enumerable: true,
    get: function () {
      return imported[key];
    }
  });
}

But the spec allows for reexporting a symbol twice in the same file if it is actually the same binding (see example code below).

This means that Object.defineProperty(exports, "c", ...) is called twice, causing the error.

One of the packages that do this is @fluentui/react (this came up in parcel-bundler/parcel#4399)

Input Code

export * from "./a.js";
export * from "./b.js";

// ---- everything below is just for context ---

// a.js
export { c } from './c.js'; // <-- 
export const a = "a";
// b.js
export { c } from './c.js'; // <-- 
export const b = "b";

// c.js
export function c() {
	return false;
}

Expected behavior

The first file should export a, b, c (and not throw).

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

babel --plugins @babel/plugin-transform-modules-commonjs --delete-dir-on-start src-valid -d lib

Environment

  System:
    OS: macOS 10.15.4
  Binaries:
    Node: 14.4.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  npmPackages:
    @babel/cli: ^7.10.1 => 7.10.1
    @babel/core: ^7.10.2 => 7.10.2
    @babel/plugin-transform-modules-commonjs: ^7.10.1 => 7.10.1

Possible Solution
Not use Object.defineProperty? configurable: true ? But all of these have other sideffects

Additional context

Technically, f there are conflicting reexports (see src-invalid), there should be a SyntaxError (!).

But even Rollup doesn't handle this correctly (and Babel cannot work across multiple files anyway). rollup/rollup#3629

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: modulesoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions