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

Problems with import * as foo... #174

Closed
eigilsagafos opened this issue Jun 9, 2020 · 3 comments
Closed

Problems with import * as foo... #174

eigilsagafos opened this issue Jun 9, 2020 · 3 comments

Comments

@eigilsagafos
Copy link

I just spent some time testing esbuild in a pretty large graphql node project. I'm able to make it build at an amazing speed 🚀 however I'm having issues with duplicate declarations in the output. The issue seems to be that when using import * as moduleName they are named by the filename and prefixed _exports in the output resulting in duplicate declarations if you have multiple files with the same name.

So this:

import * as foo from "../modules/foo/mutations"
import * as bar from "../modules/bar/mutations"

becomes this:

const mutations_exports = {};
__export(mutations_exports, {
  ...
});


const mutations_exports = {};
__export(mutations_exports, {
  ...
});

The end result is multiple const mutations_exports declarations.

@evanw
Copy link
Owner

evanw commented Jun 9, 2020

Thanks for the report. Correctness issues like this are my top priority and I really want to fix this. I believe someone else was hitting this too in #147. However, I'm unable to reproduce the issue based on your description.

Here's what I get when I try this:

(() => {
  let __defineProperty = Object.defineProperty;
  let __markAsModule = (target) => {
    return __defineProperty(target, "__esModule", {value: true});
  };
  let __export = (target, all) => {
    __markAsModule(target);
    for (let name in all)
      __defineProperty(target, name, {get: all[name], enumerable: true});
  };

  // ../modules/foo/mutations.js
  const mutations_exports = {};
  __export(mutations_exports, {
    foo: () => foo
  });
  let foo = 123;

  // ../modules/bar/mutations.js
  const mutations_exports2 = {};
  __export(mutations_exports2, {
    bar: () => bar
  });
  let bar = 123;

  // example.js
  console.log(mutations_exports, mutations_exports2);
})();

My source files:

  • ../modules/foo/mutations.js

    export let foo = 123
  • ../modules/bar/mutations.js

    export let bar = 123
  • example.js

    import * as foo from "../modules/foo/mutations"
    import * as bar from "../modules/bar/mutations"
    console.log(foo, bar)

Can you provide a code sample that reproduces the issue?

@eigilsagafos
Copy link
Author

Thanks for getting back to me so quickly. Here is a reproducible example: https://github.com/eigilsagafos/esbuild-import-bug

@evanw evanw closed this as completed in 681a907 Jun 9, 2020
@evanw
Copy link
Owner

evanw commented Jun 9, 2020

Thanks for the test case. Looks like you need to use the CommonJS output format to reproduce it. The bug was that export object renaming should only be prevented for the entry point file, not for all files. This should be fixed in version 0.4.13.

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

2 participants