Reputation:
I was going through Firefox import statements.
They have shown certain import statement like this
import defaultExport from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
But haven't added example to help differentiate between some and their example is also sort of vague.
From the docs, There are multiple things which doesn't make sense to me.. for example how is name different from defaultExport
While default export does make sense to me, the defination of name is sort of difficult to comprehend
Explanation given by them:
The name parameter is the name of the "module object" which will be used as a kind of namespace to refer to the exports.
From the above syntax: import "module-name";
Second:
import { export as alias } from "module-name";
Also, if suppose we have multiple function which we need to export
function abc1 () {
}
function abc2 () {
}
Will this be the correct way to export?
export abc1;
export abc2;
And then if we import, how would we attach variable to it? Is this where statement like this come in?
import { export as alias } from "module-name";
Upvotes: 3
Views: 1137
Reputation: 26909
Given this module module-name
:
// module-name.js
export default function foo(){ console.log("foo");}
export function bar(){ console.log("bar");}
console.log("hello world");
Consider the following cases which have been tested in node.js v9.11.1 using the command node --experimental-modules some-importer.mjs
:
default export
// import defaultExport from "module-name";
import fizzbuzz from "module-name";
Only the default export will be available thus:
fizzbuzz
(which is foo
) is availablebar
is not availableconsole.log(hello world)
will have been run*
wildcardimport * as name from "module-name";
All exports are available but attached to an Object identified as name
:
foo
is not availablebar
is not availablename.foo
is not available (though you think it would be)name.bar
is availableconsole.log(hello world)
will have been run// import { export } from "module-name";
import { bar } from "module-name"
Only the identified export is available:
foo
is not availablebar
is availableconsole.log(hello world)
will have been run// import { export as alias } from "module-name";
import { bar as mybar } from "module-name";
Only the identified export is available and only as the identified alias:
foo
is not availablebar
is not availablemybar
(which is bar
) is availableconsole.log(hello world)
will have been rundefault export
and using the *
wildcard// import defaultExport, * as name from "module-name";
import fizzbuzz, * as name from "module-name";
The default item from the module can be referenced as defaultExport
and all other exported items are attached to name
fizzbuzz
(which is foo
) is availablebar
is not availablename.bar
is availableconsole.log(hello world)
will have been runimport "module-name";
The module is loaded, but nothing is actually available in the module that imported. This means that file runs but nothing is exposed
foo
is not availablebar
is not availableconsole.log(hello world)
will have been runUpvotes: 4
Reputation: 138447
how is
name
different fromdefaultExport
?
name
is an object that holds all the exported values as exported key/values, except for the default export, which will be in defaultExport
. If you would export the following from a file:
export default function main() {}
export function helper1() {}
export function helper2() {}
Then you could import main
as the default import:
import main from "file";
That won't import the helpers. For that you would use * as
:
import * as helpers from "file";
helpers.helper1();
And then if we import, how would we attach variable to it?
They get attached to the same name they were exported with, so to only import one of the above helpers:
import { helper1 } from "file";
helper1();
If you want to rename that import because it is missleading / clashing, then the as
syntax comes in:
import { helper1 as someOther } from "file";
someOther();
Upvotes: 0