Is it ok to use the exports
keyword to access the exported values, inside the module it self ? ( is it part of the es6 modules spec ? )
It seems to be working fine with webpack and babel / commonjs modules, the exports
keywords contains a reference to all exported methods.
But my concern is that this might not be valid and maybe this is only possible because it's a babel / commonjs enviroment.
Also I am not sure how this would affect tree-shaking, as now it would be impossible to determine which exports are actually being used, since the bindMethods
method can access all methods in this module dynamically, without having to declare explicit names.
Example:
import bindMethods from 'module';
var binder = bindMethods(exports);
export function foo(value){
binder(value).bar();
}
export function bar(value){
binder(value).foo();
}
Yes, you'd be right in saying using exports
like that isn't valid es6 and is only working because babel transpiles your code into commonjs.
Webpack 1 doesn't support tree shaking so it shouldn't cause any problems there. It would be interesting to see what happens if you disable transform-es2015-modules-commonjs
and use Webpack 2 to see whether it does effect tree shaking -- I highly doubt it will though.