ES6: "import * as alias" vs "import alias"
import utils from 'utils'
imports default from 'utils' package. undefined
in the case provided.
import * as utils from 'utils'
imports entire module exports
object with all available named exports including default.
From your example:
Case A:
//utils.js
export function doSomething()
{
//...
}
Case B:
//utils.js
export function doSomething()
{
//...
}
export default function doSomethingDefault()
{
//...
}
Result:
import utils from 'utils'
utils() // (Case A: undefined, Case B: doSomethingDefault)
import * as utils from 'utils'
utils // (Case A: utils = { doSomething: function }, Case B: utils = { doSomething: function, default: function })
import { doSomething } from 'utils'
doSomething() // (both Case A and Case B: doSomething = doSomething)
The difference between Case A and Case B is that, in Case A import utils from 'utils'
, utils
will be undefined
because there is no default export. In case B, utils
will refer to doSomethingDefault
.
With import * as utils from 'utils'
, in Case A utils
will have one method (doSomething
), while in Case B utils
will have two methods (doSomething
and default
).