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

Object destructuring inside namespaces causes other exports to disappear #113

Open
AFatNiBBa opened this issue Aug 7, 2023 · 0 comments

Comments

@AFatNiBBa
Copy link

I have a file containing the following code:

namespace B {
    export function c() { }
}

export namespace A {
    export const { c } = B; 
    export function a() { }
    export function b() { }
}

console.log(A);

When building it with vite it gets turned to this:

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.alfa = {}));
})(this, (function (exports) { 'use strict';

    var B;
    ((B2) => {
      function c() {
      }
      B2.c = c;
    })(B || (B = {}));
    exports.A = void 0;
    ((A2) => {
      ({ c: A2.c } = B);
      function a() {
      }
      A2.a = a;
      function b() {
      }
      A2.b = b;
    })(exports.A || (exports.A = {}));
    console.log(exports.A);

    Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

}));

And if I run it, it logs { c: [Function: c], a: [Function: a], b: [Function: b] }.
If I use vite-plugin-solid AND the file is a .tsx, the build result is the following:

(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.alfa = {}));
})(this, (function (exports) { 'use strict';

    let B;
    (function (_B) {
      function c() {}
      _B.c = c;
    })(B || (B = {}));
    exports.A = void 0;
    (function (_A) {
      const {
        c
      } = B;
      _A.c = c;
      function b() {}
      _A.b = b;
    })(exports.A || (exports.A = {}));
    console.log(exports.A);

    Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
}));

And it outputs { c: [Function: c], b: [Function: b] } when ran.
Why does "A.a" gets removed from the namespace?
Moreover, why does changing the code like this works?

namespace B {
    export function c() { }
}

export namespace A {
    export function a() { }
    export function b() { }
    export const { c } = B; // ← Moved on the bottom of the namespace
}

console.log(A);
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

1 participant