# importing selected chucks of a module into an object
 
Modules are always imported into the root scope of the imported file. But in 
some cases it can be desirable to load modules into a namespace-object. 
Especially when importing a large number of parts/chunks from a module creating 
a separate variable for each become unwieldy.
 
There's a way to load the module as/into an object using this syntax:
 
```
import * as myLib from 'myLib';
 
// usage
myLib.chunkA();
```
 
However, it’s impossible to load only certain chunks AND bundle the chucks into 
an object at the same time.
 
Currently an editor needs to do this:
 
```
import { chunkA, chunkB } from 'myLib';
const myLib = { chunkA, chunkB };
 
// usage
myLib.chunkA();
```
 
I propose this would be allowed:
 
```
import { chunkA, chunkB } as myLib from 'myLib';
 
// usage
myLib.chunkA();
```
 
This can become a useful tool when using multiple libraries with similar 
utility or namingschemes, for example:
 
```
import { parse } from 'xml-lib';
import { parse } from 'json-lib';
import { parse } from 'html-lib';
 
// usage
parse(); // :(
```
 
One of the ways this can currently be solved is like this:
 
```
import { parse as xmlParse } from 'xml-lib';
import { parse as jsonParse } from 'json-lib';
import { parse as htmlParse } from 'html-lib';
 
// usage
xmlParse();
jsonParse();
htmlParse();
```
 
Namespacing variables like this can be a valid strategy, but it can become 
cumbersome:
 
```
import { parse as xmlParse, create as xmlCreate, sandwich as xmlSandwich } from 
'xml-lib';
import { parse as jsonParse, create as jsonCreate, bacon as jsonBacon } from 
'json-lib';
import { parse as htmlParse, create as htmlCreate, orange as htmlOrange } from 
'html-lib';
```
 
It would be nice to have this option:
 
```
import { parse } as xmlLib from 'xml-lib';
import { parse } as jsonLib from 'json-lib';
import { parse } as htmlLib from 'html-lib';
 
// usage
xmlLib.parse();
jsonLib.parse();
htmlLib.parse();
```
 
It would even be usable like this, though it's not likely to be needed:
 
```
import { parse as read } as xmlLib from 'xml-lib';
 
// usage
xmlLib.read();
```


_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to