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

Renaming of variables? #15

Closed
dbashford opened this issue Nov 25, 2014 · 3 comments
Closed

Renaming of variables? #15

dbashford opened this issue Nov 25, 2014 · 3 comments
Labels

Comments

@dbashford
Copy link

import bar from 'foo'
bar.what();

Turns into this

(function () {
    'use strict';
    var foo = require('foo');   
    foo.default.what();
}).call(global);

So bar is renamed to foo. If I have a foo or declare a foo I'll get a collision. Also debugging could become a pain with the name change, but source maps take care of that (just haven't got to that yet).

Guess the renaming was purposeful?

@dbashford
Copy link
Author

Should say, if it was purposeful and the collision issue is an understood and calculated risk, figuring its not a common use case, that's cool. Not sure what the mitigation would be in other than to check variables in scope... or to be in scope... which may be a bridge entirely too far given the odd use case.

@Rich-Harris
Copy link
Contributor

Yeah, I realised this the other day but hadn't got round to creating an issue. It is purposeful - if you're in non-strict mode, you get this result...

(function () {
    'use strict';

    var bar = require('foo')
    bar.what();
}).call(global);

...which is both more readable than __imports_0 or whatever, and guarantees no collisions (because a var bar or function bar () {} in the same module would be a syntax error). Something similar needs to happen for strict mode transformations - will mark this as a bug.

Bundling is a bit trickier, because you need a variable name based on the module ID rather than the import name (otherwise it might vary within the bundle), and you have to deconflict across all modules. Still do-able though

@Rich-Harris
Copy link
Contributor

@dbashford This behaves a bit better in 0.4.7 - default imports like import bar from 'foo' will infer the variable name from the import declaration; batch imports (import * from 'fs') will do likewise. If you're using named imports, the module name will be based on the path (relative to the module), but if that collides with variable names it will keep prefixing it with _ until there's no collision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants