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

Update requestBase to remove implied global scope reference by 'this' #1076

Closed
wants to merge 1 commit into from

Conversation

ntilwalli
Copy link

@ntilwalli ntilwalli commented Sep 30, 2016

This PR is submitted for discussion and possible merge. This PR updates requestBase so the this reference doe not imply the global scope which is aligned with ES6 module semantics and is required for proper bundling with rollup.

Rollup, upon seeing a this reference in the code whose parent scope is global will redefine this to undefined since that is conformant with ES6 module semantics. (rollup/rollup#759) In this case, since the methods in requestBase are mixed into other prototypes during run-time, in practice this does not end up referring to the global scope, so the redefinition by rollup causes errors. By enclosing the methods in a parent object the this no longer seems to imply the global scope and rollup does not modify the this references and this module gets bundled without issue.

@kornelski
Copy link
Contributor

This is a bit weird. Rollup's requirement doesn't make sense in CommonJS world. Could such transformation be done by Rollup instead?

@ntilwalli
Copy link
Author

I agree, a bit weird to workaround ES6 module semantics in a CommonJS package... ideally this would be handled with some sort of exception in rollup or some smarter parsing, but it was such a simple change I submitted the PR to have discussion/get your perspective. The issue I posted in rollup is here: rollup/rollup#1007

@ntilwalli
Copy link
Author

I discovered this PR fixes the rollup-bundling issue but not because this was being overrwritten to undefined as I first thought. The clearTimeout function in the request-base.js module ends up shadowing the global/window clearTimeout due to the way rollup transforms commonjs modules to es6... I've submitted an issue to rollup-plugin-commonjs to better understand how to approach. rollup/rollup-plugin-commonjs#127

@kornelski
Copy link
Contributor

OK, that makes more sense. Unfortunately exports.clearTimeout is a public interface in superagent, so I can't rename it.

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

Successfully merging this pull request may close these issues.

None yet

2 participants