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

Require a umd module as commonJS conflict with requirejs. #1348

Closed
wmzy opened this issue Jun 4, 2021 · 4 comments
Closed

Require a umd module as commonJS conflict with requirejs. #1348

wmzy opened this issue Jun 4, 2021 · 4 comments

Comments

@wmzy
Copy link

wmzy commented Jun 4, 2021

Look at the out code below:

  // node_modules/tslib/tslib.js
  var require_tslib = __commonJS({
  "node_modules/tslib/tslib.js"(exports, module) {
    // ...
    (function(factory) {
      var root = typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : {};
      // note: If requirejs has been load before, this branch will be run. 
      // And the `require_tslib()` will return an empty `{}`.
      if (typeof define === "function" && define.amd) {
        define("tslib", ["exports"], function(exports2) {
          factory(createExporter(root, createExporter(exports2)));
        });
      } else if (typeof module === "object" && typeof module.exports === "object") {
        factory(createExporter(root, createExporter(module.exports)));
      } else {
        factory(createExporter(root));
      }
      function createExporter(exports2, previous) {
        // ...
      }
    })(function(exporter) {
      // ...
      exporter("__importStar", __importStar);
      exporter("__importDefault", __importDefault);
    });
    }
});

If requirejs has been load before, the require_tslib() will return an empty {}.
This will result an error like #746 .

To avoid this, can we wrrap the code like:

var require_tslib = __commonJS({
  "node_modules/tslib/tslib.js"(exports, module, define /* to hide the global define */) {
   // ...
  }
});

?

@evanw
Copy link
Owner

evanw commented Jun 5, 2021

It would be wrong for esbuild to shadow define with undefined in all code that it bundles. This would make it impossible to use code that legitimately needs to access define.

However, if you would like to do that yourself, you can use esbuild's define feature for this. An example would be --define:define=undefined to replace all references to the global variable define with references to the global variable undefined.

I'm closing this issue because you can already configure esbuild to do this by yourself and is not a default behavior that should be added to esbuild for everyone.

@evanw evanw closed this as completed Jun 5, 2021
@wmzy
Copy link
Author

wmzy commented Jun 6, 2021

Even if this is not a perfect solution, I think this is still a bug of esbuid.

To define the define as undefined is a more dangerous way . It will replace all define but not only top level define.

I think the best way is to do like the rollup.

@evanw
Copy link
Owner

evanw commented Jun 6, 2021

It will replace all define but not only top level define.

That's not true. The define feature in esbuild only replaces references to global variables. It does not replace references to local variables:

$ echo "define" | ./esbuild --define:define=foo
foo;

$ echo "var define" | ./esbuild --define:define=foo
var define;

@wmzy
Copy link
Author

wmzy commented Jun 7, 2021

Yes, I got it wrong here.

But I still hope that you can consider removing the umd wrapper to reduce this weird issue and produce a smaller bundle.

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