Concerning this part of the question :
Will the first one actually importing everything even though I'm not using them in the main code?
Here's how it gets compiled with Babel 6.26:
Named
import { bar, bar2, bar3 } from './foo';
... becomes ...
'use strict';
var _foo = require('./foo');
Wildcard
import * as Foo from './foo';
... becomes ...
'use strict';
var _foo = require('./foo');
var Foo = _interopRequireWildcard(_foo);
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key))
newObj[key] = obj[key];
}
}
newObj.default = obj;
return newObj;
}
}
In both cases the whole file is imported through require
.
With wildcards imports, an _interopRequireWildcard
function is defined and used to assign all exports to the namespace variable.
It's worth noting that compiled code will only contain a single _interopRequireWildcard
definition, and one call to require
and _interopRequireWildcard
for each import.
Ultimately, the use of wildcard imports will involve a bit more processing at run-time and cause a slight increase in size for the compiled js.