Regarding this part of the question:
Will the first one import everything, even if I do not use them in the main code?
Here's how it compiles 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 entire file is imported via require
.
When importing wildcards, the _interopRequireWildcard
function is _interopRequireWildcard
and is used to assign the entire export to a namespace variable.
It is worth noting that the compiled code will contain only one _interopRequireWildcard
definition and one call to require
and _interopRequireWildcard
for each import.
Ultimately, using wildcard imports will require a bit more processing at runtime and will result in a slight increase in the size of the compiled js.
source share